.net中Cache管理操作

隐藏行号 复制代码 这是一段程序代码。
  1. using System;
    
  2. using System.Web;
    
  3. using System.Web.Caching;
    
  4. using System.Collections;
    
  5. /// <summary>
    
  6. /// 设置Cache操作类
    
  7. /// </summary>
    
  8. public class SetCache
    
  9. {
    
  10.     #region 用户自定义变量
    
  11.     private static readonly Cache _cache;//缓存实例
    
  12.     private static readonly int hourfactor;
    
  13.     #endregion
    
  14. 
    
  15.     #region 构造函数
    
  16.     static SetCache()
    
  17.     {
    
  18.         hourfactor = 3600;
    
  19.         _cache = HttpRuntime.Cache;
    
  20.     }
    
  21.     private SetCache()
    
  22.     {
    
  23.     }
    
  24.     #endregion
    
  25. 
    
  26.     #region 清除所有缓存
    
  27.     /// <summary>
    
  28.     /// 清除所有缓存
    
  29.     /// </summary>
    
  30.     public static void Clear()
    
  31.     {
    
  32.         //要循环访问 Cache 对象的枚举数
    
  33.         IDictionaryEnumerator enumerator = _cache.GetEnumerator();//检索用于循环访问包含在缓存中的键设置及其值的字典枚举数
    
  34.         if (enumerator != null)
    
  35.         {
    
  36.             while (enumerator.MoveNext())
    
  37.             {
    
  38.                 _cache.Remove(enumerator.Key.ToString());
    
  39.             }
    
  40.         }
    
  41.     }
    
  42.     #endregion
    
  43. 
    
  44.     #region 得到缓存实例
    
  45.     /// <summary>
    
  46.     /// 得到缓存实例
    
  47.     /// </summary>
    
  48.     /// <param name="key">缓存实例名称</param>
    
  49.     /// <returns>返回缓存实例</returns>
    
  50.     public static object GetCache(string key)
    
  51.     {
    
  52.         return _cache[key];
    
  53.     }
    
  54.     #endregion
    
  55. 
    
  56.     #region 缓存实例插入
    
  57.     /// <summary>
    
  58.     /// 缓存实例插入(默认缓存20分钟)
    
  59.     /// </summary>
    
  60.     /// <param name="key">缓存实例名称</param>
    
  61.     /// <param name="obj">要缓存的对象</param>
    
  62.     public static void Insert(string key, object obj)
    
  63.     {
    
  64.         CacheDependency dep = null;
    
  65.         Insert(key, obj, dep, 20);
    
  66.     }
    
  67.     /// <summary>
    
  68.     /// 缓存实例插入
    
  69.     /// </summary>
    
  70.     /// <param name="key">缓存实例名称</param>
    
  71.     /// <param name="obj">要缓存的对象</param>
    
  72.     /// <param name="seconds">缓存的时间</param>
    
  73.     public static void Insert(string key, object obj, int seconds)
    
  74.     {
    
  75.         CacheDependency dep = null;
    
  76.         Insert(key, obj, dep, seconds);
    
  77.     }
    
  78.     /// <summary>
    
  79.     /// 缓存实例插入(缓存过期时间是一天)
    
  80.     /// </summary>
    
  81.     /// <param name="key">缓存实例名称</param>
    
  82.     /// <param name="obj">要缓存的对象</param>
    
  83.     /// <param name="dep">缓存的依赖项</param>
    
  84.     public static void Insert(string key, object obj, CacheDependency dep)
    
  85.     {
    
  86.         Insert(key, obj, dep, hourfactor * 12);
    
  87.     }
    
  88.     /// <summary>
    
  89.     /// 缓存实例插入(缓存过期时间是一天)
    
  90.     /// </summary>
    
  91.     /// <param name="key">缓存实例名称</param>
    
  92.     /// <param name="obj">要缓存的对象</param>
    
  93.     /// <param name="xmlPath">缓存的依赖项xml文件的路径(绝对路径)</param>
    
  94.     public static void Insert(string key, object obj, string xmlPath)
    
  95.     {
    
  96.         CacheDependency dep = new CacheDependency(xmlPath);
    
  97.         Insert(key, obj, dep, hourfactor * 12);
    
  98.     }
    
  99.     /// <summary>
    
  100.     /// 缓存实例插入
    
  101.     /// </summary>
    
  102.     /// <param name="key">缓存实例名称</param>
    
  103.     /// <param name="obj">要缓存的对象<</param>
    
  104.     /// <param name="seconds">缓存时间</param>
    
  105.     /// <param name="priority">该对象相对于缓存中存储的其他项的成本</param>
    
  106.     public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
    
  107.     {
    
  108.         Insert(key, obj, null, seconds, priority);
    
  109.     }
    
  110.     /// <summary>
    
  111.     /// 缓存实例插入
    
  112.     /// </summary>
    
  113.     /// <param name="key">用于引用该对象的缓存键</param>
    
  114.     /// <param name="obj">要插入缓存中的对象</param>
    
  115.     /// <param name="dep">该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含空引用(Visual Basic 中为 Nothing)</param>
    
  116.     /// <param name="seconds">所插入对象将过期并被从缓存中移除的时间。</param>
    
  117.     public static void Insert(string key, object obj, CacheDependency dep, int seconds)
    
  118.     {
    
  119.         Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
    
  120.     }
    
  121.     /// <summary>
    
  122.     /// 缓存实例插入
    
  123.     /// </summary>
    
  124.     /// <param name="key">用于引用该对象的缓存键</param>
    
  125.     /// <param name="obj">要插入缓存中的对象</param>
    
  126.     /// <param name="xmlPath">缓存的依赖项xml文件的路径(绝对路径)</param>
    
  127.     /// <param name="seconds">所插入对象将过期并被从缓存中移除的时间。</param>
    
  128.     public static void Insert(string key, object obj, string xmlPath, int seconds)
    
  129.     {
    
  130.         CacheDependency dep = new CacheDependency(xmlPath);
    
  131.         Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
    
  132.     }
    
  133.     /// <summary>
    
  134.     /// 缓存实例插入
    
  135.     /// </summary>
    
  136.     /// <param name="key">用于引用该对象的缓存键</param>
    
  137.     /// <param name="obj">要插入缓存中的对象</param>
    
  138.     /// <param name="dep">该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含空引用(Visual Basic 中为 Nothing)。</param>
    
  139.     /// <param name="seconds">所插入对象将过期并被从缓存中移除的时间。</param>
    
  140.     /// <param name="priority">该对象相对于缓存中存储的其他项的成本,由 CacheItemPriority 枚举表示。该值由缓存在退出对象时使用;具有较低成本的对象在具有较高成本的对象之前被从缓存移除。 </param>
    
  141.     public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
    
  142.     {
    
  143.         if (obj != null)
    
  144.         {
    
  145.             _cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double)seconds), TimeSpan.Zero, priority, null);
    
  146.         }
    
  147.     }
    
  148.     #endregion
    
  149. 
    
  150.     #region 移出单个缓存
    
  151.     /// <summary>
    
  152.     /// 移出单个缓存
    
  153.     /// </summary>
    
  154.     /// <param name="key">缓存实例名称</param>
    
  155.     public static void Remove(string key)
    
  156.     {
    
  157.         _cache.Remove(key);
    
  158.     }
    
  159.     #endregion
    
  160. 
    
  161.     #region 得到所有使用的Cache键值
    
  162.     /// <summary>
    
  163.     /// 得到所有使用的Cache键值
    
  164.     /// </summary>
    
  165.     /// <returns>返回所有的Cache键值</returns>
    
  166.     public static ArrayList GetAllCacheKey()
    
  167.     {
    
  168.         ArrayList arrList = new ArrayList();
    
  169.         IDictionaryEnumerator enumerator = _cache.GetEnumerator();
    
  170.         if (enumerator != null)
    
  171.         {
    
  172.             while (enumerator.MoveNext())
    
  173.             {
    
  174.                 arrList.Add(enumerator.Key);
    
  175.             }
    
  176.         }
    
  177.         return arrList;
    
  178.     }
    
  179.     #endregion
    
  180. }
    
posted @ 2010-10-13 22:14  luckdv  阅读(1816)  评论(0编辑  收藏  举报