标签:read from for stat space 固定 regex insert core
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; using System.Web.Caching; namespace WebCore.UI.Common { public class Cache { private static System.Web.Caching.Cache _cache; static Cache() { HttpContext current = HttpContext.Current; if (current != null) { _cache = current.Cache; } else { _cache = HttpRuntime.Cache; } } private Cache() { } /// <summary> /// 清空 /// </summary> public static void Clear() { IDictionaryEnumerator enumerator = _cache.GetEnumerator(); ArrayList list = new ArrayList(); while (enumerator.MoveNext()) { list.Add(enumerator.Key); } foreach (string str in list) { _cache.Remove(str); } } /// <summary> /// 获取缓存 /// </summary> /// <param name="key">key</param> /// <returns></returns> public static object Get(string key) { return _cache[key]; } /// <summary> /// 移除缓存 /// </summary> /// <param name="key"></param> public static void Remove(string key) { _cache.Remove(key); } public static void RemoveByPattern(string pattern) { IDictionaryEnumerator enumerator = _cache.GetEnumerator(); Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase); while (enumerator.MoveNext()) { if (regex.IsMatch(enumerator.Key.ToString())) { _cache.Remove(enumerator.Key.ToString()); } } } /// <summary> /// 固定过期时间 /// </summary> /// <param name="key"></param> /// <param name="obj"></param> /// <param name="seconds"></param> /// <param name="dep"></param> /// <param name="absoluteExpiration">DateTime.Now</param> public static void Insert(string key, object obj, int seconds, CacheDependency dep, DateTime absoluteExpiration) { if (absoluteExpiration == null) { throw new NullReferenceException("AbsoluteExpiration不能为空"); } if (seconds < 0) { throw new NullReferenceException("seconds 需大于0"); } Insert(key, obj, dep, seconds, absoluteExpiration, null, CacheItemPriority.Default, null); } /// <summary> /// 滑动过期时间 /// </summary> /// <param name="key"></param> /// <param name="obj"></param> /// <param name="seconds"></param> /// <param name="dep"></param> /// <param name="slidingExpiration">TimeSpan.FromMinutes(1)</param> public static void Insert(string key, object obj, CacheDependency dep, TimeSpan slidingExpiration) { if (slidingExpiration == null) { throw new NullReferenceException("SlidingExpiration不能为空"); } Insert(key, obj, dep, 0, null, slidingExpiration, CacheItemPriority.Default, null); } public static void Insert(string key, object obj, CacheDependency dep, int seconds, DateTime? absoluteExpiration, TimeSpan? slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback cacheItemRemovedCallback) { if (string.IsNullOrEmpty(key)) { throw new NullReferenceException("key 不能为空"); } if (obj == null) { throw new NullReferenceException("obj 不能为空"); } if (slidingExpiration.HasValue) { Insert(key, obj, dep, slidingExpiration.Value, priority, cacheItemRemovedCallback); } else { Insert(key, obj, dep, seconds, absoluteExpiration.Value, priority, cacheItemRemovedCallback); } } /// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="obj"></param> /// <param name="dep"></param> /// <param name="seconds"></param> /// <param name="absoluteExpiration">absoluteExpiration.AddSeconds(seconds)</param> /// <param name="priority"></param> /// <param name="cacheItemRemovedCallback"></param> private static void Insert(string key, object obj, CacheDependency dep, int seconds, DateTime absoluteExpiration, CacheItemPriority priority, CacheItemRemovedCallback cacheItemRemovedCallback) { if (obj != null) { _cache.Insert(key, obj, dep, absoluteExpiration.AddSeconds(seconds), TimeSpan.Zero, priority, cacheItemRemovedCallback); } } /// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="obj"></param> /// <param name="dep"></param> /// <param name="slidingExpiration">TimeSpan.FromMinutes(1)</param> /// <param name="priority"></param> /// <param name="cacheItemRemovedCallback"></param> private static void Insert(string key, object obj, CacheDependency dep, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback cacheItemRemovedCallback) { if (obj != null) { _cache.Insert(key, obj, dep, System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpiration, priority, cacheItemRemovedCallback); } } } }
做了下网站栏目的服务器缓存,方法分两种,指定过期时间、滑动过期时间,方法不复杂。不是很完善,先记下来,以后再修补吧。
暂且记录几个不合理的地方:
(1)滑动缓存的时效 slidingExpiration 滑动 TimeSpan.FromMinutes(1) 一分钟
(2)absoluteExpiration 为了灵活,先传入 DateTime, 然后 DateTime.Now.AddSeconds。是以当前时间 N 秒后作为过期时间
标签:read from for stat space 固定 regex insert core
原文地址:http://www.cnblogs.com/CodeHelper/p/6344293.html