【缓存】.net中Cache管理操作
using System;
using System.Web;
using System.Web.Caching;
using System.Collections;
///
/// 设置Cache操作类
///
public class SetCache
{
#region 用户自定义变量
private static readonly Cache _cache;//缓存实例
private static readonly int hourfactor;
#endregion
#region 构造函数
static SetCache()
{
hourfactor = 3600;
_cache = HttpRuntime.Cache;
}
private SetCache()
{
}
#endregion
#region 清除所有缓存
///
/// 清除所有缓存
///
public static void Clear()
{
//要循环访问 Cache 对象的枚举数
IDictionaryEnumerator enumerator = _cache.GetEnumerator();//检索用于循环访问包含在缓存中的键设置及其值的字典枚举数
if (enumerator != null)
{
while (enumerator.MoveNext())
{
_cache.Remove(enumerator.Key.ToString());
}
}
}
#endregion
#region 得到缓存实例
///
/// 得到缓存实例
///
/// 缓存实例名称
///
返回缓存实例 public static object GetCache(string key)
{
return _cache[key];
}
#endregion
#region 缓存实例插入
///
/// 缓存实例插入(默认缓存20分钟)
///
/// 缓存实例名称
/// 要缓存的对象
public static void Insert(string key, object obj)
{
CacheDependency dep = null;
Insert(key, obj, dep, 20);
}
///
/// 缓存实例插入
///
/// 缓存实例名称
/// 要缓存的对象
/// 缓存的时间
public static void Insert(string key, object obj, int seconds)
{
CacheDependency dep = null;
Insert(key, obj, dep, seconds);
}
///
/// 缓存实例插入(缓存过期时间是一天)
///
/// 缓存实例名称
/// 要缓存的对象
/// 缓存的依赖项
public static void Insert(string key, object obj, CacheDependency dep)
{
Insert(key, obj, dep, hourfactor * 12);
}
///
/// 缓存实例插入(缓存过期时间是一天)
///
/// 缓存实例名称
/// 要缓存的对象
/// 缓存的依赖项xml文件的路径(绝对路径)
public static void Insert(string key, object obj, string xmlPath)
{
CacheDependency dep = new CacheDependency(xmlPath);
Insert(key, obj, dep, hourfactor * 12);
}
///
/// 缓存实例插入
///
/// 缓存实例名称
/// 要缓存的对象<
/// 缓存时间
/// 该对象相对于缓存中存储的其他项的成本
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
{
Insert(key, obj, null, seconds, priority);
}
///
/// 缓存实例插入
///
/// 用于引用该对象的缓存键
/// 要插入缓存中的对象
/// 该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含空引用(Visual Basic 中为 Nothing)
/// 所插入对象将过期并被从缓存中移除的时间。
public static void Insert(string key, object obj, CacheDependency dep, int seconds)
{
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
}
///
/// 缓存实例插入
///
/// 用于引用该对象的缓存键
/// 要插入缓存中的对象
/// 缓存的依赖项xml文件的路径(绝对路径)
/// 所插入对象将过期并被从缓存中移除的时间。
public static void Insert(string key, object obj, string xmlPath, int seconds)
{
CacheDependency dep = new CacheDependency(xmlPath);
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
}
///
/// 缓存实例插入
///
/// 用于引用该对象的缓存键
/// 要插入缓存中的对象
/// 该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含空引用(Visual Basic 中为 Nothing)。
/// 所插入对象将过期并被从缓存中移除的时间。
/// 该对象相对于缓存中存储的其他项的成本,由 CacheItemPriority 枚举表示。该值由缓存在退出对象时使用;具有较低成本的对象在具有较高成本的对象之前被从缓存移除。
public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
{
if (obj != null)
{
_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double)seconds), TimeSpan.Zero, priority, null);
}
}
#endregion
#region 移出单个缓存
///
/// 移出单个缓存
///
/// 缓存实例名称
public static void Remove(string key)
{
_cache.Remove(key);
}
#endregion
#region 得到所有使用的Cache键值
///
/// 得到所有使用的Cache键值
///
///
返回所有的Cache键值 public static ArrayList GetAllCacheKey()
{
ArrayList arrList = new ArrayList();
IDictionaryEnumerator enumerator = _cache.GetEnumerator();
if (enumerator != null)
{
while (enumerator.MoveNext())
{
arrList.Add(enumerator.Key);
}
}
return arrList;
}
#endregion
}
posted on
2015-01-09 00:13 v.e.n.u.s 阅读(
...) 评论(
...) 编辑 收藏