zl程序教程

您现在的位置是:首页 >  后端

当前栏目

asp.net下Cache缓存操作类代码

NetASP缓存代码 操作 cache
2023-06-13 09:14:27 时间
复制代码代码如下:

usingSystem.Collections.Generic;
usingSystem.Web;
usingSystem;
namespaceDataAccess
{
///<summary>
///缓存控制类
///</summary>
publicclassCacheControl
{
publicstaticList<string>AllUseCacheKey=newList<string>();
///<summary>
///添加缓存
///</summary>
///<paramname="key"></param>
///<paramname="value"></param>
///<paramname="absoluteExpiration"></param>
publicstaticvoidAddCache(stringkey,objectvalue,DateTimeabsoluteExpiration)
{
if(!AllUseCacheKey.Contains(key))
{
AllUseCacheKey.Add(key);
}
HttpContext.Current.Cache.Add(key,value,null,absoluteExpiration,TimeSpan.Zero,System.Web.Caching.CacheItemPriority.Normal,null);
}
///<summary>
///移除缓存
///</summary>
///<paramname="key"></param>
publicstaticvoidRemoveCache(stringkey)
{
if(AllUseCacheKey.Contains(key))
{
AllUseCacheKey.Remove(key);
}
HttpContext.Current.Cache.Remove(key);
}
///<summary>
///清空使用的缓存
///</summary>
publicstaticvoidClearCache()
{
foreach(stringvalueinAllUseCacheKey)
{
HttpContext.Current.Cache.Remove(value);
}
AllUseCacheKey.Clear();
}
}
}