1 /// <summary> 2 /// HttpRuntime Cache读取设置缓存信息封装 3 /// 使用描述:给缓存赋值使用HttpRuntimeCache.Set(key,value....)等参数(第三个参数可以传递文件的路径(HttpContext.Current.Server.MapPath())) 4 /// 读取缓存中的值使用JObject jObject=HttpRuntimeCache.Get(key) as JObject,读取到值之后就可以进行一系列判断 5 /// </summary> 6 public class CacheExtend 7 { 8 /// <summary> 9 /// 设置缓存时间,配置(从配置文件中读取) 10 /// </summary> 11 private const double Seconds = 30*24*60*60; 12 13 /// <summary> 14 /// 缓存指定对象,设置缓存 15 /// </summary> 16 public static bool Set(string key, object value) 17 { 18 return Set(key, value, null, DateTime.Now.AddSeconds(Seconds), Cache.NoSlidingExpiration, 19 CacheItemPriority.Default, null); 20 } 21 22 /// <summary> 23 /// 缓存指定对象,设置缓存 24 /// </summary> 25 public static bool Set(string key, object value, string path) 26 { 27 try 28 { 29 var cacheDependency = new CacheDependency(path); 30 return Set(key, value, cacheDependency); 31 } 32 catch 33 { 34 return false; 35 } 36 } 37 38 /// <summary> 39 /// 缓存指定对象,设置缓存 40 /// </summary> 41 public static bool Set(string key, object value, CacheDependency cacheDependency) 42 { 43 return Set(key, value, cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, 44 CacheItemPriority.Default, null); 45 } 46 47 /// <summary> 48 /// 缓存指定对象,设置缓存 49 /// </summary> 50 public static bool Set(string key, object value, double seconds, bool isAbsulute) 51 { 52 return Set(key, value, null, (isAbsulute ? DateTime.Now.AddSeconds(seconds) : Cache.NoAbsoluteExpiration), 53 (isAbsulute ? Cache.NoSlidingExpiration : TimeSpan.FromSeconds(seconds)), CacheItemPriority.Default, 54 null); 55 } 56 57 /// <summary> 58 /// 获取缓存对象 59 /// </summary> 60 public static object Get(string key) 61 { 62 return GetPrivate(key); 63 } 64 65 /// <summary> 66 /// 判断缓存中是否含有缓存该键 67 /// </summary> 68 public static bool Exists(string key) 69 { 70 return (GetPrivate(key) != null); 71 } 72 73 /// <summary> 74 /// 移除缓存对象 75 /// </summary> 76 /// <param name="key"></param> 77 /// <returns></returns> 78 public static bool Remove(string key) 79 { 80 if (string.IsNullOrEmpty(key)) 81 { 82 return false; 83 } 84 HttpRuntime.Cache.Remove(key); 85 return true; 86 } 87 88 /// <summary> 89 /// 移除所有缓存 90 /// </summary> 91 /// <returns></returns> 92 public static bool RemoveAll() 93 { 94 IDictionaryEnumerator iDictionaryEnumerator = HttpRuntime.Cache.GetEnumerator(); 95 while (iDictionaryEnumerator.MoveNext()) 96 { 97 HttpRuntime.Cache.Remove(Convert.ToString(iDictionaryEnumerator.Key)); 98 } 99 return true; 100 } 101 102 103 /// <summary> 104 /// 尝试获取缓存,不存在则执行匿名委托 105 /// </summary> 106 /// <typeparam name="T"></typeparam> 107 /// <param name="key"></param> 108 /// <param name="func"></param> 109 /// <returns></returns> 110 public static T TryGet<T>(string key, Func<T> func) 111 { 112 if (Exists(key)) return (T) Get(key); 113 var result = func.Invoke(); 114 Set(key, result); 115 return result; 116 } 117 118 119 120 //------------------------提供给上面方法进行调用----------------------------------- 121 /// <summary> 122 /// 设置缓存 123 /// </summary> 124 public static bool Set(string key, object value, CacheDependency cacheDependency, DateTime dateTime, 125 TimeSpan timeSpan, CacheItemPriority cacheItemPriority, CacheItemRemovedCallback cacheItemRemovedCallback) 126 { 127 if (string.IsNullOrEmpty(key) || value == null) 128 { 129 return false; 130 } 131 HttpRuntime.Cache.Insert(key, value, cacheDependency, dateTime, timeSpan, cacheItemPriority, 132 cacheItemRemovedCallback); 133 return true; 134 } 135 136 /// <summary> 137 /// 获取缓存 138 /// </summary> 139 private static object GetPrivate(string key) 140 { 141 return string.IsNullOrEmpty(key) ? null : HttpRuntime.Cache.Get(key); 142 } 143 }