Memcache expiry time 1 hour out using AWS Elasticache and Enyim memcached client

1.2k Views Asked by At

I'm trying to test my AWS Elasticache nodes using Memcached and the Enyim client, however for some reason the expiry time seems to be out by 1 hour.

I've added data using this code:

_client.Store(StoreMode.Set, "testkey", "test", DateTime.Now.AddMinutes(1));

Then I'm attempting to retrieve the data using

var data = _client.Get<string>("testkey");

However this never retrieves the data. If however I change the expiry to 61 minutes from now, it will store the data for 1 minute. I've tried this with TimeSpan rather than DateTime.Now but get the same issue.

I've also outputted DateTime.Now value which is correct, and my AWS region is EU-West which is in the same timezone as the outputted DateTime.Now

2

There are 2 best solutions below

0
andrewm On

I figured it out myself. I didn't really take UTC into account, and that's what the Elasticache instances use. Therefore to fix my issue I just need to use DateTime.UtcNow rather than DateTime.Now

0
Houman On

It would be safer to use:

_client.Store(StoreMode.Set, "testkey", "test", TimeSpan.FromMinutes(1.0));

This way, you won't have any dependencies to the DateTime and UTC.