调用
public string GetNodeName(string nodeNum) { Func<string> func = () => { return db.FirstOrDefault<string>("SELECT PNodeName FROM " + PublicVars.NodeNumTableName + " WHERE PNodeNum=@0 AND AppKey=@1", nodeNum, BaseConfig.AppKey); }; return CacheHelper.Set<string>(MethodBase.GetCurrentMethod(), func); }
缓存帮助类
/// <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="name">key</param> public static void Remove(string name) { if (HttpRuntime.Cache[name] != null) HttpRuntime.Cache.Remove(name); } /// <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 (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="method">缓存方法</param> /// <param name="func">数据方法</param> /// <param name="timeOut">缓存时间(秒)</param> /// <returns>缓存数据</returns> public static T Set<T>(MethodBase method, Func<T> func, int timeOut = 20 * 60) where T : class { ParameterInfo[] parameters = method.GetParameters(); string cacheKey = $"CH_{method.DeclaringType.Name}_{method.Name}"; if (parameters.Length > 0) { cacheKey += "_" + MD5Helper.MD5Encrypt32(SerializeHelper.ToJson(parameters)); } T cacheData = CacheHelper.Get(cacheKey) as T; if (cacheData == null) { cacheData = func(); CacheHelper.Set(cacheKey, cacheData, null, timeOut); } return cacheData; } }
其他方法:AOP
标签:缓存,name,Cache,简化,net,null,public,string From: https://www.cnblogs.com/5tomorrow/p/16986933.html