Skip to content

Commit

Permalink
give a way to access the stale cache
Browse files Browse the repository at this point in the history
  • Loading branch information
flisky authored and Yin Jifeng committed Apr 2, 2020
1 parent e16ff42 commit 65008ce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
7 changes: 3 additions & 4 deletions bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,13 @@ type Response struct {
type RemoveReason uint32

const (
_ RemoveReason = iota
// Expired means the key is past its LifeWindow.
Expired
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
8 changes: 7 additions & 1 deletion bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,14 +934,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)

// 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
8 changes: 3 additions & 5 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +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()
resp.EntryStatus = Expired
return nil, resp, nil
}
entry = readEntry(wrappedEntry)
s.lock.RUnlock()
s.hit(hashedKey)
return entry, resp, nil
}

Expand Down

0 comments on commit 65008ce

Please sign in to comment.