Skip to content

Cleanup timer not updated on Set() of existing key #204

@gaalmeida

Description

@gaalmeida

Seems to be an edge case, but if the key with TTL closest to eviction is updated with a shorter expiration, the cleanup timer is not updated, causing it to be evicted only at its old expiration. Here's a code snippet that exemplifies this behavior:

now := time.Now()
cache := ttlcache.New[string, int]()
cache.OnEviction(func(ctx context.Context, reason ttlcache.EvictionReason, item *ttlcache.Item[string, int]) {
	if reason == ttlcache.EvictionReasonExpired {
		fmt.Printf("key: %s, value: %d, expired after: %v\n", item.Key(), item.Value(), time.Since(now))
	}
})
go cache.Start()

cache.Set("key1", 1, 10*time.Second)
cache.Set("key1", 2, time.Second)

When running it locally, It yielded this output:

key: key1, value: 2, expired after: 10.001017375s

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions