Skip to content

Commit 65008ce

Browse files
fliskyYin Jifeng
authored and
Yin Jifeng
committed
give a way to access the stale cache
1 parent e16ff42 commit 65008ce

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

bigcache.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ type Response struct {
3232
type RemoveReason uint32
3333

3434
const (
35-
_ RemoveReason = iota
3635
// Expired means the key is past its LifeWindow.
37-
Expired
36+
Expired = RemoveReason(1)
3837
// NoSpace means the key is the oldest and the cache size was at its maximum when Set was called, or the
3938
// entry exceeded the maximum shard size.
40-
NoSpace
39+
NoSpace = RemoveReason(2)
4140
// Deleted means Delete was called and this key was removed as a result.
42-
Deleted
41+
Deleted = RemoveReason(3)
4342
)
4443

4544
// NewBigCache initialize new instance of BigCache

bigcache_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,14 +934,20 @@ func TestBigCache_GetWithInfo(t *testing.T) {
934934

935935
// when
936936
data, resp, err := cache.GetWithInfo(key)
937+
938+
// then
937939
assertEqual(t, []byte(value), data)
938940
noError(t, err)
939941
assertEqual(t, Response{}, resp)
942+
943+
// when
940944
clock.set(5)
941945
data, resp, err = cache.GetWithInfo(key)
946+
947+
// then
942948
assertEqual(t, err, nil)
943949
assertEqual(t, Response{EntryStatus: Expired}, resp)
944-
assertEqual(t, []byte(nil), data)
950+
assertEqual(t, []byte(value), data)
945951
}
946952

947953
type mockedLogger struct {

shard.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,13 @@ func (s *cacheShard) getWithInfo(key string, hashedKey uint64) (entry []byte, re
4949
return nil, resp, ErrEntryNotFound
5050
}
5151

52+
entry = readEntry(wrappedEntry)
5253
oldestTimeStamp := readTimestampFromEntry(wrappedEntry)
54+
s.lock.RUnlock()
55+
s.hit(hashedKey)
5356
if currentTime-oldestTimeStamp >= s.lifeWindow {
54-
s.lock.RUnlock()
5557
resp.EntryStatus = Expired
56-
return nil, resp, nil
5758
}
58-
entry = readEntry(wrappedEntry)
59-
s.lock.RUnlock()
60-
s.hit(hashedKey)
6159
return entry, resp, nil
6260
}
6361

0 commit comments

Comments
 (0)