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

Fix update of expired items #124

Merged
merged 2 commits into from
Jun 11, 2024
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
12 changes: 6 additions & 6 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
ttl = c.options.ttl
}

elem := c.get(key, false)
elem := c.get(key, false, true)
if elem != nil {
// update/overwrite an existing item
item := elem.Value.(*Item[K, V])
Expand Down Expand Up @@ -176,14 +176,14 @@ func (c *Cache[K, V]) set(key K, value V, ttl time.Duration) *Item[K, V] {
// It returns nil if the item is not found or is expired.
// Not safe for concurrent use by multiple goroutines without additional
// locking.
func (c *Cache[K, V]) get(key K, touch bool) *list.Element {
func (c *Cache[K, V]) get(key K, touch bool, includeExpired bool) *list.Element {
elem := c.items.values[key]
if elem == nil {
return nil
}

item := elem.Value.(*Item[K, V])
if item.isExpiredUnsafe() {
if !includeExpired && item.isExpiredUnsafe() {
return nil
}

Expand Down Expand Up @@ -218,7 +218,7 @@ func (c *Cache[K, V]) getWithOpts(key K, lockAndLoad bool, opts ...Option[K, V])
c.items.mu.Lock()
}

elem := c.get(key, !getOpts.disableTouchOnHit)
elem := c.get(key, !getOpts.disableTouchOnHit, false)

if lockAndLoad {
c.items.mu.Unlock()
Expand Down Expand Up @@ -436,7 +436,7 @@ func (c *Cache[K, V]) DeleteExpired() {
// If the item is not found, the method is no-op.
func (c *Cache[K, V]) Touch(key K) {
c.items.mu.Lock()
c.get(key, true)
c.get(key, true, false)
c.items.mu.Unlock()
}

Expand Down Expand Up @@ -469,7 +469,7 @@ func (c *Cache[K, V]) Items() map[K]*Item[K, V] {

items := make(map[K]*Item[K, V], len(c.items.values))
for k := range c.items.values {
item := c.get(k, false)
item := c.get(k, false, false)
if item != nil {
items[k] = item.Value.(*Item[K, V])
}
Expand Down
32 changes: 31 additions & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,36 @@ func Test_Cache_set(t *testing.T) {
}
})
}

// finally, test proper expiration queue handling on expired item update.
// recreate situation when expired item gets updated
// and not auto-cleaned up yet.
c := New[string, struct{}](
WithDisableTouchOnHit[string,struct{}](),
)

// insert an item and let it expire
c.Set("test", struct{}{}, 1)
time.Sleep(50*time.Millisecond)

// update the expired item
updatedItem := c.Set("test", struct{}{}, 100*time.Millisecond)

// eviction should not happen as we prolonged element
cl := c.OnEviction(func(_ context.Context, _ EvictionReason, item *Item[string, struct{}]){
t.Errorf("eviction happened even though item has not expired yet: key=%s, evicted item expiresAt=%s, updated item expiresAt=%s, now=%s",
item.Key(),
item.ExpiresAt().String(),
updatedItem.ExpiresAt().String(),
time.Now().String())
})
// start of automatic cleanup process is delayed to allow update win the race
// and update expired before its removal
go c.Start()

time.Sleep(90*time.Millisecond)
cl()
c.Stop()
}

func Test_Cache_get(t *testing.T) {
Expand Down Expand Up @@ -374,7 +404,7 @@ func Test_Cache_get(t *testing.T) {
oldItem.ttl = 0
}

elem := cache.get(c.Key, c.Touch)
elem := cache.get(c.Key, c.Touch, false)

if c.Key == notFoundKey {
assert.Nil(t, elem)
Expand Down
Loading