go-cache icon indicating copy to clipboard operation
go-cache copied to clipboard

Delete expired items before setting

Open jaimem88 opened this issue 4 years ago • 5 comments

Check if an item exists and it has expired before setting so that onEvicted is actually called.

Fixes #48.

jaimem88 avatar Oct 13 '20 00:10 jaimem88

@patrickmn for your review 🙏

jaimem88 avatar Oct 13 '20 00:10 jaimem88

No. Always.

We overwrite an existing item. It should be comparison tha the item that we overwrite is a different one. So, this comparison is deliberate. Usage of expired produces wrong behavior.

On Thu, 15 Oct 2020 at 01:58, Jaime Martinez [email protected] wrote:

@jaimem88 commented on this pull request.

In cache.go https://github.com/patrickmn/go-cache/pull/132#discussion_r505085507:

@@ -57,6 +57,10 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) { if d > 0 { e = time.Now().Add(d).UnixNano() }

  • // delete before setting and call onEvicted
  • c.deleteIfExpired(k)

updated :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/patrickmn/go-cache/pull/132#discussion_r505085507, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASOSQK3G7GDYXNVFTMGGCLSKY3KJANCNFSM4SNYEWEA .

ayufan avatar Oct 15 '20 21:10 ayufan

@patrickmn Would you mind looking at this?

ayufan avatar Oct 20 '20 09:10 ayufan

Note this is a incompatible change, and some people may be relying on the current behaviour.

Also this library doesn't seem maintained at the moment, so little chance it will get merged. It's better to just maintain your own fork if you need this behaviour.

arp242 avatar Oct 22 '20 03:10 arp242

Trying to find a cache with TTL that has been rigorously tested and this seems to be a popular choice. However, looks like repo is not maintained and hasn't been released since 2017. @koalalorenzo Where you able to find any other forks reliable?

ViswanathB avatar Feb 09 '21 23:02 ViswanathB