All Products
Search
Document Center

:NET Core_ EnyimMemcachedCore

Last Updated:Mar 06, 2025

About the client

EnyimMemcachedCore is a Memcached client migrated from EnyimMemcached to .NET Core. The client supports .NET Core.

1. Install the NuGet package

Install-Package EnyimMemcachedCore

2. Configure the NuGet package

2.1 Configure the NuGet package in appsetting.json

  • Configuration without verification

      {
        "enyimMemcached": {
          "Servers": [
            {
              "Address": "memcached",
              "Port": 11211
            }
          ]
        }
      }
  • Configuration with verification

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

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

2.2 Hard-code the NuGet package without configuration files

Hard-coding configuration code in Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddEnyimMemcached(options =>
        {
            options.AddServer("memcached", 11211);                
            //options.AddPlainTextAuthenticator("", "usename", "password");
        });
    }        

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseEnyimMemcached();
    }
}

3. Call the NuGet package

3.1 Call the IMemcachedClient operation

public class TabNavService
{
    private ITabNavRepository _tabNavRepository;
    private IMemcachedClient _memcachedClient;

    public TabNavService(
        ITabNavRepository tabNavRepository,
        IMemcachedClient memcachedClient)
    {
        _tabNavRepository = tabNavRepository;
        _memcachedClient = memcachedClient;
    }

    public async Task<IEnumerable<TabNav>> GetAll()
    {
        var cacheKey = "aboutus_tabnavs_all";
        var result = await _memcachedClient.GetAsync<IEnumerable<TabNav>>(cacheKey);
        if (!result.Success)
        {
            var tabNavs = await _tabNavRepository.GetAll();
            await _memcachedClient.AddAsync(cacheKey, tabNavs, 300);
            return tabNavs;
        }
        else
        {
            return result.Value;
        }
    }
}

3.2 call the IDistributedCache operation from Microsoft.Extensions.Caching.Abstractions

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;
    }
}