话不多说,直接上代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Collections; 6 using System.Web; 7 using System.Web.Caching; 8 9 namespace Tools 10 { 11 /// <summary> 12 /// 缓存辅助类 13 /// </summary> 14 public static class CacheHelper 15 { 16 #region // 绝对缓存过期时间 17 public static DateTime Absolute_Minute_1 18 { 19 get { return DateTime.Now.AddMinutes(1); } 20 } 21 22 public static DateTime Absolute_Minute_10 23 { 24 get { return DateTime.Now.AddMinutes(10); } 25 } 26 27 public static DateTime Absolute_Minute_30 28 { 29 get { return DateTime.Now.AddMinutes(30); } 30 } 31 32 public static DateTime Absolute_Hour_1 33 { 34 get { return DateTime.Now.AddHours(1); } 35 } 36 37 public static DateTime Absolute_Hour_2 38 { 39 get { return DateTime.Now.AddHours(2); } 40 } 41 42 public static DateTime Absolute_Hour_5 43 { 44 get { return DateTime.Now.AddHours(5); } 45 } 46 47 public static DateTime Absolute_Hour_12 48 { 49 get { return DateTime.Now.AddHours(12); } 50 } 51 52 public static DateTime Absolute_Day_1 53 { 54 get { return DateTime.Now.AddDays(1); } 55 } 56 57 public static DateTime Absolute_Day_7 58 { 59 get { return DateTime.Now.AddDays(7); } 60 } 61 62 public static DateTime Absolute_Day_14 63 { 64 get { return DateTime.Now.AddDays(14); } 65 } 66 67 public static DateTime Absolute_Day_15 68 { 69 get { return DateTime.Now.AddDays(15); } 70 } 71 72 public static DateTime Absolute_Month_1 73 { 74 get { return DateTime.Now.AddMonths(1); } 75 } 76 #endregion 77 78 #region // 滑动缓存过期时间 79 public static TimeSpan Sliding_Minute_1 80 { 81 get { return new TimeSpan(TimeSpan.TicksPerMinute); } 82 } 83 84 public static TimeSpan Sliding_Minute_10 85 { 86 get { return new TimeSpan(TimeSpan.TicksPerMinute * 10); } 87 } 88 89 public static TimeSpan Sliding_Minute_30 90 { 91 get { return new TimeSpan(TimeSpan.TicksPerMinute * 30); } 92 } 93 94 public static TimeSpan Sliding_Hour_1 95 { 96 get { return new TimeSpan(TimeSpan.TicksPerHour); } 97 } 98 99 public static TimeSpan Sliding_Hour_2 100 { 101 get { return new TimeSpan(TimeSpan.TicksPerHour * 2); } 102 } 103 104 public static TimeSpan Sliding_Hour_5 105 { 106 get { return new TimeSpan(TimeSpan.TicksPerHour * 5); } 107 } 108 109 public static TimeSpan Sliding_Hour_12 110 { 111 get { return new TimeSpan(TimeSpan.TicksPerHour * 12); } 112 } 113 114 public static TimeSpan Sliding_Day_1 115 { 116 get { return new TimeSpan(TimeSpan.TicksPerDay); } 117 } 118 #endregion 119 120 /// <summary> 121 /// 缓存 122 /// </summary> 123 private static Cache cache = HttpRuntime.Cache; 124 125 /// <summary> 126 /// 根据键获取缓存数据 127 /// </summary> 128 /// <param name="cacheKey">缓存的键</param> 129 /// <returns></returns> 130 private static object GetCache(string cacheKey) 131 { 132 return cache.Get(cacheKey); 133 } 134 135 /// <summary> 136 /// 设置缓存 137 /// </summary> 138 /// <param name="cacheKey">缓存的键</param> 139 /// <param name="objValue">缓存的值</param> 140 private static void SetCache(string cacheKey, object objValue) 141 { 142 cache.Insert(cacheKey, objValue); 143 } 144 145 /// <summary> 146 /// 设置缓存 147 /// </summary> 148 /// <param name="cacheKey">缓存的键</param> 149 /// <param name="objValue">缓存的值</param> 150 /// <param name="slidingExpiration">滑动过期时间</param> 151 private static void SetCache(string cacheKey, object objValue, TimeSpan slidingExpiration) 152 { 153 cache.Insert(cacheKey, objValue, null, Cache.NoAbsoluteExpiration, slidingExpiration); 154 } 155 156 /// <summary> 157 /// 设置缓存 158 /// </summary> 159 /// <param name="cacheKey">缓存的键</param> 160 /// <param name="objValue">缓存的值</param> 161 /// <param name="absoluteExpiration">绝对过期时间</param> 162 private static void SetCache(string cacheKey, object objValue, DateTime absoluteExpiration) 163 { 164 cache.Insert(cacheKey, objValue, null, absoluteExpiration, Cache.NoSlidingExpiration); 165 } 166 167 /// <summary> 168 /// 设置缓存 169 /// </summary> 170 /// <param name="cacheKey">缓存的键</param> 171 /// <param name="objValue">缓存的值</param> 172 /// <param name="dependency">文件依赖</param> 173 private static void SetCache(string cacheKey, object objValue, CacheDependency dependency) 174 { 175 cache.Insert(cacheKey, objValue, dependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration); 176 } 177 178 /// <summary> 179 /// 移除指定的缓存 180 /// </summary> 181 /// <param name="cacheKey">缓存的键</param> 182 public static void Remove(string cacheKey) 183 { 184 cache.Remove(cacheKey); 185 } 186 187 /// <summary> 188 /// 移除全部缓存 189 /// </summary> 190 public static void Remove() 191 { 192 IDictionaryEnumerator CacheEnum = cache.GetEnumerator(); 193 while (CacheEnum.MoveNext()) 194 { 195 Remove(CacheEnum.Key.ToString()); 196 } 197 } 198 199 /// <summary> 200 /// 删除以cacheKeyPrefix为前缀的缓存Key的缓存 201 /// </summary> 202 /// <param name="cacheKeyPrefix">缓存键前缀</param> 203 public static void RemoveByKeyStartsWith(string cacheKeyPrefix) 204 { 205 IDictionaryEnumerator CacheEnum = cache.GetEnumerator(); 206 while (CacheEnum.MoveNext()) 207 { 208 var key = CacheEnum.Key.ToString(); 209 if (key != null && key.StartsWith(cacheKeyPrefix)) 210 { 211 Remove(key); 212 } 213 } 214 } 215 216 /// <summary> 217 /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。 218 /// </summary> 219 /// <typeparam name="T">缓存的数据类型</typeparam> 220 /// <param name="cacheKey">缓存的键</param> 221 /// <param name="getData">回调方法</param> 222 /// <returns>缓存中的数据</returns> 223 public static T Get<T>(string cacheKey, Func<T> getData) 224 { 225 var data = GetCache(cacheKey); 226 if (data == null) 227 { 228 data = getData(); 229 SetCache(cacheKey, data); 230 } 231 return (T)data; 232 } 233 234 /// <summary> 235 /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。 236 /// </summary> 237 /// <typeparam name="T">缓存的数据类型</typeparam> 238 /// <param name="cacheKey">缓存的键</param> 239 /// <param name="slidingExpiration">滑动过期时间</param> 240 /// <param name="getData">回调方法</param> 241 /// <returns>缓存中的数据</returns> 242 public static T Get<T>(string cacheKey, TimeSpan slidingExpiration, Func<T> getData) 243 { 244 var data = GetCache(cacheKey); 245 if (data == null) 246 { 247 data = getData(); 248 SetCache(cacheKey, data, slidingExpiration); 249 } 250 return (T)data; 251 } 252 253 /// <summary> 254 /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。 255 /// </summary> 256 /// <typeparam name="T">缓存的数据类型</typeparam> 257 /// <param name="cacheKey">缓存的键</param> 258 /// <param name="absoluteExpiration">绝对过期时间</param> 259 /// <param name="getData">回调方法</param> 260 /// <returns>缓存中的数据</returns> 261 public static T Get<T>(string cacheKey, DateTime absoluteExpiration, Func<T> getData) 262 { 263 var data = GetCache(cacheKey); 264 if (data == null) 265 { 266 data = getData(); 267 SetCache(cacheKey, data, absoluteExpiration); 268 } 269 return (T)data; 270 } 271 272 /// <summary> 273 /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。 274 /// </summary> 275 /// <typeparam name="T">缓存的数据类型</typeparam> 276 /// <param name="cacheKey">缓存的键</param> 277 /// <param name="dependency">文件依赖</param> 278 /// <param name="getData">回调方法</param> 279 /// <returns>缓存中的数据</returns> 280 public static T Get<T>(string cacheKey, CacheDependency dependency, Func<T> getData) 281 { 282 var data = GetCache(cacheKey); 283 if (data == null) 284 { 285 data = getData(); 286 SetCache(cacheKey, data, dependency); 287 } 288 return (T)data; 289 } 290 291 /// <summary> 292 /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。 293 /// </summary> 294 /// <typeparam name="T">缓存的数据类型</typeparam> 295 /// <param name="cacheKey">缓存的键</param> 296 /// <param name="filename">依赖的文件路径</param> 297 /// <param name="getData">回调方法</param> 298 /// <returns>缓存中的数据</returns> 299 public static T Get<T>(string cacheKey, string filename, Func<T> getData) 300 { 301 return Get<T>(cacheKey, new CacheDependency(filename), getData); 302 } 303 304 /// <summary> 305 /// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。 306 /// </summary> 307 /// <typeparam name="T">缓存的数据类型</typeparam> 308 /// <param name="cacheKey">缓存的键</param> 309 /// <param name="filenames">依赖的文件路径</param> 310 /// <param name="getData">回调方法</param> 311 /// <returns>缓存中的数据</returns> 312 public static T Get<T>(string cacheKey, string[] filenames, Func<T> getData) 313 { 314 return Get<T>(cacheKey, new CacheDependency(filenames), getData); 315 } 316 } 317 }