Skip to content

Commit

Permalink
feat: 去掉本地缓存设置
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed Apr 26, 2020
1 parent 559faca commit 8565981
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
19 changes: 10 additions & 9 deletions entry/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}
}
Expand Down
5 changes: 2 additions & 3 deletions freesia.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package freesia
import (
"context"
"os"
"time"

"github.com/go-redis/redis"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 8565981

Please sign in to comment.