欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-07 23:08:45
复制代码 代码如下: using system.collections.generic; using system.web; using system; namespace...
复制代码 代码如下:

using system.collections.generic;
using system.web;
using system;
namespace dataaccess
{
/// <summary>
/// 缓存控制类
/// </summary>
public class cachecontrol
{
public static list<string> allusecachekey = new list<string>();
/// <summary>
/// 添加缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="absoluteexpiration"></param>
public static void addcache(string key, object value, datetime absoluteexpiration)
{
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>
/// <param name="key"></param>
public static void removecache(string key)
{
if (allusecachekey.contains(key))
{
allusecachekey.remove(key);
}
httpcontext.current.cache.remove(key);
}
/// <summary>
/// 清空使用的缓存
/// </summary>
public static void clearcache()
{
foreach (string value in allusecachekey)
{
httpcontext.current.cache.remove(value);
}
allusecachekey.clear();
}
}
}