richfitz / storr

:package: Object cacher for R
http://richfitz.github.io/storr
Other
116 stars 10 forks source link

Support expiring keys #5

Open richfitz opened 9 years ago

richfitz commented 9 years ago

Redis does lazy key expires (i.e., check for expiry time on read). That could be done as part of the key -> hash lookup, returning NULL if the key has expired. It'll add a little extra work, but be nice for doing the "local cache" pattern, especially when using the external driver.

Might be best to make this optional at the storr level (like key mangling and default namespace) but not sure.

richfitz commented 8 years ago

Implementing this efficiently will require the "traits" support I need elsewhere, so this is probably going to be a "version 2" thing.

Drivers need to declare if they support ttl natively (e.g., Redis and rrlite do, environment, rds, Berkeley db don't). If they do we can pass ttl directly in to set (on the key not the data) and forget about it. If they don't and if ttl is given then we'd need to store time information in another tree (ttl/<namespace>/<key>) as (say) c(Sys.time(), ttl, Sys.time() + ttl).

Alternatively, the driver could be free to implement this; set_hash gains a ttl argument with default NULL and the driver may ignore it. Then the driver is responsible for the entire "has the key expired / return the key" thing.

daattali commented 8 years ago

Came here to request a ttl feature. Glad to see you've already started thinking about it.

richfitz commented 8 years ago

If you have any thoughts about how to implement this efficiently I'd appreciate it. I really feel like this one should not be run on every key but having the facility to turn it on could support a number of use-cases. The most obvious way to do this is via an underlying db that supports ttl (e.g., Redis above) but it wold be nice to have another way of making this work.

daattali commented 8 years ago

Haven't thought about implementation at all, sorry. As we all know, there are only two things that are difficult in computer science, and you've already tackled the first one, so I expect you'll be able to figure this one out in a proper way too... :)

But yes I agree it would be off for most people and only turned on as optional. In my case, I want to download a file from the internet once every 24 hours, but also allow the user to invalidate the saved object and download it right now if they so desire.

I'll let you know if I give this another thought, but I probably won't. Good luck!

bedantaguru commented 6 years ago

I had similar idea (in different context altogether) (https://github.com/MadeInR/obsoleteValue). It would be nice if we can make value of a specific key become obsolete under predefined condition. One specific case will be time dependent. If desired I can try my best to contribute to this idea.