jellydator / ttlcache

An in-memory cache with item expiration and generics
MIT License
883 stars 115 forks source link

Set without expanding the Expire time #115

Closed R4wm closed 4 months ago

R4wm commented 8 months ago

Its not clear to me how to ".Set()" and not start the expire time over again. Can someone point to reference or doc or advise ?

davseby commented 8 months ago

I think it's not possible. As a workaround, you could get the item and reset it with the modified ttl value.

var ttl time.Duration = cache.DefaultTTL

if previousItem := cache.Get(key); previousItem != nil {
      ttl = previousItem.ExpiresAt().Sub(time.Now())
}

cache.Set(key, value, ttl)
swithek commented 8 months ago

At present not updating an existing expiration time isn't possible, though it might a good feature for one of the future versions. We could add a new ttlcache.PreviousTTL option that would indicate that the previous TTL should be set after an update.

swithek commented 4 months ago

Implemented in #121