golang-lru
golang-lru copied to clipboard
Addition of AddWithTtl to Expirable cache
I'd like to propose adding the ability to have a per-key TTL to the Expirable implementation. This would accept a time.Duration
and set the ExpiresAt
of the LRU entry to the passed duration. Passing a value <= 0 would result in turning expiring off, as is the case when setting the TTL for a new Expirable cache.
// AddWithTtl creates an entry with the provided TTL, overriding the cache-level TTL
func (c *LRU[K, V]) AddWithTtl(key K, value V, t time.Duration) (evicted bool)
The existing Add
method would remain unchanged and would use the cache's TTL value.
I'm opening this issue to start a discussion about this design and will open a PR after discussion- tagging @mgaffney @jefferai @paskal for feedback.
I am not sure it's comparable with the current cleanup mechanism. Will new method work with it or you would need some other cleanup?