NET Core: EnyimMemcachedCore

更新时间:

客户端介绍

EnyimMemcachedCore 是一个从 EnyimMemcached 迁移至 .NET Core 的 Memcached 客户端,支持 .NET Core。

1. 安装 NuGet 包

  1. Install-Package EnyimMemcachedCore

2. 配置

2.1 在 appsetting.json 中进行配置

  • 不带验证的配置
  1. {
  2. "enyimMemcached": {
  3. "Servers": [
  4. {
  5. "Address": "memcached",
  6. "Port": 11211
  7. }
  8. ]
  9. }
  10. }
  • 带验证的配置
  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. }
  • 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 直接硬编码配置(无需配置文件)

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. 调用

3.1 使用 IMemcachedClient 接口

  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 使用 IDistributedCache 接口(来自 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. }