From 91361a0082c6d6fe4e2ff65311b1726b9e61ae52 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 --- bigcache.go | 8 ++++++-- bigcache_test.go | 2 +- shard.go | 3 +-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/bigcache.go b/bigcache.go index bd205ced..cab82308 100644 --- a/bigcache.go +++ b/bigcache.go @@ -32,9 +32,9 @@ type Response struct { type RemoveReason uint32 const ( - // @TODO: Go defaults to 0 so in case we want to return EntryStatus back to the caller Expired cannot be differentiated + _ RemoveReason = iota // Expired means the key is past its LifeWindow. - 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 @@ -116,6 +116,10 @@ func (c *BigCache) Get(key string) ([]byte, error) { return shard.get(key, hashedKey) } +// GetWithInfo reads entry for the key. +// It returns an ErrEntryNotFound when +// no entry exists for the given key. +// It also returns Response with entry status information func (c *BigCache) GetWithInfo(key string) ([]byte, Response, error) { hashedKey := c.hash.Sum64(key) shard := c.getShard(hashedKey) diff --git a/bigcache_test.go b/bigcache_test.go index 4d5e2cbd..56e2918f 100644 --- a/bigcache_test.go +++ b/bigcache_test.go @@ -771,7 +771,7 @@ func TestBigCache_GetWithInfo(t *testing.T) { assert.Equal(t, Response{}, resp) clock.set(5) data, resp, err = cache.GetWithInfo(key) - assert.Equal(t, err, ErrEntryIsDead) + assert.Equal(t, err, nil) assert.Equal(t, Response{EntryStatus: Expired}, resp) assert.Nil(t, data) } diff --git a/shard.go b/shard.go index 36a6d9dc..7adeee59 100644 --- a/shard.go +++ b/shard.go @@ -42,9 +42,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 entry, resp, ErrEntryIsDead + return entry, resp, nil } entry := readEntry(wrappedEntry) s.lock.RUnlock() From c3845a81ec8faa50ad6f66d021edd8d5286fc829 Mon Sep 17 00:00:00 2001 From: jgheewala Date: Wed, 18 Nov 2020 09:49:27 -0800 Subject: [PATCH 2/2] Entry RemoveReason is iota based with default value being 0 Currently all const for RemovedReason are explicitly set to avoid any breaking changes. For v3 migrating the reasons to iota makes code readability and managing addition of more reasons easier in the future. Test: Validated all test cases run successfully. --- bigcache.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bigcache.go b/bigcache.go index 37a793d7..0adefed4 100644 --- a/bigcache.go +++ b/bigcache.go @@ -34,12 +34,12 @@ type RemoveReason uint32 const ( _ RemoveReason = iota // Expired means the key is past its LifeWindow. - Expired = RemoveReason(1) + 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 = RemoveReason(2) + NoSpace // Deleted means Delete was called and this key was removed as a result. - Deleted = RemoveReason(3) + Deleted ) // NewBigCache initialize new instance of BigCache