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 1, 2020
1 parent e16ff42 commit 2adb804
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
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)

// then
data, resp, err = cache.GetWithInfo(key)
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 2adb804

Please sign in to comment.