1. 介绍

EnyimMemcachedCore 是一个支持 .NET Core 的 Memcached 客户端,是从 EnyimMemcached 迁移至 .NET Core的,源代码托管在 GitHub 上:https://github.com/cnblogs/EnyimMemcachedCore ,NuGet 包地址:https://www.nuget.org/packages/EnyimMemcachedCore

2. 使用说明

2.1 安装 NuGet 包

Install-Package EnyimMemcachedCore

2.2 配置

2.2.1 在 appsetting.json 中进行配置

1)不带验证的配置

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

2)带验证的配置

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

3)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.2 直接硬编码配置(无需配置文件)

Startup.cs 中的硬编码配置代码

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

2.3 调用

2.3.1 使用 IMemcachedClient 接口

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, );
return tabNavs;
}
else
{
return result.Value;
}
}
}

2.3.2 使用 IDistributedCache 接口(来自 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() > )
{
json = JsonConvert.SerializeObject(creatives);
}
await _cache.SetStringAsync(
cacheKey,
json,
new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromMinutes()));
}
else
{
creatives = JsonConvert.DeserializeObject<List<CreativeDTO>>(creativesJson);
} return creatives;
}
}

3. 问题支持

如果在使用中遇到问题,麻烦您在 GitHub 上提交 Issue

最新文章

  1. Android内容观察者
  2. 转:Eclipse 一直不停 building workspace... 完美解决总结
  3. 简单的jquery选择器的实现
  4. android面试题之四
  5. CentOS 中 YUM 安装桌面环境(转)
  6. instr函数的&quot;重载&quot;
  7. 前端笔记之JavaScript面向对象(二)内置构造函数&amp;相关方法|属性|运算符&amp;继承&amp;面向对象
  8. 【pytorch】pytorch-backward()的理解
  9. Hive篇---Hive与Hbase整合
  10. 用Tensorflow实现DCGAN
  11. selenium 操作过程中,元素标红高亮的两种实现方式
  12. POJ3204 Ikki&#39;s Story I - Road Reconstruction
  13. Kubernetes图形化归纳总结基础介绍整理
  14. thinkphp5的生命周期
  15. http协议基础(九)响应首部字段
  16. Nginx 整合 FastDFS 实现文件服务器
  17. 2018软工项目UML设计(团队)
  18. ListOrderedMap与Map的区别
  19. 陈新宇:CKafka在人脸识别PAAS中的应用
  20. TP中if标签

热门文章

  1. Apache 与 php的环境搭建
  2. Angular企业级开发(1)-AngularJS简介
  3. ubuntu系统下如何修改host
  4. PHP 设计模式概述
  5. NodeJs支付宝移动支付签名及验签
  6. arcpy+PyQt+py2exe快速开发桌面端ArcGIS应用程序
  7. 使用Nginx反向代理 让IIS和Tomcat等多个站点一起飞
  8. 跟着老男孩教育学Python开发【第五篇】:模块
  9. Vue.js——60分钟组件快速入门(上篇)
  10. C#移动跨平台开发(2)Xamarin移动跨平台解决方案是如何工作的?