From 6cdae77e76d5aebc546ec44a1323fe53e87bd66f Mon Sep 17 00:00:00 2001 From: Laurent Dechoux Date: Thu, 30 Jan 2020 14:19:58 +0100 Subject: [PATCH 1/3] Add recache test for setWithTTL --- cache_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/cache_test.go b/cache_test.go index 43ad9615..c9f5208b 100644 --- a/cache_test.go +++ b/cache_test.go @@ -330,6 +330,45 @@ func TestCacheSet(t *testing.T) { require.False(t, c.Set(1, 1, 1)) } +func TestRecacheWithTTL(t *testing.T) { + c, err := NewCache(&Config{ + NumCounters: 100, + MaxCost: 10, + BufferItems: 64, + Metrics: true, + }) + + require.NoError(t, err) + + // Set initial value for key = 1 + c.SetWithTTL(1, 1, 1, 5*time.Second) + time.Sleep(2 * time.Second) + + // Get value from cache for key = 1 + val, ok := c.Get(1) + require.True(t, ok) + require.NotNil(t, val) + require.Equal(t, 1, val) + + // Wait for expiration + time.Sleep(5 * time.Second) + + // The cached value for key = 1 should be gone + val, ok = c.Get(1) + require.False(t, ok) + require.Nil(t, val) + + // Set new value for key = 1 + c.SetWithTTL(1, 2, 1, 5*time.Second) + time.Sleep(2 * time.Second) + + // Get value from cache for key = 1 + val, ok = c.Get(1) + require.True(t, ok) + require.NotNil(t, val) + require.Equal(t, 2, val) +} + func TestCacheSetWithTTL(t *testing.T) { m := &sync.Mutex{} evicted := make(map[uint64]struct{}) From baf81542a38f664b5e1ef624b555e8f52cf2f5be Mon Sep 17 00:00:00 2001 From: Laurent Dechoux Date: Thu, 30 Jan 2020 14:27:08 +0100 Subject: [PATCH 2/3] Control insert in recache test --- cache_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cache_test.go b/cache_test.go index c9f5208b..c1dc7456 100644 --- a/cache_test.go +++ b/cache_test.go @@ -341,7 +341,8 @@ func TestRecacheWithTTL(t *testing.T) { require.NoError(t, err) // Set initial value for key = 1 - c.SetWithTTL(1, 1, 1, 5*time.Second) + insert := c.SetWithTTL(1, 1, 1, 5*time.Second) + require.True(t, insert) time.Sleep(2 * time.Second) // Get value from cache for key = 1 @@ -359,7 +360,8 @@ func TestRecacheWithTTL(t *testing.T) { require.Nil(t, val) // Set new value for key = 1 - c.SetWithTTL(1, 2, 1, 5*time.Second) + insert = c.SetWithTTL(1, 2, 1, 5*time.Second) + require.True(t, insert) time.Sleep(2 * time.Second) // Get value from cache for key = 1 From d7b4f0c83f08164c5fb14c21ca8548d5df5cd102 Mon Sep 17 00:00:00 2001 From: Laurent Dechoux Date: Thu, 30 Jan 2020 15:13:13 +0100 Subject: [PATCH 3/3] remove key from store and policy after key expiration --- ttl.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ttl.go b/ttl.go index 02864f7e..346d2f20 100644 --- a/ttl.go +++ b/ttl.go @@ -132,8 +132,10 @@ func (m *expirationMap) cleanup(store store, policy policy, onEvict onEvictFunc) continue } - _, value := store.Del(key, conflict) cost := policy.Cost(key) + policy.Del(key) + _, value := store.Del(key, conflict) + if onEvict != nil { onEvict(key, conflict, value, cost) }