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

Update item with same expiry

Open pranjal5215 opened this issue 9 years ago • 3 comments

Hi patrickmn, We wanted to use in memory (fast) cache in golang to collect items from a running stream and use the item at the end of expiration as collected over time, approach we were using before was to create a cache and keep a channel ticking on time.After() on each key which went memory hungry, so using approach with go-cache where items expire after every X duration.

As such wanted to update item in cache with same expiry as original. So added function to do so ... would be happy to merge with upstream ...

pranjal5215 avatar Sep 15 '16 13:09 pranjal5215

I'm a little confused about this. Is the Update() method intended to keep the same expiration time as the original item? In that case, can't it just re-use the previous item's Expiration value?

patrickmn avatar Nov 25 '16 19:11 patrickmn

While setting item in cache set() in needed internally and Set() in called externally, both of these take duration field which is either DefaultExpiration or the passed value,

now here the expiry to be set is new expiry time which would be x =[time which was set earlier - now ] so set() needs to be called with that difference x,

Set() calculates expiry as e = time.Now().Add(d).UnixNano() As we don't store when was an item set, so difference would be required to be calculated before an update,

Hence the use case.

pranjal5215 avatar Nov 29 '16 18:11 pranjal5215

Found this fork: https://github.com/arp242/zcache

pablodz avatar Jan 03 '24 02:01 pablodz