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

Reset item expiry upon access / LRU option #4

Open
weeco opened this issue Sep 25, 2024 · 0 comments
Open

Reset item expiry upon access / LRU option #4

weeco opened this issue Sep 25, 2024 · 0 comments

Comments

@weeco
Copy link

weeco commented Sep 25, 2024

Description:

I'm currently using the twmb/go-cache library to cache Kafka clients with the following settings:

cacheSettings := []cache.Opt{
    cache.MaxAge(30 * time.Second),
    cache.MaxErrorAge(time.Second),
    cache.AutoCleanInterval(15 * time.Second),
}

return &KafkaClientProvider{
    clientCache: cache.New[string, *kgo.Client](cacheSettings...),
}

The cache works well, but I'm missing a feature: the ability to reset the expiry timer (in my case, MaxAge of 30 seconds) whenever an entry is retrieved. The current behavior is that entries become stale (eligible for cleanup/eviction) based on the initial time they were set, regardless of how often they're accessed.

Problem:

In my use case, I want the cached Kafka clients to only be evicted if they haven't been accessed for the duration specified by MaxAge. The current setup evicts items based on their initial creation time, which means even frequently accessed items are evicted after 30 seconds.

Proposal:

Introduce a new cache option, which can be added to reset the timer for an entry upon access/retrieval. This would basically allow a LRU cache. Usage could look like this:

  // A configurable function where users specify what the new maxAge or maxErrorAge should be.
  cache.RefreshExpiryOnAccess(maxAge, maxErrorAge time.Duration)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant