Skip to content

Commit

Permalink
fix: reset
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed Apr 29, 2020
1 parent f3bbebf commit c7d6c9d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
11 changes: 11 additions & 0 deletions entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ func (e *Entry) SourceCenter() bool {
return e.loadFrom == SourceCenter
}

// Reset resets a entry.
func (e *Entry) Reset() {
e.value = nil
e.data = e.data[:0]
e.ttl = 0
e.loadFrom = sourceUnknown
}

// CheckSet checks set conditions.
func checkSet(e *Entry) error {
if int64(e.exp) == 0 {
Expand All @@ -133,6 +141,9 @@ func checkSet(e *Entry) error {
if e.enableLocalCache && int64(e.localExp) == 0 {
return errors.New("local exp must be greater than zero when enabling local cache")
}
if e.value == nil {
return errors.New("value must be non nil")
}
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion entry/load_source.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package entry

const (
_ int = iota
// init state
sourceUnknown int = iota
// SourceLocal local cache
SourceLocal
// SourceCenter center cache
Expand Down
6 changes: 6 additions & 0 deletions freesia.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (f *Freesia) Get(e *entry.Entry) error {
{
err = e.Decode(b)
if err != nil {
e.Reset()
return redis.Nil
}
ttl, _ := ttlCmd.Result()
Expand All @@ -144,6 +145,9 @@ func (f *Freesia) Get(e *entry.Entry) error {
_ = f.cache.Set(e.Key(), e.Data(), entry.DefaultLocalExpiration())
}
}
case redis.Nil:
e.Reset()
return redis.Nil
default:
return err
}
Expand Down Expand Up @@ -209,9 +213,11 @@ func (f *Freesia) batchGet(es ...*entry.Entry) ([]*entry.Entry, error) {
b, err := entryCmd.StringCmd.Bytes()
switch {
case errors.Is(err, redis.Nil):
e.Reset()
case err == nil:
err = e.Decode(b)
if err != nil {
e.Reset()
continue
}
ttl, _ := entryCmd.DurationCmd.Result()
Expand Down

0 comments on commit c7d6c9d

Please sign in to comment.