首页 技术 正文
技术 2022年11月6日
0 收藏 461 点赞 813 浏览 2836 个字

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,093
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,569
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,417
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,190
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,825
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,908