.Net core中使用MemoryCache缓存

1.基础使用:IMemoryCache接口中有的

2.option讲解:主要讲解滑动过期

3.通过.net core中对于memorycache的扩展来使用

4.通过IOC来使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.Options;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;

namespace Web.Controllers
{
    public class TestCacheController : Controller
    {
        public IActionResult Index()
        {
            //初始化:基础版本(后续会使用IOC)
            IMemoryCache msCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
            //基础使用:
            string key = "cacheBasic";
            //添加到缓存中
            using (var cacheEntry = msCache.CreateEntry(key))
            {
                cacheEntry.Value = "将数据存在在cache中:张三";
            }
            //从缓存中读取
            var tryGetResult = msCache.TryGetValue(key, out var value);
            var msgBasic = string.Format(@"key = {0},是否存在:{1},value = {2}", key, tryGetResult, value);
            return Content(msgBasic);
        }

        public IActionResult IndexSlideExpired()
        {
            //option什么的和.net framework都一样的,这里就不讲了
            //这里跟大家讲一下滑动过期吧,大家可能不理解这个词啥意思
            //其实就是,在多久时间内没有使用该缓存,就会移除该缓存
            //将值添加到缓存中
            string key = "testSlideExpired";
            IMemoryCache msCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
            using (var cacheEntry = msCache.CreateEntry(key))
            {
                cacheEntry.Value = "测试滑动过期,值为:张三";
                //设置为3秒滑动过期
                cacheEntry.SlidingExpiration = TimeSpan.FromSeconds(3);
            }
            //获取值
            var result1 = msCache.TryGetValue(key, out var value);
            string msg1 = string.Format("key = {0},是否获取到:{1},value = {2}", key, result1, value);
            //模拟耗时5秒
            Thread.Sleep(5 * 1000);
            //再次获取值
            var result2 = msCache.TryGetValue(key, out var value2);
            string msg2 = string.Format("key = {0},是否获取到:{1},value = {2}", key, result2, value2);
            string content = string.Format("第一次获取:{0};;;;;;第二次获取:{1}", msg1, msg2);
            return Content(content);
            //第一次获取:key = testSlideExpired,是否获取到:True,value = 测试滑动过期,值为:张三;;;;;;第二次获取:key = testSlideExpired,是否获取到:False,value = 
        }

        public IActionResult IndexExtension()
        {
            //使用.net core中对于memorycache的扩展来操作缓存
            IMemoryCache msCache = new MemoryCache(Options.Create(new MemoryCacheOptions()));
            //1.set:将数据添加到缓存中
            //*需要注意:
            //如果存在相同的key,之前的key会被删除,然后再设置新的,
            //也就意味着,新设置的缓存不会继承相同key的之前的属性(使用时请注意这一点,别缓存为啥过期为啥没了都不知道)
            string key = "setCache";
            msCache.Set(key, "张三");
            //2.getOrCreate:获取缓存,如果不存在,则新增(常用)
            string value = msCache.GetOrCreate(key, r => "李四");
            //3.get:获取值,不做过多说明
            string getValue = msCache.Get<string>(key);
            string msg1 = string.Format("key = {0},GetOrCreate = {1},Get = {2}", key, value, getValue);

            //上述GetOrCreate是获取到了已经存在的值

            //我这里设置个缓存,然后让它过期了再去GetOrCreate获取,就会获取不到,然后新增一条缓存
            string newKey = "测试GetOrCreate获取不到然后新增";
            msCache.Set(newKey, "set的值", TimeSpan.FromSeconds(3));
            //模拟耗时4秒
            Thread.Sleep(4 * 1000);

            string newValue = msCache.GetOrCreate(newKey, r => "GetOrCreate创建的值");
            string getValue2 = msCache.Get<string>(newKey);

            string msg2 = string.Format("key = {0},GetOrCreate = {1},Get = {2}", newKey, newValue, getValue2);

            return Content(msg2);
        }

        public IMemoryCache _MemoryCache;

        public TestCacheController(IMemoryCache msCache)
        {
            this._MemoryCache = msCache;
        }

        public IActionResult IndexUseIoc()
        {
            //通过IOC来使用MemoryCache
            //1.在startup中进行依赖注入
            //2.在要使用的地方使用构造函数来初始化
            //3.使用

            string key = "通过IOC使用MemoryCache";
            //设置值
            _MemoryCache.Set(key, "哈哈哈");
            //获取值
            string value = _MemoryCache.Get<string>(key);
            //获取或新增值
            string key1 = key + "的GetOrCreate";
            string value1 = _MemoryCache.GetOrCreate(key1, r => "啦啦啦");

            string msg = string.Format("key = {0},value = {1},key1 = {2},value1 = {3}", key, value, key1, value1);
            return Content(msg);
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值