NET Core: EnyimMemcachedCore
更新时间:
客户端介绍
EnyimMemcachedCore 是一个从 EnyimMemcached 迁移至 .NET Core 的 Memcached 客户端,支持 .NET Core。
源代码托管在 GitHub 上的地址:https://github.com/cnblogs/EnyimMemcachedCore
1. 安装 NuGet 包
Install-Package EnyimMemcachedCore
2. 配置
2.1 在 appsetting.json 中进行配置
- 不带验证的配置
{
"enyimMemcached": {
"Servers": [
{
"Address": "memcached",
"Port": 11211
}
]
}
}
- 带验证的配置
{
"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(options => Configuration.GetSection("enyimMemcached").Bind(options));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseEnyimMemcached();
}
}
2.2 直接硬编码配置(无需配置文件)
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. 调用
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, 300);
return tabNavs;
}
else
{
return result.Value;
}
}
}
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() > 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;
}
}
文档内容是否对您有帮助?