Store into memcached bucket with expiration

Our project is running on a 2.2.0 Couchbase server, and we are facing a really interesting problem. If we want to store a document in a memcached bucket with expiration date, 59 seconds is working OK, 60 seconds will never expire, 61 seconds will expire after 1 second. We tried with 1.3.6 client version.

I wrote a test application for this:

var client = new CouchbaseClient();

client.ExecuteRemove(“test1234”);
client.ExecuteRemove(“test12345”);
client.ExecuteRemove(“test123456”);

client.ExecuteStore(StoreMode.Set, “test1234”, “blabababla”, new TimeSpan(0, 0, 59));
client.ExecuteStore(StoreMode.Set, “test12345”, “blababablaa”, new TimeSpan(0, 0, 60));
client.ExecuteStore(StoreMode.Set, “test123456”, “blababablaaa”, new TimeSpan(0, 0, 61));

Console.WriteLine(“Reading…”);

for (int i = 0; i < 100; i++)
{
Console.WriteLine(string.Format(“read {0}: {1}”, i, client.ExecuteGet(“test1234”).Value));
Console.WriteLine(string.Format(“read {0}: {1}”, i, client.ExecuteGet(“test12345”).Value));
Console.WriteLine(string.Format(“read {0}: {1}”, i, client.ExecuteGet(“test123456”).Value));

Thread.Sleep(new TimeSpan(0, 0, 1));

}

Console.Read();

Can you help us to solve this problem?

I ran into the same problem and it looks like there is a bug in version 1.3.6 of the .NET client in the GetExpiration() method around line 1048: https://github.com/couchbase/couchbase-net-client/blob/master/src/Enyim.Caching/MemcachedClient.cs

When passed a TimeSpan, the code should be using TotalSeconds instead of Seconds. If you want to continue using version 1.3.6 of the .NET client, the workaround is to use the ExecuteStore() method with DateTime in the signature. Example:

TimeSpan validFor = new TimeSpan(0, 0, 59); DateTime expiresAt = DateTime.UtcNow.Add(validFor); client.ExecuteStore(StoreMode.Set, "test1234", "blabababla", expiresAt);

This is correct and the bug will be fixed in 1.3.7 which will be released sometime during the first week of July 2014.

-Jeff