Search code examples
rubymemcacheddalli

How to automatically delete a memcache after some time in Dalli


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.


Solution

  • 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.