0

I add a value to couchbase cache with expiration 1 seconds, however it was expired in more than 20 seconds. What is cache expiration policy of membase?

Here is my code

    public string TestCache()
    {
        String spoon = null;
        using (var client = new CouchbaseClient())
        {

            spoon=client.Get<string>("Spoon");
            if(string.IsNullOrEmpty(spoon))
            {
                client.Store(StoreMode.Set,
                             "Spoon",
                             "Hello, Couchbase! Cache data is" + DateTime.Now.ToString(),
                             TimeSpan.FromSeconds(1));
            }

            spoon = client.Get<string>("Spoon");
        }

        return string.IsNullOrEmpty(spoon)
        ? "Can not get data from cache"
        : "Data from cache: " + spoon;
    }
user929794
  • 223
  • 1
  • 11

1 Answers1

0

First off, if you set something to expire in 1 second you will not be able to get it from Membase if you ask for it more than 1 second later. I would guess that the reason you are seeing the expiry happen later is that you are specifying an absolute time and the times on your server and client are different. If your sever is 20 seconds behind the client then I would expect to see this kind of behavior. Try just setting the expiry time to 1 and then you should see the right thing happen. For values less than 30 day the time is relative. For more than 30 days the time is absolute.

Inside Membase when an item is expired it isn't removed right away. There is an expiry task that runs by default every 1 hour and iterates over all of the keys and deletes expired ones. If you have a lot of keys expiring then you can set the expiry task to run more often. Another way items can expire is when the Membase cache becomes full and items are evicted from memory. Expired items can also be deleted by this task.

mikewied
  • 5,273
  • 1
  • 20
  • 32