.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作为缓存类来使用。

代码如下:

 using System;
 using System.Collections;
 using System.Web;
 using System.Web.Caching;
 /**
 * author:qixiao
 * create2017-6-6 11:54:07
 * */
 namespace QX_Frame.Helper_DG
 {
     public class HttpRuntimeCache_Helper_DG
     {
         /// <summary>
         /// Cache_Get
         /// </summary>
         /// <param name="cacheKey">cacheKey</param>
         /// <returns></returns>
         public static object Cache_Get(string cacheKey)
         {
             return HttpRuntime.Cache[cacheKey];
         }

         #region Cache Add

         /// <summary>
         /// Cache_Add
         /// </summary>
         /// <param name="cacheKey">key</param>
         /// <param name="cacheValue">object value</param>
         /// <param name="keepMinutes"></param>
         /// <param name="dependencies">缓存的依赖项,也就是此项的更改意味着缓存内容已经过期。如果没有依赖项,可将此值设置为NULL。</param>
         /// <param name="cacheItemRemovedCallback">表示缓存删除数据对象时调用的事件,一般用做通知程序。</param>
         /// <returns></returns>
         , CacheDependency dependencies = null, CacheItemRemovedCallback cacheItemRemovedCallback = null)
         {
             HttpRuntime.Cache.Insert(cacheKey, cacheValue, dependencies, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(keepMinutes), CacheItemPriority.NotRemovable, cacheItemRemovedCallback);
             return true;
         }

         /// <summary>
         /// Cache_Add
         /// </summary>
         /// <param name="cacheKey">key</param>
         /// <param name="cacheValue">object value</param>
         /// <param name="keepMinutes"></param>
         /// <param name="dependencies">缓存的依赖项,也就是此项的更改意味着缓存内容已经过期。如果没有依赖项,可将此值设置为NULL。</param>
         /// <param name="cacheItemRemovedCallback">表示缓存删除数据对象时调用的事件,一般用做通知程序。</param>
         /// <returns></returns>
         public static Boolean Cache_Add(string cacheKey, object cacheValue, DateTime expireTime, CacheDependency dependencies = null, CacheItemRemovedCallback cacheItemRemovedCallback = null)
         {
             HttpRuntime.Cache.Insert(cacheKey, cacheValue, dependencies, expireTime, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, cacheItemRemovedCallback);
             return true;
         }

         /// <summary>
         /// Cache_Add
         /// </summary>
         /// <param name="cacheKey">key</param>
         /// <param name="cacheValue">object value</param>
         /// <param name="dependencies">缓存的依赖项,也就是此项的更改意味着缓存内容已经过期。如果没有依赖项,可将此值设置为NULL。</param>
         /// <param name="absoluteExpiration">如果设置slidingExpiration,则该项必须设置为DateTime.MaxValue。是日期型数据,表示缓存过期的时间,.NET 2.0提供的缓存在过期后是可以使用的,能使用多长时间,就看这个参数的设置。</param>
         /// <param name="slidingExpiration">如果设置absoluteExpiration,则该项必须设置为TimeSpan.Zero。表示一段时间间隔,表示缓存参数将在多长时间以后被删除,此参数与absoluteExpiration参数相关联。</param>
         /// <param name="cacheItemPriority">表示撤销缓存的优先值,此参数的值取自枚举变量“CacheItemPriority”,优先级低的数据项将先被删除。此参数主要用在缓存退出对象时。</param>
         /// <param name="cacheItemRemovedCallback">表示缓存删除数据对象时调用的事件,一般用做通知程序。</param>
         //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)
         //{
         //    DateTime absoluteExpirationTime = default(DateTime);
         //    if (!DateTime.TryParse(absoluteExpiration.ToString(), out absoluteExpirationTime) || absoluteExpiration.Equals(default(DateTime)))
         //        absoluteExpirationTime = DateTime.MaxValue;
         //    else
         //        slidingExpiration = TimeSpan.Zero;

         //    TimeSpan slidingExpirationTime = default(TimeSpan);
         //    if (!TimeSpan.TryParse(slidingExpiration.ToString(), out slidingExpirationTime) || slidingExpiration.Equals(default(TimeSpan)))
         //        slidingExpirationTime = TimeSpan.Zero;

         //    HttpRuntime.Cache.Insert(cacheKey, cacheValue, dependencies, absoluteExpirationTime, slidingExpirationTime, cacheItemPriority, cacheItemRemovedCallback);
         //    return true;
         //}

         #endregion

         /// <summary>
         /// Cache_Delete
         /// </summary>
         /// <param name="cacheKey">cacheKey</param>
         public static Boolean Cache_Delete(string cacheKey)
         {
             HttpRuntime.Cache.Remove(cacheKey);
             return true;
         }

         /// <summary>
         /// Cache_DeleteAll
         /// </summary>
         public static Boolean Cache_DeleteAll()
         {
             Cache _cache = HttpRuntime.Cache;
             IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
             while (CacheEnum.MoveNext())
             {
                 _cache.Remove(CacheEnum.Key.ToString());
             }
             return true;
         }

         /// <summary>
         /// Cache Count
         /// </summary>
         public static int CacheCount
         {
             get { return HttpRuntime.Cache.Count; }
         }
     }
 }

HttpRuntime.Cache .Net自带的缓存类的更多相关文章

  1. Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache

    两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...

  2. 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别

    先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cache.  我们再用. ...

  3. 【原】缓存之 HttpRuntime.Cache

    1.HttpRuntime.Cache HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了.但是非 Web 应用也是可以拿来用的. ...

  4. .NET使用HttpRuntime.Cache设置程序定时缓存

    第一步:判断读取缓存数据 #region 缓存读取 if (HttpRuntime.Cache["App"] != null) { return HttpRuntime.Cache ...

  5. asp.net 自带的缓存

    本文导读:在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序级别的 ...

  6. HttpContext.Current.Cache 和HttpRuntime.Cache的区别

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  7. HttpContext.Current.Cache 和 HttpRuntime.Cache 区别

    原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...

  8. HttpContext.Current.Cache 和 HttpRuntime.Cache

    HttpRuntime.Cache:用于winfrom 和 web HttpContext.Current.Cache 用于web .NET中Cache有两种调用方式:HttpContext.Curr ...

  9. Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)

    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx .NET中Cache有两种调用方式:Ht ...

随机推荐

  1. java线程池的创建使用

    利用java的多线程编程可以大大的提高系统的并发运行效率,线程越多并发执行的任务就越多,但是并不意味着效率会一直提高,相反会得到适得其反的效果. java中的多线程编程一共有三种方法: 继承Threa ...

  2. 编译器 STVD 与 IAR 的差别 个人体验

    编译器 STVD 与 IAR 的一些差别,这些事个人的体验,欢迎扔砖和指点或者补充. 1:全局查找功能: STVD:全局查找功能全局查找功能比較麻烦.有3个动作. IAR:有全局查找功能比較方便.仅仅 ...

  3. SSH2——filter过滤器

    概述: 过滤器是Servlet2.3以上新添加的一个功能,其技术也是很强大的.通过Filter技术能够对WEBserver的文件进行拦截,从而实现一些特殊的功能. 在JSP开发应用中也是必备的技能之中 ...

  4. 【剑指offer】扑克牌的顺子

    个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想測測自己的手气,看看能不能抽到顺子,假设抽到的话,他决定去买体育彩票,嘿嘿! ."红心A,黑桃3,小王,大王,方片 ...

  5. ITM事件直接接收并解析

    之前在实施一个监控项目时.客户由于买了IBM的小机.当前就赠送了TIVOLI的系统监控软件一套,客户也在他们的生产环境中部署了ITM的监控.由于没有购买IBM的netcool,无法集中管理告警事件,请 ...

  6. 自学Zabbix3.6.2-触发器triggers severity严重程度

    触发器严重性定义了触发器的重要性. 1. zabbix支持以下触发级别: SEVERITY DEFINITION 颜色 Not classified 未知. 灰色 Information 一般信息. ...

  7. org.elasticsearch.transport.ReceiveTimeoutTransportException[cluster:monitor/nodes/liveness] request_id [31] timed out after [5000ms]

    ES连接超时,异常信息 2017-09-07 10:42:45.042 [elasticsearch[Bantam][transport_client_worker][T#17]{New I/O wo ...

  8. 《Java NIO (中文版)》【PDF】下载

    <Java NIO (中文版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062530 NIO (中文版)>[PDF]& ...

  9. iOS 去掉小数点后边多余的0

    -(NSString*)removeFloatAllZero:(NSString*)string { NSString * testNumber = string; NSString * outNum ...

  10. kendo ui grid选中行事件,获取combobox选择的值

    背景: 以前用 telerik ui做的grid现在又要换成kendo ui,不过说句实话kendo ui真的比telerik好多,可以说超级升级改头换面.当然用的mvc的辅助方法,以前的teleri ...