.Net Core MemoryCache-权限菜单缓存

2021-04-18 11:27

阅读:350

标签:实现   remove   pack   name   举例   扩展   help   框架   产品   

 前提:在框架设计中,因为权限菜单访问比较频繁,相对稳定,且数据体积较小,通过缓存来提升性能,提升获取数据的效率。
 
1.ICache封装
缓存接口,实现对缓存增删改查的封装
 1    public interface ICache
 2     {
 3         /// 
 4         /// 读取缓存
 5         /// 
 6         /// 
 7         /// 
 8         /// 
 9         T Get(string key);
10         /// 
11         /// 设置缓存
12         /// 
13         /// 
14         /// 
15         /// 
16         void Add(string key, T data, ObsloteType obsloteType = default, int cachetime =  30);
17         /// 
18         /// 是否包含
19         /// 
20         /// 
21         /// 
22         bool Contains(string key);
23         /// 
24         /// 清除缓存
25         /// 
26         /// 
27         bool Remove(string key);
28         /// 
29         /// 更新缓存
30         /// 
31         /// 
32         /// 
33         bool Upate(string key,T data, ObsloteType obsloteType = default, int cacheMin =  30);
34        
35     }

 

枚举类 ObsloteType
 1  /// 
 2     /// Cache策略类型  永久/绝对过期/滑动过期
 3     /// 
 4     public enum ObsloteType
 5     {
 6         /// 
 7         /// 永久
 8         /// 
 9         Never,
10         /// 
11         /// 绝对过期
12         /// 
13         Absolutely,
14         /// 
15         /// 滑动过期 如果期间查询或更新,就再次延长
16         /// 
17         Relative
18     }

 

 
2.MemoryCache使用
引用Nuget包  Install-Package Microsoft.Extensions.Caching.Memory
(1)缓存设置
在Startup.cs的ConfigureServices中添加
1 services.AddMemoryCache(options =>
2 {
3    //最大缓存空间大小限制为 1024
4    options.SizeLimit = 1024;
5    //缓存策略设置为缓存压缩比为 2%
6    options.CompactionPercentage = 0.02d;
7    //每 5 分钟进行一次过期缓存的扫描
8    options.ExpirationScanFrequency = TimeSpan.FromMinutes(5);
9 });
(2)注入并使用
1 private readonly IMemoryCache _memoryCache;
2 public HomeController(IMemoryCache memoryCache)
3 {
4             _memoryCache = memoryCache;
5 }

 

缓存的操作增删改查
(2-1)Set
public IActionResult Index()
{
  _memoryCache.Setstring>("timestamp", DateTime.Now.ToString());
  return View();
}
(2-2)Get
public IActionResult Show()
{
  string timestamp = _memoryCache.Getstring>("timestamp");
  return View("Show",timestamp);
}
(2-3)TryGetValue检查特定值
if (!_memoryCache.TryGetValuestring>
("timestamp", out string timestamp))
{
    _memoryCache.Setstring>("timestamp", DateTime.Now.ToString());
}
(2-4)GetOrCreate() 不存在新增,存在获取
public IActionResult Show()
{
  string timestamp = cache.GetOrCreatestring>
  ("timestamp", entry => {
return DateTime.Now.ToString(); });
  return View("Show",timestamp);
}
 

缓存的过期时间策略: 永久|绝对过期|滚动过期

(2-5)缓存时间配置
绝对过期:AbsoluteExpiration  在指定的日期和时间点被移除
滚动过期: SlidingExpiration 在一段时间内处于空闲状态会被移除
1 MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
2 options.AbsoluteExpiration = DateTime.Now.AddMinutes(1);
3 options.SlidingExpiration = TimeSpan.FromMinutes(1);
4 _memoryCache.Setstring>("timestamp", DateTime.Now.ToString(), options);
 
2.扩展MemoryEetensions
(1)新建“MemoryCacheExtensions”实现ICache的接口
   
 1 public   class MemoryCacheExtensions:ICache
 2     {
 3         private readonly IMemoryCache _cache;
 4         /// 
 5         /// 缓存配置项
 6         /// 
 7         private readonly MemoryCacheEntryOptions _memoryCacheEntryOptions;
 8         //构造器注入
 9         public MemoryCacheExtensions(IMemoryCache cache)
10         {
11              //单项缓存设置项
12             _memoryCacheEntryOptions = new MemoryCacheEntryOptions()
13             {
14                 Priority = CacheItemPriority.Low,
15                 //缓存大小占1份
16                 Size = 1
17             };
18             _cache = cache;
19         }
20         /// 
21         /// 添加
22         /// 
23         /// 
24         /// 
25         /// 
26         /// 
27         public void Add(string key, T data, ObsloteType obsloteType = default, int  cacheMin = 30)
28         {
29             if (obsloteType == ObsloteType.Absolutely)
30             {
31                 _memoryCacheEntryOptions.AbsoluteExpiration =  DateTime.Now.AddMinutes(cacheMin);
32             }
33             if (obsloteType == ObsloteType.Relative)
34             {
35                 _memoryCacheEntryOptions.SlidingExpiration =  TimeSpan.FromMinutes(cacheMin);
36             }
37             if (Contains(key))
38             {
39                 Upate(key, data, obsloteType);
40             }
41             else
42             {
43                 _cache.Set(key, data, _memoryCacheEntryOptions);
44             }
45            
46         }
47         /// 
48         /// 是否存在
49         /// 
50         /// 
51         /// 
52         public bool Contains(string key)
53         {
54             object RetValue;
55             return _cache.TryGetValue(key, out RetValue);
56         }
57         public T Get(string key)
58         {
59             return _cache.Get(key);
60         }
61       
62         public bool Remove(string key)
63         {
64             bool ReturnBool = true;
65             if (!Contains(key))
66             {
67                 ReturnBool = false;
68             }
69             else {
70                 _cache.Remove(key);
71             }
72             
73             return ReturnBool;
74         }
75         public bool Upate(string key,T data, ObsloteType obsloteType, int cacheMin =  30)
76         {
77             bool ReturnBool = true;
78             if (!Contains(key))
79             {
80                 ReturnBool = false;
81             }
82             else
83             {
84                 _cache.Remove(key);
85                 Add(key, data, obsloteType, cacheMin);
86             }
87             return ReturnBool;
88          
89         }
90     }
其中“MemoryCacheEntryOptions”缓存项设置,可以设置Priority(优先级),Size(缓存大小),缓存时间的配置(参见2-5)
(2)在Startup中注入扩展类
  services.AddScoped(typeof(MemoryCacheExtensions));
3.MemoryCacheExtensions使用,对权限菜单缓存读取
 1 private readonly Frame_RelationsService _service;
 2 private readonly MemoryCacheExtensions _memoryCacheExtensions;
 3 public FrameRelationsController(Frame_RelationsService service,  MemoryCacheExtensions  memoryCacheExtensions)
 4 {
 5   _service = service;
 6    _memoryCacheExtensions = memoryCacheExtensions;
 7 }
 8  
 9 [AllowAnonymous]
10 public string LoadRoleMenu( )
11 {
12    PageResponse resp = new PageResponse();
13    string KeyName = "Menu_" + CurrentUser.ID;
14      //第一次加载 判断是否有Key为"Keyname"的缓存
15    if (_memoryCacheExtensions.Contains(KeyName))
16    {
17       resp.Message = _memoryCacheExtensions.Get(KeyName);
18    }
19    else
20    {
21          //返回菜单权限的信息
22      resp.Message = _service.LoadRoleMenu(CurrentUser.RoleID);
23      _memoryCacheExtensions.Add(KeyName, resp.Message);
24    }
25    return  JsonHelper.Instance.Serialize(Newtonsoft.Json.Linq.JArray.Parse(resp.Message));
26  }

 

总结:适合缓存的特点:访问频繁,耗时耗资源,相对稳定,体积不大。
举例:字典/省市区/配置文件/公告信息/部门/权限/热搜/产品列表/商品评论

.Net Core MemoryCache-权限菜单缓存

标签:实现   remove   pack   name   举例   扩展   help   框架   产品   

原文地址:https://www.cnblogs.com/ywkcode/p/12283855.html


评论


亲,登录后才可以留言!