ASP.NET 使用HttpContext.Current.Cache
转自:http://dev.mjxy.cn/a-401.aspx
public abstract class BizObject
{
protected const int MAXROWS = int.MaxValue-1;
protected static Cache Cache
{
get { return HttpContext.Current.Cache; }
}
//Cache Data
protected static void CacheData(string key, object data)
{
if (Settings.EnableCaching && data != null)
{
Cache.Insert(key, data, null, DateTime.Now.AddSeconds(Settings.CacheDuration), TimeSpan.Zero);
}
}
/// <summary>
/// 清除带prefix前缀的缓存
/// </summary>
protected static void PurgeCacheItems(string prefix)
{
prefix = prefix.ToLower();
List<string> itemsToRemove = new List<string>();
IDictionaryEnumerator enumerator = BizObject.Cache.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Key.ToString().ToLower().StartsWith(prefix))
itemsToRemove.Add(enumerator.Key.ToString());
}
foreach (string itemToRemove in itemsToRemove)
BizObject.Cache.Remove(itemToRemove);
}