From 0520cac294df1abbf913c2e185cd090b75627e58 Mon Sep 17 00:00:00 2001 From: Jay Gheewala Date: Tue, 8 Oct 2019 21:41:49 -0700 Subject: [PATCH 1/2] 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 --- bigcache.go | 4 ++-- bigcache_test.go | 2 +- entry_not_found_error.go | 2 -- shard.go | 3 +-- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/bigcache.go b/bigcache.go index 9589b7bd..d6b00f66 100644 --- a/bigcache.go +++ b/bigcache.go @@ -32,9 +32,9 @@ type Response struct { type RemoveReason uint32 const ( + _ RemoveReason = iota // 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 // 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 diff --git a/bigcache_test.go b/bigcache_test.go index 183614db..65c2eb15 100644 --- a/bigcache_test.go +++ b/bigcache_test.go @@ -1000,7 +1000,7 @@ func TestBigCache_GetWithInfo(t *testing.T) { assertEqual(t, Response{}, resp) clock.set(5) data, resp, err = cache.GetWithInfo(key) - assertEqual(t, err, ErrEntryIsDead) + assertEqual(t, err, nil) assertEqual(t, Response{EntryStatus: Expired}, resp) assertEqual(t, []byte(nil), data) } diff --git a/entry_not_found_error.go b/entry_not_found_error.go index 4a5b5f9b..8993384d 100644 --- a/entry_not_found_error.go +++ b/entry_not_found_error.go @@ -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") ) diff --git a/shard.go b/shard.go index 52563029..2d6ba621 100644 --- a/shard.go +++ b/shard.go @@ -52,9 +52,8 @@ func (s *cacheShard) getWithInfo(key string, hashedKey uint64) (entry []byte, re oldestTimeStamp := readTimestampFromEntry(wrappedEntry) 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 + return nil, resp, nil } entry = readEntry(wrappedEntry) s.lock.RUnlock() From 3da1e578270eb040a4e30ed5e476eeda94b9d358 Mon Sep 17 00:00:00 2001 From: flisky Date: Wed, 1 Apr 2020 22:07:04 +0800 Subject: [PATCH 2/2] give a way to access the stale cache --- bigcache.go | 7 +++---- bigcache_test.go | 8 +++++++- shard.go | 8 +++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/bigcache.go b/bigcache.go index d6b00f66..d5de8144 100644 --- a/bigcache.go +++ b/bigcache.go @@ -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 diff --git a/bigcache_test.go b/bigcache_test.go index 65c2eb15..4bf28a5d 100644 --- a/bigcache_test.go +++ b/bigcache_test.go @@ -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) + + // then assertEqual(t, err, nil) assertEqual(t, Response{EntryStatus: Expired}, resp) - assertEqual(t, []byte(nil), data) + assertEqual(t, []byte(value), data) } type mockedLogger struct { diff --git a/shard.go b/shard.go index 2d6ba621..a3c8c097 100644 --- a/shard.go +++ b/shard.go @@ -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 }