Skip to content

Commit 59ed52b

Browse files
author
陈卓涵
committed
fix: expired
1 parent 0b306aa commit 59ed52b

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

bigcache_test.go

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,21 @@ func TestTimingEviction(t *testing.T) {
219219
MaxEntrySize: 256,
220220
}, &clock)
221221

222-
// when
223222
cache.Set("key", []byte("value"))
224-
clock.set(5)
223+
224+
// when
225+
clock.set(1)
225226
cache.Set("key2", []byte("value2"))
226227
_, err := cache.Get("key")
227228

229+
// then
230+
noError(t, err)
231+
232+
// when
233+
clock.set(5)
234+
cache.Set("key2", []byte("value2"))
235+
_, err = cache.Get("key")
236+
228237
// then
229238
assertEqual(t, ErrEntryNotFound, err)
230239
}
@@ -1066,22 +1075,40 @@ func TestBigCache_GetWithInfo(t *testing.T) {
10661075
value := "100"
10671076
cache.Set(key, []byte(value))
10681077

1069-
// when
1070-
data, resp, err := cache.GetWithInfo(key)
1071-
1072-
// then
1073-
assertEqual(t, []byte(value), data)
1074-
noError(t, err)
1075-
assertEqual(t, Response{}, resp)
1076-
1077-
// when
1078-
clock.set(5)
1079-
data, resp, err = cache.GetWithInfo(key)
1078+
for _, tc := range []struct {
1079+
name string
1080+
clock int64
1081+
wantData string
1082+
wantResp Response
1083+
}{
1084+
{
1085+
name: "zero",
1086+
clock: 0,
1087+
wantData: value,
1088+
wantResp: Response{},
1089+
},
1090+
{
1091+
name: "Before Expired",
1092+
clock: 4,
1093+
wantData: value,
1094+
wantResp: Response{},
1095+
},
1096+
{
1097+
name: "Expired",
1098+
clock: 5,
1099+
wantData: value,
1100+
wantResp: Response{},
1101+
},
1102+
} {
1103+
t.Run(tc.name, func(t *testing.T) {
1104+
clock.set(tc.clock)
1105+
data, resp, err := cache.GetWithInfo(key)
10801106

1081-
// then
1082-
assertEqual(t, err, nil)
1083-
assertEqual(t, Response{EntryStatus: Expired}, resp)
1084-
assertEqual(t, []byte(value), data)
1107+
assertEqual(t, []byte(tc.wantData), data)
1108+
noError(t, err)
1109+
assertEqual(t, tc.wantResp, resp)
1110+
})
1111+
}
10851112
}
10861113

10871114
type mockedLogger struct {

shard.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ func (s *cacheShard) getWithInfo(key string, hashedKey uint64) (entry []byte, re
5050
}
5151

5252
entry = readEntry(wrappedEntry)
53-
oldestTimeStamp := readTimestampFromEntry(wrappedEntry)
54-
s.lock.RUnlock()
55-
s.hit(hashedKey)
56-
if currentTime-oldestTimeStamp >= s.lifeWindow {
53+
if s.isExpired(wrappedEntry, currentTime) {
5754
resp.EntryStatus = Expired
5855
}
56+
s.lock.RUnlock()
57+
s.hit(hashedKey)
5958
return entry, resp, nil
6059
}
6160

@@ -268,17 +267,21 @@ func (s *cacheShard) del(hashedKey uint64) error {
268267
}
269268

270269
func (s *cacheShard) onEvict(oldestEntry []byte, currentTimestamp uint64, evict func(reason RemoveReason) error) bool {
271-
oldestTimestamp := readTimestampFromEntry(oldestEntry)
272-
if currentTimestamp < oldestTimestamp {
273-
return false
274-
}
275-
if currentTimestamp-oldestTimestamp > s.lifeWindow {
270+
if s.isExpired(oldestEntry, currentTimestamp) {
276271
evict(Expired)
277272
return true
278273
}
279274
return false
280275
}
281276

277+
func (s *cacheShard) isExpired(oldestEntry []byte, currentTimestamp uint64) bool {
278+
oldestTimestamp := readTimestampFromEntry(oldestEntry)
279+
if currentTimestamp <= oldestTimestamp { // if currentTimestamp < oldestTimestamp, the result will out of uint64 limits;
280+
return false
281+
}
282+
return currentTimestamp-oldestTimestamp > s.lifeWindow
283+
}
284+
282285
func (s *cacheShard) cleanUp(currentTimestamp uint64) {
283286
s.lock.Lock()
284287
for {

0 commit comments

Comments
 (0)