diff --git a/entry/setting.go b/entry/setting.go index 4259915..f066907 100644 --- a/entry/setting.go +++ b/entry/setting.go @@ -4,6 +4,15 @@ import ( "time" ) +const ( + defaultLocalExpiration = 5 * time.Second +) + +// DefaultLocalExpiration returns the default of local expiration. +func DefaultLocalExpiration() time.Duration { + return defaultLocalExpiration +} + // Setting configures an entry. type Setting func(e *Entry) error @@ -23,19 +32,11 @@ func WithExpiration(d time.Duration) Setting { } } -// WithLocalExpiration sets the local expiration for the key. -func WithLocalExpiration(d time.Duration) Setting { - return func(e *Entry) error { - e.enableLocalCache = true - e.localExp = d - return nil - } -} - // EnableLocalCache enables local cache. func EnableLocalCache() Setting { return func(e *Entry) error { e.enableLocalCache = true + e.localExp = defaultLocalExpiration return nil } } diff --git a/freesia.go b/freesia.go index 052eaa3..c8d2932 100644 --- a/freesia.go +++ b/freesia.go @@ -3,7 +3,6 @@ package freesia import ( "context" "os" - "time" "github.com/go-redis/redis" "github.com/sirupsen/logrus" @@ -136,8 +135,8 @@ func (f *Freesia) Get(e *entry.Entry) error { return redis.Nil } e.SetSource(entry.SourceCenter) - if e.EnableLocalCache() && e.TTL() > 3 { - _ = f.cache.Set(e.Key(), e.Data(), 3*time.Second) + if e.EnableLocalCache() && e.TTL() > entry.DefaultLocalExpiration().Seconds() { + _ = f.cache.Set(e.Key(), e.Data(), entry.DefaultLocalExpiration()) } } default: