Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove key from policy after TTL eviction #130

Merged
merged 3 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,47 @@ 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
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
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
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
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{})
Expand Down
4 changes: 3 additions & 1 deletion ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down