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

Add GetPossiblyExpired()

Open Freeaqingme opened this issue 7 years ago • 6 comments

In some situations it's okay to receive stale data. In fact, I have an application that depends on it. I wasn't able to access that data out of the box, and as such I added this function.

Freeaqingme avatar Feb 05 '17 23:02 Freeaqingme

I'm not sold on this one, but not necessarily against it. Do you have an example of how this would be useful?

patrickmn avatar Apr 19 '17 00:04 patrickmn

A use case I have is I am reading data from a database and caching it for x minutes. But if the database connection is lost for some reason, i'd love to keep reading stale data from the cache till the db comes back up online, rather than causing my application to return no data.

With Freeaqingme’s approach, the pattern would be GetFromCache, if expired GetFromDb, if error or could not make connection, GetExpiredData if present.

As an implementation option, one can also consider with the GetFromCache call, you could return the data and indicate in some way (can think of a couple) whether the data is stale, in which case one could replace it.

rahulraheja avatar Apr 30 '17 04:04 rahulraheja

I started off with something similar as @rahulraheja described. However, in my case, the cached data is certain not to change once it's been persisted. Although it is possible for something to be no longer useful. E.g., when a user is assigned to shard X, that will never change. But it's possible for that user to cancel his product, and then you'd want the item to eventually disappear from cache. So, if the item still appears in cache, I'm fine with using it. Even when it has expired.

I implemented that here: https://github.com/Freeaqingme/influxdb-relay/blob/master/relay/shardAssignmentStore.go#L69

(it's been a while since I implemented it. There may have been an ulterior grand design behind this, but reading the code I can't really figure out what that should have been...)

Freeaqingme avatar May 01 '17 16:05 Freeaqingme

@patrickmn Does this satisfy your question on use cases? I would love to get this feature in (to support graceful degradation) either with @Freeaqingme implementation recommendation or if you have any other recommendation to achieve the same effect.

rahulraheja avatar May 05 '17 22:05 rahulraheja

Another possible implementation https://github.com/patrickmn/go-cache/pull/53

rahulraheja avatar May 06 '17 01:05 rahulraheja

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

pablodz avatar Jan 03 '24 02:01 pablodz