diff --git a/cache.go b/cache.go index 43f4e58..750f95e 100644 --- a/cache.go +++ b/cache.go @@ -74,8 +74,8 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) { // adds ~200 ns (as of go1.) c.mu.Unlock() - // try to call onEvicted if key existed before but it was expired before cleanup - if evicted && item.Expired() { + // try to call onEvicted if key existed before but the item is different + if evicted && item.Object != x { c.onEvicted(k, item.Object) } } diff --git a/cache_test.go b/cache_test.go index af7fff2..5dc61f9 100644 --- a/cache_test.go +++ b/cache_test.go @@ -1265,11 +1265,9 @@ func TestOnEvictedCalledBeforeSet(t *testing.T) { t.Fatal("tc.onEvicted is nil") } - // ensure item expires - time.Sleep(expiry) - - // calling Set again should evict expired item - tc.Set("foo", 3, DefaultExpiration) + // calling Set again with the same key should evict + // the item if different + tc.Set("foo", 5, DefaultExpiration) x, _ := tc.Get("bar") if !works {