.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);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章