How to automatically delete a memcache after some time in Dalli

1.4k views Asked by At

I'm not sure if this can be done at all.

I'm trying to set Dalli to delete a memcache after 3 seconds (just to experiment)


    dalli = Dalli::Client.new
    dalli.add("test1","value", 3)
    dalli.get("test1").should eql "value"

    sleep(10)
    dalli.get("test1").should eql nil


In the code, I have set TTL for 3 seconds, and then I expect that after 3 seconds the "test1" would be deleted but apparently not. So, the test fails in the second assertion. How can I ask Dalli to expire a key/value after a certain amount of time?

Thanks a lot.

2

There are 2 answers

0
Sneal On BEST ANSWER

You can only explicitly remove a key by calling delete or implicitly via TTL.

Does this happen with a different key besides 'test1'? Try changing your 'add' command to a 'set' command. The add command is conditional, it only sets the value if doesn't already exist. Is it possible you already set that key previously (although unlikely from the code you provided) without specifying a TTL? If you didn't specify a TTL the item is cached indefinitely.

0
tibbon On

Put the following in your session_store.rb initializer

Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 20.minutes

That will expire the cache after 20 minutes.