edit-icon download-icon

NET Core_ EnyimMemcachedCore

Last Updated: Dec 14, 2017

About the client

EnyimMemcachedCore is a Memcached client for migrating data from EnyimMemcached to .NET Core and supports .NET Core.

1. Install the NuGet package

  1. Install-Package EnyimMemcachedCore

2. Configure NuGet

2.1 Configure in appsetting.json

  • Configuration of NuGet without verification

    1. {
    2. "enyimMemcached": {
    3. "Servers": [
    4. {
    5. "Address": "memcached",
    6. "Port": 11211
    7. }
    8. ]
    9. }
    10. }
  • Configuration of NuGet with verification

    1. {
    2. "enyimMemcached": {
    3. "Servers": [
    4. {
    5. "Address": "memcached",
    6. "Port": 11211
    7. }
    8. ],
    9. "Authentication": {
    10. "Type": "Enyim.Caching.Memcached.PlainTextAuthenticator",
    11. "Parameters": {
    12. "zone": "",
    13. "userName": "username",
    14. "password": "password"
    15. }
    16. }
    17. }
    18. }
  • Configuration code in Startup.cs

    1. public class Startup
    2. {
    3. public void ConfigureServices(IServiceCollection services)
    4. {
    5. services.AddEnyimMemcached(options => Configuration.GetSection("enyimMemcached").Bind(options));
    6. }
    7. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    8. {
    9. app.UseEnyimMemcached();
    10. }
    11. }

2.2 Direct hard-coding configuration (no configuration files required)

Hard-coding configuration code in Startup.cs

  1. public class Startup
  2. {
  3. public void ConfigureServices(IServiceCollection services)
  4. {
  5. services.AddEnyimMemcached(options =>
  6. {
  7. options.AddServer("memcached", 11211);
  8. //options.AddPlainTextAuthenticator("", "usename", "password");
  9. });
  10. }
  11. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
  12. {
  13. app.UseEnyimMemcached();
  14. }
  15. }

3. Call

3.1 Use IMemcachedClient interface

  1. public class TabNavService
  2. {
  3. private ITabNavRepository _tabNavRepository;
  4. private IMemcachedClient _memcachedClient;
  5. public TabNavService(
  6. ITabNavRepository tabNavRepository,
  7. IMemcachedClient memcachedClient)
  8. {
  9. _tabNavRepository = tabNavRepository;
  10. _memcachedClient = memcachedClient;
  11. }
  12. public async Task<IEnumerable<TabNav>> GetAll()
  13. {
  14. var cacheKey = "aboutus_tabnavs_all";
  15. var result = await _memcachedClient.GetAsync<IEnumerable<TabNav>>(cacheKey);
  16. if (!result.Success)
  17. {
  18. var tabNavs = await _tabNavRepository.GetAll();
  19. await _memcachedClient.AddAsync(cacheKey, tabNavs, 300);
  20. return tabNavs;
  21. }
  22. else
  23. {
  24. return result.Value;
  25. }
  26. }
  27. }

3.2 Use IDistributedCache interface(from Microsoft.Extensions.Caching.Abstractions )

  1. public class CreativeService
  2. {
  3. private ICreativeRepository _creativeRepository;
  4. private IDistributedCache _cache;
  5. public CreativeService(
  6. ICreativeRepository creativeRepository,
  7. IDistributedCache cache)
  8. {
  9. _creativeRepository = creativeRepository;
  10. _cache = cache;
  11. }
  12. public async Task<IList<CreativeDTO>> GetCreatives(string unitName)
  13. {
  14. var cacheKey = $"creatives_{unitName}";
  15. IList<CreativeDTO> creatives = null;
  16. var creativesJson = await _cache.GetStringAsync(cacheKey);
  17. if (creativesJson == null)
  18. {
  19. creatives = await _creativeRepository.GetCreatives(unitName)
  20. .ProjectTo<CreativeDTO>().ToListAsync();
  21. var json = string.Empty;
  22. if (creatives != null && creatives.Count() > 0)
  23. {
  24. json = JsonConvert.SerializeObject(creatives);
  25. }
  26. await _cache.SetStringAsync(
  27. cacheKey,
  28. json,
  29. new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes(5)));
  30. }
  31. else
  32. {
  33. creatives = JsonConvert.DeserializeObject<List<CreativeDTO>>(creativesJson);
  34. }
  35. return creatives;
  36. }
  37. }
Thank you! We've received your feedback.