Fix eviction check

This commit is contained in:
Jaime Martinez 2020-10-16 16:55:06 +11:00
parent 38560f59f4
commit 9b97ae73e4
No known key found for this signature in database
GPG Key ID: 7A88010914F01A73
2 changed files with 5 additions and 7 deletions

View File

@ -74,8 +74,8 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
// adds ~200 ns (as of go1.) // adds ~200 ns (as of go1.)
c.mu.Unlock() c.mu.Unlock()
// try to call onEvicted if key existed before but it was expired before cleanup // try to call onEvicted if key existed before but the item is different
if evicted && item.Expired() { if evicted && item.Object != x {
c.onEvicted(k, item.Object) c.onEvicted(k, item.Object)
} }
} }

View File

@ -1265,11 +1265,9 @@ func TestOnEvictedCalledBeforeSet(t *testing.T) {
t.Fatal("tc.onEvicted is nil") t.Fatal("tc.onEvicted is nil")
} }
// ensure item expires // calling Set again with the same key should evict
time.Sleep(expiry) // the item if different
tc.Set("foo", 5, DefaultExpiration)
// calling Set again should evict expired item
tc.Set("foo", 3, DefaultExpiration)
x, _ := tc.Get("bar") x, _ := tc.Get("bar")
if !works { if !works {