.net 方法内容缓存简化
调用:
1 2 3 4 5 6 7 8 9 | public string GetNodeName( string nodeNum) { Func< string > func = () => { return db.FirstOrDefault< string >( "SELECT PNodeName FROM " + NodeNumTableName + " WHERE PNodeNum=@0 AND AppKey=@1" , nodeNum, PublicVars.AppKey); }; return CacheHelper.Set< string >( func, 1200, nodeNum); } |
缓存帮助类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | /// <summary> /// 缓存帮助类 /// </summary> public static class CacheHelper { /// <summary> /// 获取 /// </summary> /// <param name="name">key</param> /// <returns></returns> public static object Get( string name) { return HttpRuntime.Cache.Get(name); } /// <summary> /// 获取 /// </summary> /// <param name="method">缓存方法</param> /// <param name="methodParams">方法参数(保证唯一性)</param> /// <returns></returns> public static object Get(MethodBase method, params object [] methodParams) { return HttpRuntime.Cache.Get(GetMethodCacheName(method, methodParams)); } /// <summary> /// 移除 /// </summary> /// <param name="name">key</param> public static void Remove( string name) { HttpRuntime.Cache.Remove(name); } /// <summary> /// 移除所有缓存 /// </summary> /// <returns></returns> public static bool RemoveAll() { IDictionaryEnumerator iDictionaryEnumerator = HttpRuntime.Cache.GetEnumerator(); while (iDictionaryEnumerator.MoveNext()) { HttpRuntime.Cache.Remove(Convert.ToString(iDictionaryEnumerator.Key)); } return true ; } /// <summary> /// 写入 /// </summary> /// <param name="name">key</param> /// <param name="value">value</param> /// <param name="cacheDependency">依赖项</param> /// <param name="timeOut">过期时间(秒)</param> public static void Set( string name, object value, CacheDependency cacheDependency = null , int timeOut = 20 * 60) { if (value == null ) //缓存空对象要报错 { return ; } if (cacheDependency == null ) { HttpRuntime.Cache.Insert(name, value, null , DateTime.Now.AddSeconds(timeOut), TimeSpan.Zero); } else { HttpRuntime.Cache.Insert(name, value, cacheDependency, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(timeOut)); } } /// <summary> /// 写入 /// </summary> /// <typeparam name="T">返回类型</typeparam> /// <param name="func">数据方法</param> /// <param name="timeOut">缓存时间(秒)</param> /// <param name="methodParams">方法参数(保证唯一性)</param> /// <returns>缓存数据</returns> public static T Set<T>(Func<T> func, int timeOut = 20 * 60, params object [] methodParams) where T : class { /* * 注意事项: * 1.本方法请勿滥用,太多的缓存也会降低性能 * 2.调用本方法一定要避免返回脏数据问题,请合理利用methodParams参数 * 3.缓存时间要控制在合理的时间段内,如果时间很短一定要考虑一个问题:缓存数据的利用率怎么样,是否真的需要缓存 * 4.返回后不能操作对象,否则可能出现缓存数据为操作后的对象问题 */ if (timeOut < 1) { return func(); } string cacheKey = GetMethodCacheName(func.Method, methodParams); T cacheData = CacheHelper.Get(cacheKey) as T; if (cacheData == null ) { cacheData = func(); CacheHelper.Set(cacheKey, cacheData, null , timeOut); } return cacheData; } /// <summary> /// 获取换成方法缓存名称 /// </summary> /// <param name="method">缓存方法</param> /// <param name="methodParams">方法参数(保证唯一性)</param> /// <returns></returns> public static string GetMethodCacheName(MethodBase method, object [] methodParams) { string cacheKey = $ "CH_{method.DeclaringType.FullName}_{method.Name}" ; if (methodParams != null && methodParams.Length > 0) { cacheKey += "_" + MD5Helper.MD5Encrypt32(SerializeHelper.ToJson(methodParams)); } return cacheKey; } } |
其他方法:AOP
分类:
ASP.NET
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构