matthewrudy / memoist

ActiveSupport::Memoizable with a few enhancements
MIT License
920 stars 99 forks source link

{Question} Time-based expiry mechanism #91

Closed nikola-maric closed 3 years ago

nikola-maric commented 3 years ago

Hi, I was wondering if you think this library would benefit from time-based cache invalidation mechanism expressed in seconds, something in lines of

    memoize :sleep, expire_after: 60

Basically, if expire_after key is found in method_names (as part of last Hash argument), there would be another variable to do a bookkeeping for, last time certain method was called (possibly with some arguments) and it's cache was skipped. When skipped, that time would be bumped to new Time.now value. That check be performed when resolving skip_cache variable, so that if last_access_time + expire_after <= Time.now skip_cache should be resolved to true, at least from that angle.

I could create PR + tests for that if you think its worthwhile, but preliminary benchmark results do show as much as 25-35% performance degradation (kinda expected, as there are some additional time-based checks). Maybe splitting implementation in memoize + timed_memoize/expirable_memoize could solve this and at least constrain performance degradation to methods memoized with this new approach (old methods would remain as-is).

I know this would increase code surface size or decrease perf (maybe there is a way to mitigate both 🤔 ), hence the question 😄

github0013 commented 3 years ago

You might wanna consider switching to this as this repo hasn't been maintained for a while. https://github.com/tycooon/memery

nikola-maric commented 3 years ago

@github0013 yeah, I guess you are right, thanks for pointing to memery gem, it has exactly what I need 💯