Core3.1本地模拟缓存已经系统自带的缓存的封装
手写简单地模拟本地缓存,

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace FEG.ESB.Common { /// <summary> /// 本地缓存的模拟使用,报表数据临时使用 /// @auth jason /// </summary> public static class FEG_LocalCacheHelper { private static readonly Dictionary<string, object> _dic; static FEG_LocalCacheHelper() { _dic = new Dictionary<string, object>(); } private static object obj = new object(); /// <summary> /// 新增缓存 这里直接使用object /// </summary> /// <param name="key"></param> /// <param name="obj"></param> /// <returns></returns> public static bool AddCacheData(string key, object obj) { if (!key.IsNullOrEmptyStr() && obj != null) { lock (obj) { if (!_dic.ContainsKey(key)) { _dic.Add(key, obj); return true; } } } return false; } /// <summary> /// 从本地缓存中拿到数据 /// </summary> /// <param name="key"></param> /// <returns></returns> public static object GetDataByKey(string key) { if (!key.IsNullOrEmptyStr()) { if (_dic.ContainsKey(key)) { object obj = _dic[key]; return obj != null ? obj : null; } } return null; } //public static SetCacheData(string key, Func<object> func) //{ // object obj = FEG_LocalCacheHelper.GetDataByKey(key); // if (obj == null) // { // } //} /// <summary> /// 移除本地缓存 /// </summary> /// <returns></returns> public static bool FlashLocalCache() { if (_dic != null) { foreach (var item in _dic.Keys) { if (_dic[item] != null) { _dic.Remove(item); } } return true; } return false; } } }

string key = $"KnowledgePointFamiliarity{cou_id}_{per_id}_{startTime.ToString("yyyyMMdd")}_{endTime.ToString("yyyyMMdd")}"; object cache_data = FEG_LocalCacheHelper.GetDataByKey(key); var data = new List<KnowledgePointFamiliarity>(); if (cache_data != null) { data = (List<KnowledgePointFamiliarity>)cache_data; br.Message = "查询知识点熟悉度信息成功!"; } else { data = _knowledgeService.KnowledgePointFamiliarity(cou_id, per_id, startTime, endTime); if (data != null && data.Any()) { FEG_LocalCacheHelper.AddCacheData(key, data); br.Message = "查询知识点熟悉度信息成功!"; } else { br.Message = "暂无知识点熟悉度信息!"; } }
系统自带缓存

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 6 namespace ZRFCoreTestMongoDB.Commoms 7 { 8 using Microsoft.Extensions.Caching.Memory; 9 using Microsoft.Extensions.Options; 10 using ZRFCoreTestMongoDB.Model; 11 /// <summary> 12 /// auth @ zrf 2020-07-23 13 /// </summary> 14 public class SystemCacheHelper 15 { 16 private static IMemoryCache msCache = null;// new MemoryCache(Options.Create(new MemoryCacheOptions())); 17 private static readonly object obj = new object(); 18 //static SystemCacheHelper() 19 //{ 20 // msCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); 21 //} 22 public static IMemoryCache CreateInstans() 23 { 24 if (msCache == null) 25 { 26 lock (obj) 27 { 28 if (msCache == null) 29 { 30 msCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); 31 } 32 } 33 } 34 return msCache; 35 } 36 37 /// <summary> 38 /// 滑动过期/绝对过期 39 /// </summary> 40 /// <typeparam name="T"></typeparam> 41 /// <param name="key"></param> 42 /// <param name="model"></param> 43 /// <param name="hd_ab">默认true :绝对过期,否则滑动过期</param> 44 /// <param name="Minutes"></param> 45 public static void SetCache<T>(string key, T model, bool hd_ab = true, int minutes = 3) 46 { 47 using (ICacheEntry entry = CreateInstans().CreateEntry(key)) 48 { 49 entry.Value = model; 50 if (hd_ab) 51 { 52 entry.AbsoluteExpiration = DateTime.Now.AddMinutes(minutes); 53 } 54 else 55 { 56 entry.SlidingExpiration = TimeSpan.FromMinutes(minutes); 57 } 58 } 59 } 60 61 /// <summary> 62 /// 文件依赖过期 63 /// </summary> 64 /// <typeparam name="T"></typeparam> 65 /// <param name="key"></param> 66 /// <param name="model"></param> 67 public static void SetCacheByFile<T>(string key, T model) 68 { 69 using (ICacheEntry entry = CreateInstans().CreateEntry(key)) 70 { 71 entry.Value = model; 72 string filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cachefile.txt"); 73 System.IO.FileInfo fileInfo = new System.IO.FileInfo(filepath); 74 entry.AddExpirationToken(new Microsoft.Extensions.FileProviders.Physical.PollingFileChangeToken(fileInfo)); 75 } 76 } 77 78 /// <summary> 79 /// 滑动过期 80 /// </summary> 81 /// <typeparam name="T"></typeparam> 82 /// <param name="key"></param> 83 /// <param name="model"></param> 84 /// <param name="Minutes"></param> 85 public static void SetCacheSliding<T>(string key, T model, int minutes = 3) 86 { 87 using (ICacheEntry entry = CreateInstans().CreateEntry(key)) 88 { 89 entry.Value = model; 90 entry.SlidingExpiration = TimeSpan.FromMinutes(minutes); 91 } 92 } 93 94 /// <summary> 95 /// 绝对过期 96 /// </summary> 97 /// <typeparam name="T"></typeparam> 98 /// <param name="key"></param> 99 /// <param name="model"></param> 100 /// <param name="Minutes"></param> 101 public static void SetCacheAbsolute<T>(string key, T model, int Minutes = 3) 102 { 103 using (ICacheEntry entry = CreateInstans().CreateEntry(key)) 104 { 105 entry.Value = model; 106 entry.AbsoluteExpiration = DateTime.Now.AddMinutes(Minutes); 107 } 108 } 109 public static T GetByCache<T>(string key) 110 { 111 if (CreateInstans().TryGetValue(key, out T model)) 112 { 113 return model; 114 } 115 return default; 116 } 117 } 118 }
如有疑问或者错误的地方,请跟帖,本人会第一时间答复以及相互学习,谢谢!个人会不断的上传自己的学习心得!
好了今天就先到这里,下次有时间再更新,如果存在不合理的地方,欢迎大家多多指教留言!!!
分类:
.NetCore/EFCore
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具