diff --git a/cache.go b/cache.go index 19297a0..ab11cb4 100644 --- a/cache.go +++ b/cache.go @@ -116,7 +116,7 @@ func (c *Cache[K, V]) cleaner() { // remove all expired entries for c.chain != nil && c.chain.HasExpired() { - c.items.Delete(c.chain.data) + c.items.CompareAndDelete(c.chain.data, c.chain.item) c.chain = c.chain.next } } diff --git a/cache_test.go b/cache_test.go index 4793d4b..0d8ee23 100644 --- a/cache_test.go +++ b/cache_test.go @@ -132,3 +132,23 @@ func TestCache_Cleaner(t *testing.T) { c.sched.Wait() assert.Nil(t, c.chain) } + +func TestCache_UpdateExpiry(t *testing.T) { + timeNow = func() time.Time { return time.Now() } + + c := New[string, string]() + c.Set("a", "b", time.Now().Add(2*time.Second)) + + time.Sleep(time.Second) + get, b := c.Get("a") + assert.True(t, b) + assert.Equal(t, "b", get) + + c.Set("a", "b", time.Now().Add(5*time.Second)) + + // after expiry of the first set call + time.Sleep(2 * time.Second) + get, b = c.Get("a") + assert.True(t, b) + assert.Equal(t, "b", get) +}