muesli / cache2go

Concurrency-safe Go caching library with expiration capabilities and access counters

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request] Change item lifeSpan

GalileoCap opened this issue · comments

I'd like to be able to change an item's life span after adding it.
In my case I want to have it so:

  1. An user interacts with my application
  2. I look for them in the cache, if they're not present I get them from a database.
  3. Make sure they stay in memory while I need them (which can take an arbitrary amount of time)
  4. ... (my program)
  5. After I'm done, set it so they expire in 60 seconds unless the user interacts (which would take us back to the first step).

Right now this can be done by:

  1. Looking for them in the cache. And if they're not present I get them BUT save it outside the cache.
  2. ... (my program)
  3. After I'm done, If they were in the cache and still are, remove them.
  4. Add them to the cache with a life span of 60 seconds.

This adds a lot of complexity which would be solved by being able to change the items life span (first to 0 and then to 60).

Issue implementing it

I tried implementing it by adding a method to CacheItem (you can see it here), but came across an issue if you:

  1. Create an item with a life span
  2. Set it to 0
  3. Wait until the next expirationCheck (here it stops running because there's no items with lifespan != 0)
  4. Set it to any value different than 0
  5. Now it will never expire because the expirationCheck stopped running

This issue is the reason the test currently doesn't pass.
If I run table.expirationCheck() after resetting the life span it works as expected, but currently there's no way to do it from the method.

It has come to my attention that this is already possible by re-adding the item to the cache.