标签:实现 remove pack name 举例 扩展 help 框架 产品
1 public interface ICache 2 { 3 /// <summary> 4 /// 读取缓存 5 /// </summary> 6 /// <typeparam name="T"></typeparam> 7 /// <param name="key"></param> 8 /// <returns></returns> 9 T Get<T>(string key); 10 /// <summary> 11 /// 设置缓存 12 /// </summary> 13 /// <param name="key"></param> 14 /// <param name="data"></param> 15 /// <param name="cachetime"></param> 16 void Add<T>(string key, T data, ObsloteType obsloteType = default, int cachetime = 30); 17 /// <summary> 18 /// 是否包含 19 /// </summary> 20 /// <param name="key"></param> 21 /// <returns></returns> 22 bool Contains(string key); 23 /// <summary> 24 /// 清除缓存 25 /// </summary> 26 /// <param name="key"></param> 27 bool Remove(string key); 28 /// <summary> 29 /// 更新缓存 30 /// </summary> 31 /// <param name="key"></param> 32 /// <returns></returns> 33 bool Upate<T>(string key,T data, ObsloteType obsloteType = default, int cacheMin = 30); 34 35 }
1 /// <summary> 2 /// Cache策略类型 永久/绝对过期/滑动过期 3 /// </summary> 4 public enum ObsloteType 5 { 6 /// <summary> 7 /// 永久 8 /// </summary> 9 Never, 10 /// <summary> 11 /// 绝对过期 12 /// </summary> 13 Absolutely, 14 /// <summary> 15 /// 滑动过期 如果期间查询或更新,就再次延长 16 /// </summary> 17 Relative 18 }
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 });
1 private readonly IMemoryCache _memoryCache; 2 public HomeController(IMemoryCache memoryCache) 3 { 4 _memoryCache = memoryCache; 5 }
public IActionResult Index() { _memoryCache.Set<string>("timestamp", DateTime.Now.ToString()); return View(); }
public IActionResult Show() { string timestamp = _memoryCache.Get<string>("timestamp"); return View("Show",timestamp); }
if (!_memoryCache.TryGetValue<string> ("timestamp", out string timestamp)) { _memoryCache.Set<string>("timestamp", DateTime.Now.ToString()); }
public IActionResult Show() { string timestamp = cache.GetOrCreate<string> ("timestamp", entry => { return DateTime.Now.ToString(); }); return View("Show",timestamp); }
缓存的过期时间策略: 永久|绝对过期|滚动过期
1 MemoryCacheEntryOptions options = new MemoryCacheEntryOptions(); 2 options.AbsoluteExpiration = DateTime.Now.AddMinutes(1); 3 options.SlidingExpiration = TimeSpan.FromMinutes(1); 4 _memoryCache.Set<string>("timestamp", DateTime.Now.ToString(), options);
1 public class MemoryCacheExtensions:ICache 2 { 3 private readonly IMemoryCache _cache; 4 /// <summary> 5 /// 缓存配置项 6 /// </summary> 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 /// <summary> 21 /// 添加 22 /// </summary> 23 /// <param name="key"></param> 24 /// <param name="data"></param> 25 /// <param name="cacheMin"></param> 26 /// <param name="obsloteType"></param> 27 public void Add<T>(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<T>(key, data, obsloteType); 40 } 41 else 42 { 43 _cache.Set(key, data, _memoryCacheEntryOptions); 44 } 45 46 } 47 /// <summary> 48 /// 是否存在 49 /// </summary> 50 /// <param name="key"></param> 51 /// <returns></returns> 52 public bool Contains(string key) 53 { 54 object RetValue; 55 return _cache.TryGetValue(key, out RetValue); 56 } 57 public T Get<T>(string key) 58 { 59 return _cache.Get<T>(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<T>(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 }
services.AddScoped(typeof(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<string>(KeyName); 18 } 19 else 20 { 21 //返回菜单权限的信息 22 resp.Message = _service.LoadRoleMenu(CurrentUser.RoleID); 23 _memoryCacheExtensions.Add<string>(KeyName, resp.Message); 24 } 25 return JsonHelper.Instance.Serialize(Newtonsoft.Json.Linq.JArray.Parse(resp.Message)); 26 }
标签:实现 remove pack name 举例 扩展 help 框架 产品
原文地址:https://www.cnblogs.com/ywkcode/p/12283855.html