Repository: allegro/bigcache
Pull request #216 (2020-04-01 14:14:42)

give a way to access the stale cache
an update for #181 :)

codecov-io: # [Codecov](https://codecov.io/gh/allegro/bigcache/pull/216?src=pr&el=h1) Report
> Merging [#216](https://codecov.io/gh/allegro/bigcache/pull/216?src=pr&el=desc) into [master](https://codecov.io/gh/allegro/bigcache/commit/37b9eb28693b36c88824afefa1dfe1a20826e74b&el=desc) will **decrease** coverage by `0.02%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/allegro/bigcache/pull/216/graphs/tree.svg?width=650&height=150&src=pr&token=NRx0gXD6he)](https://codecov.io/gh/allegro/bigcache/pull/216?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #216      +/-   ##
==========================================
- Coverage   90.61%   90.58%   -0.03%     
==========================================
  Files          15       15              
  Lines         714      712       -2     
==========================================
- Hits          647      645       -2     
  Misses         58       58              
  Partials        9        9              
```


| [Impacted Files](https://codecov.io/gh/allegro/bigcache/pull/216?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [bigcache.go](https://codecov.io/gh/allegro/bigcache/pull/216/diff?src=pr&el=tree#diff-YmlnY2FjaGUuZ28=) | `99.10% <ø> (ø)` | |
| [shard.go](https://codecov.io/gh/allegro/bigcache/pull/216/diff?src=pr&el=tree#diff-c2hhcmQuZ28=) | `91.22% <100.00%> (-0.08%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/allegro/bigcache/pull/216?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/allegro/bigcache/pull/216?src=pr&el=footer). Last update [37b9eb2...2adb804](https://codecov.io/gh/allegro/bigcache/pull/216?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

mxplusb: @cristaloleg this looks good to me. I think it would be good to version bump after merging this because it sets an explicit value, so it's technically a minor API change.

@flisky can you please rebase and then re-request a review?

Patch:
diff --git a/bigcache.go b/bigcache.go
index 9589b7b..d5de814 100644
--- a/bigcache.go
+++ b/bigcache.go
@@ -33,13 +33,12 @@ type RemoveReason uint32
 
 const (
 	// 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 = 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/entry_not_found_error.go b/entry_not_found_error.go
index 4a5b5f9..8993384 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 5256302..a3c8c09 100644
--- a/shard.go
+++ b/shard.go
@@ -49,16 +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()
-		// @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
 	}
-	entry = readEntry(wrappedEntry)
-	s.lock.RUnlock()
-	s.hit(hashedKey)
 	return entry, resp, nil
 }
 


Test patch:
diff --git a/bigcache_test.go b/bigcache_test.go
index 183614d..4bf28a5 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)
-	assertEqual(t, err, ErrEntryIsDead)
+
+	// then
+	assertEqual(t, err, nil)
 	assertEqual(t, Response{EntryStatus: Expired}, resp)
-	assertEqual(t, []byte(nil), data)
+	assertEqual(t, []byte(value), data)
 }
 
 type mockedLogger struct {