Skip to content

Commit ccdbc60

Browse files
fliskyjgheewala
andauthored
give a way to access the stale cache (#216)
* 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 <jgheewala@signalfx.com>
1 parent d572104 commit ccdbc60

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

bigcache.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ type RemoveReason uint32
3333

3434
const (
3535
// Expired means the key is past its LifeWindow.
36-
// @TODO: Go defaults to 0 so in case we want to return EntryStatus back to the caller Expired cannot be differentiated
37-
Expired RemoveReason = iota
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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,14 +995,20 @@ func TestBigCache_GetWithInfo(t *testing.T) {
995995

996996
// when
997997
data, resp, err := cache.GetWithInfo(key)
998+
999+
// then
9981000
assertEqual(t, []byte(value), data)
9991001
noError(t, err)
10001002
assertEqual(t, Response{}, resp)
1003+
1004+
// when
10011005
clock.set(5)
10021006
data, resp, err = cache.GetWithInfo(key)
1003-
assertEqual(t, err, ErrEntryIsDead)
1007+
1008+
// then
1009+
assertEqual(t, err, nil)
10041010
assertEqual(t, Response{EntryStatus: Expired}, resp)
1005-
assertEqual(t, []byte(nil), data)
1011+
assertEqual(t, []byte(value), data)
10061012
}
10071013

10081014
type mockedLogger struct {

entry_not_found_error.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ import "errors"
55
var (
66
// ErrEntryNotFound is an error type struct which is returned when entry was not found for provided key
77
ErrEntryNotFound = errors.New("Entry not found")
8-
// ErrEntryIsDead is an error type struct which is returned when entry has past it's life window
9-
ErrEntryIsDead = errors.New("entry is dead")
108
)

shard.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +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()
55-
// @TODO: when Expired is non-default value return err as nil as the resp will have proper entry status
5657
resp.EntryStatus = Expired
57-
return nil, resp, ErrEntryIsDead
5858
}
59-
entry = readEntry(wrappedEntry)
60-
s.lock.RUnlock()
61-
s.hit(hashedKey)
6259
return entry, resp, nil
6360
}
6461

0 commit comments

Comments
 (0)