There are several handy blogs about how to use the Amazon DynamoDb, Amazon EC2 and other services but I guess there are not enough information about how to use the Elasticache service. Indeed, the documentation only mentions that the service is Memcached compliant but it’s a pain for C# developers who are starting to use this service to figure out the way to use it.

As you probably might know, Amazon provides API’s for Elasticache operations (ie: create a cache cluster), but not for cache “transactions” (ie: add/remove an item from/to the cache). You can use your favorite Memcached client. I prefer the BeIT Memcached library.

After selecting a client library you can write the code to use it and bear in mind that you need to know the end point address provided by Amazon. To get it, log onto the Amazon Web Console and select the Elasticache option within the services menu. Then click on “Launch Cache Cluster” button and give it a meaningful name.

Wait a few seconds, click on your newly created cache and select the “Copy Node Enpoint(s)”, then you will get an URL like the following one:

AmazonElasticacheEndPoint

Finally define a cache object, grab the URL from the config file with some code like the next example and start using its Set/Get methods:


AppSettingsReader reader = new AppSettingsReader();
 string memCachedUrl = reader.GetValue("MemCachedUrl", string.Empty.GetType()) as string;
 MemcachedClient.Setup("MyCache", new string[] { memCachedUrl });
 cache = MemcachedClient.GetInstance("MyCache");
 cache.SendReceiveTimeout = 5000;
 cache.ConnectTimeout = 5000;
 cache.MinPoolSize = 1;
 cache.MaxPoolSize = 5;
 loggerObj = logger;

Cheers,

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer