How to configure EnyimMemcachedCore to access Elasticache in AWS Lambda?

2.8k Views Asked by At

I am trying to port a simple memcached client from .NET 4 to .Net Core on AWS Lambda. I am struggling to configure the new EnyimMemcachedCore client because the examples (https://github.com/cnblogs/EnyimMemcachedCore) use appsettings.json to setup the config, but Lambda functions using .net core do not use appsettings.json. I need to be able to setup the server/port/endpoint in the C# code.

Can anyone give me an example using EnyimMemcachedCore that creates the configuration manually?

The standard .net use of Enyim was trivial to fetch by key and return a value:

using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;

...
// setup Enyim memcached client
MemcachedClient myCache;
MemcachedClientConfiguration config;
config = new MemcachedClientConfiguration(); 
config.AddServer("theIP", thePort);
config.Protocol = MemcachedProtocol.Text;

// instantiate client
myCache = new MemcachedClient(config);

// get the stored item
var result = myCache.Get(key);

How do I do something similar (configure the memcached client in code, not in a config file) with EnyimMemcachedCore?

3

There are 3 best solutions below

0
On

I guess this has been fixed as of today, 18 September 2018. I have tried the following appsettings, used the configuration endpoint of memcache which has one node

"enyimMemcached": {
    "Servers": [
        {
            "Address": "st-cache-01-v2.l0nmej.cfg.xxxx.cache.amazonaws.com",
            "Port": 11211
        }
    ]
}

And the code on the ConfigureServices

services.AddEnyimMemcached(Configuration);

It's working like charm. I haven't tried with two nodes yet. Please feel free to correct me if it doesn't work for more than one node

0
On

This is how you should do it:

  1. Install nuget Packages:

EnyimMemcachedCore

Microsoft.Extensions.Logging.Console

  1. Add dependencies:
using Enyim.Caching;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Microsoft.Extensions.Logging;
  1. Initialize
using var loggerFactory = LoggerFactory.Create(builder =>
{
    builder.AddConsole();
});

ILogger logger = loggerFactory.CreateLogger<MemcachedHelper>();

var configMemcachedOptions = new MemcachedClientOptions();
configMemcachedOptions.UseSslStream = true;
var config = new MemcachedClientConfiguration(loggerFactory, configMemcachedOptions);
config.AddServer(serverAddress, port);
config.Protocol = MemcachedProtocol.Text;

_memcachedClient = new MemcachedClient(loggerFactory, config);

More information can be found in here: https://nodogmablog.bryanhogan.net/2022/10/using-a-distributed-memory-cache-with-net-lambda-functions/

Just remember to turn on configMemcachedOptions.UseSslStream = true;

0
On
// setup Enyim memcached client
var config = new MemcachedClientConfiguration();

//add each node manually if you can't get the Amazon.ElastiCacheCluster config for Core, 
//but if you can, use that instead of MemcachedClientConfiguration
config.AddServer("something.0001.usw1.cache.amazonaws.com", 11211);
config.AddServer("something.0002.usw1.cache.amazonaws.com", 11211);

config.Protocol = MemcachedProtocol.Text;

// instantiate client
var myCache = new Enyim.Caching.MemcachedClient(config);

you can add the nodes individually until the cluster config becomes available for .NET Core (if it hasn't yet)