支持 .NET Core 的 Memcached 客户端 EnyimMemcachedCore
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", 11211); //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, 300); 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() > 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; } }
3. 问题支持
如果在使用中遇到问题,麻烦您在 GitHub 上提交 Issue 。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· DeepSeek本地性能调优
· autohue.js:让你的图片和背景融为一体,绝了!
· 10亿数据,如何做迁移?
2013-12-12 云计算之路-阿里云上:用上了开放缓存服务OCS
2013-12-12 园子里游戏开发者的福利:通过认证免费领取2台高配“阿里云”云服务器
2011-12-12 上周热点回顾(12.5-12.11)