AWS

AWS : ElastiCache

What are we talking about this time?

Last time we talked about AWS DynamoDB, this time we will talk about AWS ElasticCache

Initial setup

If you did not read the very first part of this series of posts, I urge you to go and read that one now as it shows you how to get started with AWS, and create an IAM user : https://sachabarbs.wordpress.com/2018/08/30/aws-initial-setup/

Where is the code

The code for this post can be found here in GitHub :

What are we talking about this time?

This time we will be talking about ElasticCache.

So what exactly is ElasticCache?

ElastiCache is AWSs cache cluster. This comes in 2 flavours

  • Redis
  • MemCached

Where you may use the console to pick your cluster size/VMs

image

I have chosen to use MemCached for this example. So once you have setup a cluster, you will see the nodes created for you in the AWS console. If you go and examine the nodes you can click on one of them, and you should see a dialog similar to this. It shows the nodes configuration end point, and also the actual nodes in the cluster. But it also shows you how to download a client library for use with .NET.

 

image

 

For .NET AWS used the following .NET Memcached client : https://github.com/enyim/EnyimMemcached. So if you did download using the link shown in the above dialog, you should see some DLLs like this

image

So once you have them its really just a job of referencing these 2 Dlls

  • Amazon.ElastiCacheCluster.dll
  • Enyim.Caching.dll

 

The  you should be able to add a App.Config like this (make sure you use the address with the “cfg” in it)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section 
            name="clusterclient" 
            type="Amazon.ElastiCacheCluster.ClusterConfigSettings, Amazon.ElastiCacheCluster" />
    </configSections>

    <clusterclient>
        <!-- the hostname and port values are from step 1 above -->
        <endpoint hostname="sachaXXXXXX.cfg.XXXXXXXXX.cache.amazonaws.com" port="11211" />
    </clusterclient>
</configuration>

And use some simple C# code to write/read from the cache like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Amazon.ElastiCacheCluster;
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;

namespace ElastiCache.Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            // instantiate a new client.
            ElastiCacheClusterConfig config = new ElastiCacheClusterConfig();
            MemcachedClient memClient = new MemcachedClient(config);

            // Store the data for 3600 seconds (1hour) in the cluster. 
            // The client will decide which cache host will store this item.
            memClient.Store(StoreMode.Set,"mykey","This is the data value.", TimeSpan.FromMinutes(10));

            var mykey = memClient.Get<string>("mykey");




            Console.ReadLine();
        }
    }
}

And to be honest that is all there really is to it.

 

I guess the final point is, if you do not like this MemCached .NET client, AWS Elasticache is “memcached compatible” so that really means you can use any memcached library of your choosing.

 

Anyway another nice short one, I am saving the AWS Batch one for a rainy day, that post will be quite indepth believe me

Advertisements