缓存-memoryCache和redis分布式缓存
相似redis分布式缓存,实现session共享:redis实现共享session - 留下成长的足迹 - 博客园 (cnblogs.com)
1.本地memoryCache
安装nuget包
2.依赖注入MemoryCache
---在startup类中注册 public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddMemoryCache(options => { options.Clock = new LocalClock(); }); } private class LocalClock : ISystemClock { public DateTimeOffset UtcNow => DateTime.Now; } 在controller类中使用 public class MemoryCacheController : Controller { private readonly ILogger<MemoryCacheController> _logger; private IConfiguration _iConfiguration; private IMemoryCache _iMemoryCache; public MemoryCacheController(ILogger<MemoryCacheController> logger, IConfiguration configuration, IMemoryCache memoryCache) { this._logger = logger; this._iConfiguration = configuration; this._iMemoryCache = memoryCache; } public IActionResult Index() { string key = $"MemoryCacheController"; #region MemoryCache if (this._iMemoryCache.TryGetValue<string>(key, out string time)) { } else { time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"); this._iMemoryCache.Set(key, time, TimeSpan.FromSeconds(120)); } base.ViewBag.MemoryCacheNow = time; #endregion return View(); } }
3.结合redis,把MemoryCache的内容放在redis中。
下载buget包
启动redis
在startup类中 public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); //redis分布式缓存 services.AddDistributedRedisCache(options => { options.Configuration = "127.0.0.1:6379"; options.InstanceName = "RedisDistributionedCache20220409"; }); services.AddMemoryCache(options => { options.Clock = new LocalClock(); }); } public class MemoryCacheController : Controller { private readonly ILogger<MemoryCacheController> _logger; private IConfiguration _iConfiguration; private IMemoryCache _iMemoryCache; private IDistributedCache _iDistributeCache; public MemoryCacheController(ILogger<MemoryCacheController> logger, IConfiguration configuration, IMemoryCache memoryCache, IDistributedCache distributeCache) { this._logger = logger; this._iConfiguration = configuration; this._iMemoryCache = memoryCache; this._iDistributeCache = distributeCache; } public IActionResult Index() { string key = $"MemoryCacheController"; #region MemoryCache if (this._iMemoryCache.TryGetValue<string>(key, out string time)) { } else { time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"); this._iMemoryCache.Set(key, time, TimeSpan.FromSeconds(120)); } base.ViewBag.MemoryCacheNow = time; #endregion #region distributionCache string keyDistributeCache = $"MemoryCacheController-DistributeCache"; string timeDistributed = this._iDistributeCache.GetString(keyDistributeCache); if (!string.IsNullOrEmpty(timeDistributed)) { } else { timeDistributed = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"); this._iDistributeCache.SetString(keyDistributeCache, timeDistributed, new DistributedCacheEntryOptions() { AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(20) }); } base.ViewBag.DistributedCacheNow = timeDistributed; #endregion return View(); } }
redis缓存的数据
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix