A Memcached client for .NET Core. Available on Nuget https://www.nuget.org/packages/EnyimMemcachedCore
Branch: master
Clone or download
Pull request Compare This branch is 300 commits ahead, 20 commits behind enyim:master.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Enyim.Caching.Tests Tweak MemcachedClientGetTests for Unexpected BsonType exception Dec 30, 2018
Enyim.Caching Change version number to 2.1.12 Dec 31, 2018
MemcachedTest Update packages Dec 29, 2018
SampleWebApp.IntegrationTests Update packages Dec 29, 2018
SampleWebApp Update packages Dec 29, 2018
.gitignore Add SampleWebApp project Dec 9, 2016
.travis.yml
EnyimCachingCore.sln Refactor PooledSocket.ReadAsync method Dec 30, 2018
LICENSE Changed license to Apache 2.0. Mar 15, 2010
README.mdown Update README.mdown Sep 20, 2018
build.sh Update build.sh and run-test.sh Mar 17, 2017
footer.txt Changed license to Apache 2.0. Mar 15, 2010
test.sh Rewrite SampleWebApp and add integration tests Sep 20, 2018

README.mdown

Enyim Memcached Client

This is a .NET Core client library for Memcached migrated from EnyimMemcached. Available on Nuget https://www.nuget.org/packages/EnyimMemcachedCore

Build Status

Configure

The appsettings.json Without Authentication

{
  "enyimMemcached": {
    "Servers": [
      {
        "Address": "memcached",
        "Port": 11211
      }
    ]
  }
}

The appsettings.json With Authentication

{
  "enyimMemcached": {
    "Servers": [
      {
        "Address": "memcached",
        "Port": 11211
      }
    ],
    "Authentication": {
      "Type": "Enyim.Caching.Memcached.PlainTextAuthenticator",
      "Parameters": {
        "zone": "",
        "userName": "username",
        "password": "password"
      }
    }
  }
}

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEnyimMemcached();
        //services.AddEnyimMemcached(Configuration);
        //services.AddEnyimMemcached(Configuration, "enyimMemcached");
        //services.AddEnyimMemcached(Configuration.GetSection("enyimMemcached"));
        //services.AddEnyimMemcached(options => options.AddServer("memcached", 11211));
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    { 
        app.UseEnyimMemcached();
    }
}

Example usage

Use IMemcachedClient interface

public class HomeController : Controller
{
    private readonly IMemcachedClient _memcachedClient;
    private readonly IBlogPostService _blogPostService;

    public HomeController(IMemcachedClient memcachedClient, IBlogPostService blogPostService)
    {
        _memcachedClient = memcachedClient;
        _blogPostService = blogPostService;
    }

    public async Task<IActionResult> Index()
    {
        var cacheKey = "blogposts-recent";
        var cacheSeconds = 600;

        var posts = await _memcachedClient.GetValueOrCreateAsync(
            cacheKey,
            cacheSeconds,
            async () => await _blogPostService.GetRecent(10));

        return Ok(posts);
    }
}

Use IDistributedCache interface

public class CreativeService
{
    private ICreativeRepository _creativeRepository;
    private IDistributedCache _cache;

    public CreativeService(
        ICreativeRepository creativeRepository,
        IDistributedCache cache)
    {
        _creativeRepository = creativeRepository;
        _cache = cache;
    }

    public async Task<IList<CreativeDTO>> GetCreatives(string unitName)
    {
        var cacheKey = $"creatives_{unitName}";
        IList<CreativeDTO> creatives = null;

        var creativesJson = await _cache.GetStringAsync(cacheKey);
        if (creativesJson == null)
        {
            creatives = await _creativeRepository.GetCreatives(unitName)
                    .ProjectTo<CreativeDTO>().ToListAsync();

            var json = string.Empty;
            if (creatives != null && creatives.Count() > 0)
            {
                json = JsonConvert.SerializeObject(creatives);
            }
            await _cache.SetStringAsync(
                cacheKey, 
                json, 
                new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(5)));
        }
        else
        {
            creatives = JsonConvert.DeserializeObject<List<CreativeDTO>>(creativesJson);
        }

        return creatives;
    }
}