Skip to content

Commit

Permalink
give a way to access the stale cache (#216)
Browse files Browse the repository at this point in the history
* Go defaults to "0"" so in case we want to return
EntryStatus back to the caller "Expired" cannot be differentiated.
Fixing this by default "_" to 0 and incremental RemoveReasons

comment was missing for GetWithInfo api so updated that

test: ran all unit test cases
Change-Id: Ic3e2259630668af0d4b5110125f2d8e43b83081b

* give a way to access the stale cache

Co-authored-by: Jay Gheewala <[email protected]>
  • Loading branch information
flisky and jgheewala authored May 7, 2020
1 parent d572104 commit ccdbc60
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
7 changes: 3 additions & 4 deletions bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,12 @@ type RemoveReason uint32

const (
// Expired means the key is past its LifeWindow.
// @TODO: Go defaults to 0 so in case we want to return EntryStatus back to the caller Expired cannot be differentiated
Expired RemoveReason = iota
Expired = RemoveReason(1)
// NoSpace means the key is the oldest and the cache size was at its maximum when Set was called, or the
// entry exceeded the maximum shard size.
NoSpace
NoSpace = RemoveReason(2)
// Deleted means Delete was called and this key was removed as a result.
Deleted
Deleted = RemoveReason(3)
)

// NewBigCache initialize new instance of BigCache
Expand Down
10 changes: 8 additions & 2 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,14 +995,20 @@ func TestBigCache_GetWithInfo(t *testing.T) {

// when
data, resp, err := cache.GetWithInfo(key)

// then
assertEqual(t, []byte(value), data)
noError(t, err)
assertEqual(t, Response{}, resp)

// when
clock.set(5)
data, resp, err = cache.GetWithInfo(key)
assertEqual(t, err, ErrEntryIsDead)

// then
assertEqual(t, err, nil)
assertEqual(t, Response{EntryStatus: Expired}, resp)
assertEqual(t, []byte(nil), data)
assertEqual(t, []byte(value), data)
}

type mockedLogger struct {
Expand Down
2 changes: 0 additions & 2 deletions entry_not_found_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ import "errors"
var (
// ErrEntryNotFound is an error type struct which is returned when entry was not found for provided key
ErrEntryNotFound = errors.New("Entry not found")
// ErrEntryIsDead is an error type struct which is returned when entry has past it's life window
ErrEntryIsDead = errors.New("entry is dead")
)
9 changes: 3 additions & 6 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@ func (s *cacheShard) getWithInfo(key string, hashedKey uint64) (entry []byte, re
return nil, resp, ErrEntryNotFound
}

entry = readEntry(wrappedEntry)
oldestTimeStamp := readTimestampFromEntry(wrappedEntry)
s.lock.RUnlock()
s.hit(hashedKey)
if currentTime-oldestTimeStamp >= s.lifeWindow {
s.lock.RUnlock()
// @TODO: when Expired is non-default value return err as nil as the resp will have proper entry status
resp.EntryStatus = Expired
return nil, resp, ErrEntryIsDead
}
entry = readEntry(wrappedEntry)
s.lock.RUnlock()
s.hit(hashedKey)
return entry, resp, nil
}

Expand Down

0 comments on commit ccdbc60

Please sign in to comment.