Skip to content

Commit

Permalink
feat: load source
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed Mar 26, 2020
1 parent 8461d13 commit 858b381
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Entry struct {
localExp time.Duration
data []byte
ttl float64
loadFrom int
codec codec.Codec
enableLocalCache bool
}
Expand Down Expand Up @@ -98,6 +99,18 @@ func (e *Entry) SetTTL(ttl float64) {
e.ttl = ttl
}

func (e *Entry) SourceLocal() bool {
return e.loadFrom == SourceLocal
}

func (e *Entry) SetSource(s int) {
e.loadFrom = s
}

func (e *Entry) SourceCenter() bool {
return e.loadFrom == SourceCenter
}

// CheckSet checks set conditions.
func checkSet(e *Entry) error {
if int64(e.exp) == 0 {
Expand Down
7 changes: 7 additions & 0 deletions entry/load_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package entry

const (
_ int = iota
SourceLocal
SourceCenter
)
8 changes: 8 additions & 0 deletions entry/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func WithLocalExpiration(d time.Duration) Setting {
}
}

// EnableLocalCache enables local cache.
func EnableLocalCache() Setting {
return func(e *Entry) error {
e.enableLocalCache = true
return nil
}
}

// WithValue sets the corresponding value.
func WithValue(value interface{}) Setting {
return func(e *Entry) error {
Expand Down
3 changes: 3 additions & 0 deletions freesia.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func (f *Freesia) Get(e *entry.Entry) error {
if err = e.Decode(b); ok && err != nil {
return err
}
e.SetSource(entry.SourceLocal)
return nil
default:
return err
Expand All @@ -123,6 +124,7 @@ func (f *Freesia) Get(e *entry.Entry) error {
if err != nil {
return redis.Nil
}
e.SetSource(entry.SourceCenter)
return nil
default:
return err
Expand Down Expand Up @@ -160,6 +162,7 @@ func (f *Freesia) GetWithTTL(e *entry.Entry) error {
default:
return err
}
return nil
}

func (f *Freesia) batchGet(es ...*entry.Entry) ([]*entry.Entry, error) {
Expand Down

0 comments on commit 858b381

Please sign in to comment.