zoukankan      html  css  js  c++  java
  • HttpRuntime.Cache .Net自带的缓存类

    .Net自带的缓存有两个,一个是Asp.Net的缓存 HttpContext.Cache,一个是.Net应用程序级别的缓存,HttpRuntime.Cache。

    MSDN上有解释说:

    HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象。
    HttpRuntime.Cache:获取当前应用程序的Cache。 

    通过源码查看可以知悉,HttpContext.Current.Cache调用的竟然也是HttpRuntime.Cache而且HttpContext只能在Web下调用,而HttpRuntimeCache可以在任何应用程序集中使用,因此,这里我直接封装HttpRuntimeCache作为缓存类来使用。

    代码如下:

      1 using System;
      2 using System.Collections;
      3 using System.Web;
      4 using System.Web.Caching;
      5 /**
      6 * author:qixiao
      7 * create2017-6-6 11:54:07
      8 * */
      9 namespace QX_Frame.Helper_DG
     10 {
     11     public class HttpRuntimeCache_Helper_DG
     12     {
     13         /// <summary>
     14         /// Cache_Get
     15         /// </summary>
     16         /// <param name="cacheKey">cacheKey</param>
     17         /// <returns></returns>
     18         public static object Cache_Get(string cacheKey)
     19         {
     20             return HttpRuntime.Cache[cacheKey];
     21         }
     22 
     23         #region Cache Add
     24 
     25         /// <summary>
     26         /// Cache_Add
     27         /// </summary>
     28         /// <param name="cacheKey">key</param>
     29         /// <param name="cacheValue">object value</param>
     30         /// <param name="keepMinutes"></param>
     31         /// <param name="dependencies">缓存的依赖项,也就是此项的更改意味着缓存内容已经过期。如果没有依赖项,可将此值设置为NULL。</param>
     32         /// <param name="cacheItemRemovedCallback">表示缓存删除数据对象时调用的事件,一般用做通知程序。</param>
     33         /// <returns></returns>
     34         public static Boolean Cache_Add(string cacheKey, object cacheValue, int keepMinutes = 10, CacheDependency dependencies = null, CacheItemRemovedCallback cacheItemRemovedCallback = null)
     35         {
     36             HttpRuntime.Cache.Insert(cacheKey, cacheValue, dependencies, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(keepMinutes), CacheItemPriority.NotRemovable, cacheItemRemovedCallback);
     37             return true;
     38         }
     39 
     40         /// <summary>
     41         /// Cache_Add
     42         /// </summary>
     43         /// <param name="cacheKey">key</param>
     44         /// <param name="cacheValue">object value</param>
     45         /// <param name="keepMinutes"></param>
     46         /// <param name="dependencies">缓存的依赖项,也就是此项的更改意味着缓存内容已经过期。如果没有依赖项,可将此值设置为NULL。</param>
     47         /// <param name="cacheItemRemovedCallback">表示缓存删除数据对象时调用的事件,一般用做通知程序。</param>
     48         /// <returns></returns>
     49         public static Boolean Cache_Add(string cacheKey, object cacheValue, DateTime expireTime, CacheDependency dependencies = null, CacheItemRemovedCallback cacheItemRemovedCallback = null)
     50         {
     51             HttpRuntime.Cache.Insert(cacheKey, cacheValue, dependencies, expireTime, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, cacheItemRemovedCallback);
     52             return true;
     53         }
     54 
     55         /// <summary>
     56         /// Cache_Add
     57         /// </summary>
     58         /// <param name="cacheKey">key</param>
     59         /// <param name="cacheValue">object value</param>
     60         /// <param name="dependencies">缓存的依赖项,也就是此项的更改意味着缓存内容已经过期。如果没有依赖项,可将此值设置为NULL。</param>
     61         /// <param name="absoluteExpiration">如果设置slidingExpiration,则该项必须设置为DateTime.MaxValue。是日期型数据,表示缓存过期的时间,.NET 2.0提供的缓存在过期后是可以使用的,能使用多长时间,就看这个参数的设置。</param>
     62         /// <param name="slidingExpiration">如果设置absoluteExpiration,则该项必须设置为TimeSpan.Zero。表示一段时间间隔,表示缓存参数将在多长时间以后被删除,此参数与absoluteExpiration参数相关联。</param>
     63         /// <param name="cacheItemPriority">表示撤销缓存的优先值,此参数的值取自枚举变量“CacheItemPriority”,优先级低的数据项将先被删除。此参数主要用在缓存退出对象时。</param>
     64         /// <param name="cacheItemRemovedCallback">表示缓存删除数据对象时调用的事件,一般用做通知程序。</param>
     65         //public static Boolean Cache_Add(string cacheKey, object cacheValue, CacheDependency dependencies = null, DateTime absoluteExpiration = default(DateTime), TimeSpan slidingExpiration = default(TimeSpan), CacheItemPriority cacheItemPriority = CacheItemPriority.NotRemovable, CacheItemRemovedCallback cacheItemRemovedCallback = null)
     66         //{
     67         //    DateTime absoluteExpirationTime = default(DateTime);
     68         //    if (!DateTime.TryParse(absoluteExpiration.ToString(), out absoluteExpirationTime) || absoluteExpiration.Equals(default(DateTime)))
     69         //        absoluteExpirationTime = DateTime.MaxValue;
     70         //    else
     71         //        slidingExpiration = TimeSpan.Zero;
     72 
     73         //    TimeSpan slidingExpirationTime = default(TimeSpan);
     74         //    if (!TimeSpan.TryParse(slidingExpiration.ToString(), out slidingExpirationTime) || slidingExpiration.Equals(default(TimeSpan)))
     75         //        slidingExpirationTime = TimeSpan.Zero;
     76 
     77         //    HttpRuntime.Cache.Insert(cacheKey, cacheValue, dependencies, absoluteExpirationTime, slidingExpirationTime, cacheItemPriority, cacheItemRemovedCallback);
     78         //    return true;
     79         //}
     80 
     81         #endregion
     82 
     83         /// <summary>
     84         /// Cache_Delete
     85         /// </summary>
     86         /// <param name="cacheKey">cacheKey</param>
     87         public static Boolean Cache_Delete(string cacheKey)
     88         {
     89             HttpRuntime.Cache.Remove(cacheKey);
     90             return true;
     91         }
     92 
     93         /// <summary>
     94         /// Cache_DeleteAll
     95         /// </summary>
     96         public static Boolean Cache_DeleteAll()
     97         {
     98             Cache _cache = HttpRuntime.Cache;
     99             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
    100             while (CacheEnum.MoveNext())
    101             {
    102                 _cache.Remove(CacheEnum.Key.ToString());
    103             }
    104             return true;
    105         }
    106 
    107         /// <summary>
    108         /// Cache Count
    109         /// </summary>
    110         public static int CacheCount
    111         {
    112             get { return HttpRuntime.Cache.Count; }
    113         }
    114     }
    115 }
    本文为七小站主原创作品,转载请注明出处:http://www.cnblogs.com/7tiny/ 且在文章页面明显位置给出原文链接。

    作者信息(详情):

    QiXiao_柒小(東)
    Software Development
    北京市海淀区 Haidian Area Beijing 100089,P.R.China
    郵箱Email : seventiny@foxmail.com  
    網址Http: http://www.7tiny.com
    QQ:1124999434 , WeChat: wd8622088 (尽量加微信)
    (专好结交天下英雄好汉,可聊天,可谈技,可约饭,可..嗯,原则是要有的~) 更多联系方式点我哦~


    Best Regard ~
  • 相关阅读:
    cad.net修改视口比例
    cad.net添加和删除图层过滤器
    cad.net创建新图层
    cad.net创建浮动视口
    根据handle转换成ObjectId
    C#类型转换
    过滤选择集
    文字随线移动
    文字打断
    注册表应用示例
  • 原文地址:https://www.cnblogs.com/7tiny/p/7007503.html
Copyright © 2011-2022 走看看