web站点中缓存的重要性毋庸置疑,我想很多网站开发人员在开发web应用系统的时候优先考虑使用的缓存并不是第三方缓存解决方案(比如 分布式缓存memcached、redis等等),而应该是.net framework已经提供的缓存解决方案。
这里分享个人整理的网站缓存管理类,虽然没有第三方缓存方案那么的完整和高可用,但是易用性、轻量级的优势显而易见。
1、网站缓存开关,当前的缓存是否可用一键管理,可以通过配置文件管理
2、检查缓存中是否存在指定的键
3、检查系统中是否存在指定的缓存
4、从缓存中获取指定键的值
5、获取缓存中键值的数量
6、添加缓存
7、返回指定的缓存
8、移除键中某关键字的缓存并返回相应的值
9、移除键中所有的缓存
10、对缓存优先级做一个默认的转换
private static readonly object lockObj = new object();
///
/// 当前的缓存是否可用
///
private bool enable = false;
///
/// 默认实例
///
private static WebCache instance = null;
///
/// 返回默认WebCache缓存实例
///
/// 是否可用最好放到配置项里配置下
public static WebCache GetCacheService(bool enable)
{if (instance == null){lock (lockObj){if (instance == null){instance = new WebCache(enable);}}}return instance;
}
///
/// 构造方法
///
private WebCache(bool enable)
{this.enable = enable;
}
///
/// 获取一个值,指示当前的缓存是否可用
///
public bool EnableCache
{get{return this.enable;}
}
///
/// 检查缓存中是否存在指定的键
///
/// 要检查的键
/// 返回一个值,指示检查的键是否存在
public bool Contains(string key)
{if (this.enable){return HttpRuntime.Cache[key] != null;}return false;
}
///
/// 检查系统中是否存在指定的缓存
///
/// 类型
/// 缓存key
/// 返回这个类型的值是否存在
public bool Contains(string key)
{object value = HttpRuntime.Cache[key];if (value is T){return true;}return false;
}
///
/// 从缓存中获取指定键的值
///
/// 要获取的键
/// 返回指定键的值
public T Get(string key)
{if (this.enable){return (T)HttpRuntime.Cache[key];}return default(T);
}
///
/// 获取缓存中键值的数量
///
public int Count
{get{if (this.enable){return HttpRuntime.Cache.Count;}return 0;}
}
///
/// 添加缓存
///
/// 关键字
/// 缓存值
public void Add(string key, T value)
{if (this.enable){HttpRuntime.Cache.Insert(key, value);}return;
}
///
/// 添加缓存
///
/// 关键字
/// 缓存值
/// 过期时间
public void Add(string key, T value, DateTime absoluteExpiration)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, absoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);}return;
}
///
/// 添加缓存
///
/// 关键字
/// 缓存值
/// 保存时间
public void Add(string key, T value, TimeSpan slidingExpiration)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpiration);}return;
}
///
/// 添加缓存
///
/// 关键字
/// 缓存值
/// 保存时间(分钟)
public void Add(string key, T value, int minutes)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, minutes, 0));}return;
}
///
/// 添加缓存
///
/// 关键字
/// 缓存值
/// 优先级
/// 保存时间
public void Add(string key, T value, CachePriority priority, TimeSpan slidingExpiration)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, slidingExpiration, CacheItemPriorityConvert(priority), null);}return;
}
///
/// 添加缓存
///
/// 关键字
/// 缓存值
/// 优先级
/// 过期时间
public void Add(string key, T value, CachePriority priority, DateTime absoluteExpiration)
{if (this.enable){HttpRuntime.Cache.Insert(key, value, null, absoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriorityConvert(priority), null);}return;
}
///
/// 尝试返回指定的缓存
///
/// 缓存内容的类型
/// 缓存的key
/// 缓存的内容
/// 是否存在这个缓存
public bool TryGetValue(string key, out T value)
{object temp = HttpRuntime.Cache[key];if (temp != null && temp is T){value = (T)temp;return true;}value = default(T);return false;
}
///
/// 移除键中某关键字的缓存并返回相应的值
///
/// 关键字
public void Remove(string key)
{object result = null;if (this.enable){if (HttpRuntime.Cache[key] != null){result = HttpRuntime.Cache.Remove(key);}}return;
}
///
/// 移除键中带某关键字的缓存
///
/// 关键字
public int RemoveContains(string key)
{int result = 0;if (this.enable){System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){if (CacheEnum.Key.ToString().Contains(key)){HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());result++;}}}return result;
}
///
/// 移除键中以某关键字开头的缓存
///
/// 关键字
public int RemoveStartWith(string key)
{int result = 0;if (this.enable){System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){if (CacheEnum.Key.ToString().StartsWith(key)){HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());result++;}}}return result;
}
///
/// 移除键中以某关键字结尾的缓存
///
/// 关键字
public int RemoveEndWith(string key)
{int result = 0;if (this.enable){System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){if (CacheEnum.Key.ToString().EndsWith(key)){HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());result++;}}}return result;
}
///
/// 移除键中所有的缓存
///
public int Clear()
{int result = 0;if (this.enable){System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){HttpRuntime.Cache.Remove(CacheEnum.Key.ToString());result++;}keys.Clear();}return result;
}
private List keys = new List();
///
/// 缓存中所有的键列表
///
public ReadOnlyCollection Keys
{get{if (this.enable){lock (keys){keys.Clear();System.Collections.IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();while (CacheEnum.MoveNext()){keys.Add(CacheEnum.Key.ToString());}}}return new ReadOnlyCollection(keys);}
}
///
/// 对缓存优先级做一个默认的转换
///
/// 原始的优先级
/// 目标优先级
private CacheItemPriority CacheItemPriorityConvert(CachePriority priority)
{CacheItemPriority p = CacheItemPriority.Default;switch (priority){case CachePriority.Low:{p = CacheItemPriority.Low;break;}case CachePriority.Normal:{p = CacheItemPriority.Normal;break;}case CachePriority.High:{p = CacheItemPriority.High;break;}case CachePriority.NotRemovable:{p = CacheItemPriority.NotRemovable;break;}}return p;
}
}