Add GetPossiblyExpired()
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.
I'm not sold on this one, but not necessarily against it. Do you have an example of how this would be useful?
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.
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...)
@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.
Another possible implementation https://github.com/patrickmn/go-cache/pull/53
Found this fork: https://github.com/arp242/zcache