缓存-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缓存的数据

 

posted @ 2022-04-09 20:53  留下成长的足迹  阅读(164)  评论(0编辑  收藏  举报