首页 > 代码库 > 【缓存】.net中Cache管理操作

【缓存】.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.     #region 构造函数
  15.     static SetCache()
  16.     {
  17.         hourfactor = 3600;
  18.         _cache = HttpRuntime.Cache;
  19.     }
  20.     private SetCache()
  21.     {
  22.     }
  23.     #endregion
  24.     #region 清除所有缓存
  25.     /// <summary>
  26.     /// 清除所有缓存
  27.     /// </summary>
  28.     public static void Clear()
  29.     {
  30.         //要循环访问 Cache 对象的枚举数
  31.         IDictionaryEnumerator enumerator = _cache.GetEnumerator();//检索用于循环访问包含在缓存中的键设置及其值的字典枚举数
  32.         if (enumerator != null)
  33.         {
  34.             while (enumerator.MoveNext())
  35.             {
  36.                 _cache.Remove(enumerator.Key.ToString());
  37.             }
  38.         }
  39.     }
  40.     #endregion
  41.     #region 得到缓存实例
  42.     /// <summary>
  43.     /// 得到缓存实例
  44.     /// </summary>
  45.     /// <param name="key">缓存实例名称</param>
  46.     /// <returns>返回缓存实例</returns>
  47.     public static object GetCache(string key)
  48.     {
  49.         return _cache[key];
  50.     }
  51.     #endregion
  52.     #region 缓存实例插入
  53.     /// <summary>
  54.     /// 缓存实例插入(默认缓存20分钟)
  55.     /// </summary>
  56.     /// <param name="key">缓存实例名称</param>
  57.     /// <param name="obj">要缓存的对象</param>
  58.     public static void Insert(string key, object obj)
  59.     {
  60.         CacheDependency dep = null;
  61.         Insert(key, obj, dep, 20);
  62.     }
  63.     /// <summary>
  64.     /// 缓存实例插入
  65.     /// </summary>
  66.     /// <param name="key">缓存实例名称</param>
  67.     /// <param name="obj">要缓存的对象</param>
  68.     /// <param name="seconds">缓存的时间</param>
  69.     public static void Insert(string key, object obj, int seconds)
  70.     {
  71.         CacheDependency dep = null;
  72.         Insert(key, obj, dep, seconds);
  73.     }
  74.     /// <summary>
  75.     /// 缓存实例插入(缓存过期时间是一天)
  76.     /// </summary>
  77.     /// <param name="key">缓存实例名称</param>
  78.     /// <param name="obj">要缓存的对象</param>
  79.     /// <param name="dep">缓存的依赖项</param>
  80.     public static void Insert(string key, object obj, CacheDependency dep)
  81.     {
  82.         Insert(key, obj, dep, hourfactor * 12);
  83.     }
  84.     /// <summary>
  85.     /// 缓存实例插入(缓存过期时间是一天)
  86.     /// </summary>
  87.     /// <param name="key">缓存实例名称</param>
  88.     /// <param name="obj">要缓存的对象</param>
  89.     /// <param name="xmlPath">缓存的依赖项xml文件的路径(绝对路径)</param>
  90.     public static void Insert(string key, object obj, string xmlPath)
  91.     {
  92.         CacheDependency dep = new CacheDependency(xmlPath);
  93.         Insert(key, obj, dep, hourfactor * 12);
  94.     }
  95.     /// <summary>
  96.     /// 缓存实例插入
  97.     /// </summary>
  98.     /// <param name="key">缓存实例名称</param>
  99.     /// <param name="obj">要缓存的对象<</param>
  100.     /// <param name="seconds">缓存时间</param>
  101.     /// <param name="priority">该对象相对于缓存中存储的其他项的成本</param>
  102.     public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
  103.     {
  104.         Insert(key, obj, null, seconds, priority);
  105.     }
  106.     /// <summary>
  107.     /// 缓存实例插入
  108.     /// </summary>
  109.     /// <param name="key">用于引用该对象的缓存键</param>
  110.     /// <param name="obj">要插入缓存中的对象</param>
  111.     /// <param name="dep">该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含空引用(Visual Basic 中为 Nothing)</param>
  112.     /// <param name="seconds">所插入对象将过期并被从缓存中移除的时间。</param>
  113.     public static void Insert(string key, object obj, CacheDependency dep, int seconds)
  114.     {
  115.         Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
  116.     }
  117.     /// <summary>
  118.     /// 缓存实例插入
  119.     /// </summary>
  120.     /// <param name="key">用于引用该对象的缓存键</param>
  121.     /// <param name="obj">要插入缓存中的对象</param>
  122.     /// <param name="xmlPath">缓存的依赖项xml文件的路径(绝对路径)</param>
  123.     /// <param name="seconds">所插入对象将过期并被从缓存中移除的时间。</param>
  124.     public static void Insert(string key, object obj, string xmlPath, int seconds)
  125.     {
  126.         CacheDependency dep = new CacheDependency(xmlPath);
  127.         Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
  128.     }
  129.     /// <summary>
  130.     /// 缓存实例插入
  131.     /// </summary>
  132.     /// <param name="key">用于引用该对象的缓存键</param>
  133.     /// <param name="obj">要插入缓存中的对象</param>
  134.     /// <param name="dep">该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含空引用(Visual Basic 中为 Nothing)。</param>
  135.     /// <param name="seconds">所插入对象将过期并被从缓存中移除的时间。</param>
  136.     /// <param name="priority">该对象相对于缓存中存储的其他项的成本,由 CacheItemPriority 枚举表示。该值由缓存在退出对象时使用;具有较低成本的对象在具有较高成本的对象之前被从缓存移除。 </param>
  137.     public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
  138.     {
  139.         if (obj != null)
  140.         {
  141.             _cache.Insert(key, obj, dep, DateTime.Now.AddSeconds((double)seconds), TimeSpan.Zero, priority, null);
  142.         }
  143.     }
  144.     #endregion
  145.     #region 移出单个缓存
  146.     /// <summary>
  147.     /// 移出单个缓存
  148.     /// </summary>
  149.     /// <param name="key">缓存实例名称</param>
  150.     public static void Remove(string key)
  151.     {
  152.         _cache.Remove(key);
  153.     }
  154.     #endregion
  155.     #region 得到所有使用的Cache键值
  156.     /// <summary>
  157.     /// 得到所有使用的Cache键值
  158.     /// </summary>
  159.     /// <returns>返回所有的Cache键值</returns>
  160.     public static ArrayList GetAllCacheKey()
  161.     {
  162.         ArrayList arrList = new ArrayList();
  163.         IDictionaryEnumerator enumerator = _cache.GetEnumerator();
  164.         if (enumerator != null)
  165.         {
  166.             while (enumerator.MoveNext())
  167.             {
  168.                 arrList.Add(enumerator.Key);
  169.             }
  170.         }
  171.         return arrList;
  172.     }
  173.     #endregion
  174. }
<style type="text/css"> .src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px} .src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px} .src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden} .toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;} .src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif} .src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important} .number_show{ padding-left:52px !important; list-style:decimal outside !important} .number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd} .number_hide{ padding-left:0px !important; list-style-type:none !important} .number_hide li{ list-style-type:none !important; border-left:0px} ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd} ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important} .linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word} ol.mainarea li pre.alt{ background-color:#f7f7ff !important} </style><script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i<trElements.length;++i){if(key.parentElement.parentElement.parentElement==trElements[i].parentElement.parentElement){codeElement=trElements[i];break}}if(codeElement!=null){var content=codeElement.innerText;if(window.clipboardData=http://www.mamicode.com/=null){window.alert("您的浏览器不支持脚本复制,请尝试手动复制。")}else{window.clipboardData.setData("Text",content);window.alert("源代码已经复制到剪贴板上。")}}}function LineNumberVisible(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i<trElements.length;++i){if(key.parentElement.parentElement.parentElement==trElements[i].parentElement.parentElement){codeElement=trElements[i];break}}if(codeElement!=null){if(codeElement.className=="mainarea number_hide"){codeElement.className="mainarea number_show";key.innerText="隐藏行号"}else{codeElement.className="mainarea number_hide";key.innerText="显示行号"}}}function ChangeIcon(key,isHover){if(isHover)key.style.color=‘red‘;else key.style.color=‘blue‘}function CopyCode_CheckKey(key){if(window.event.keyCode==13)CopyCode(key)}function AboutMe(){window.alert("本代码框由 CodePaste for Windows Live Writer 生成。\r\n\r\nAuthor: 范传根\r\nEmail: chuangen@live.cn\r\nWebsite: http://chuangen.name\r\nBlog: http://blog.csdn.net/chuangen");} </script>

【缓存】.net中Cache管理操作