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作为缓存类来使用。
代码如下:
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自带的缓存类的更多相关文章
- Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache
两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...
- 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cache. 我们再用. ...
- 【原】缓存之 HttpRuntime.Cache
1.HttpRuntime.Cache HttpRuntime.Cache 相当于就是一个缓存具体实现类,这个类虽然被放在了 System.Web 命名空间下了.但是非 Web 应用也是可以拿来用的. ...
- .NET使用HttpRuntime.Cache设置程序定时缓存
第一步:判断读取缓存数据 #region 缓存读取 if (HttpRuntime.Cache["App"] != null) { return HttpRuntime.Cache ...
- asp.net 自带的缓存
本文导读:在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序级别的 ...
- HttpContext.Current.Cache 和HttpRuntime.Cache的区别
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache 区别
原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...
- HttpContext.Current.Cache 和 HttpRuntime.Cache
HttpRuntime.Cache:用于winfrom 和 web HttpContext.Current.Cache 用于web .NET中Cache有两种调用方式:HttpContext.Curr ...
- Cache及(HttpRuntime.Cache与HttpContext.Current.Cache)
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/avon520/archive/2009/11/25/4872704.aspx .NET中Cache有两种调用方式:Ht ...
随机推荐
- python爬虫(二)_HTTP的请求和响应
HTTP和HTTPS HTTP(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收HTML页面的方法 HTTPS(HyperText Transfer Prot ...
- jenkins+github持续集成中的坑
1.前言 刚开始开发自己的独立博客的时候,每次发布都要手动打包,上传服务器,杀tomcat进程,重启,来回这么重复性工作,很快就有点不耐烦了.如果能自动化的东西,就绝不要手动了,所以自己搭建了个持续集 ...
- Java中用Apache POI生成excel和word文档
概述: 近期在做项目的过程中遇到了excel的数据导出和word的图文表报告的导出功能.最后决定用Apache POI来完毕该项功能.本文就项目实现过程中的一些思路与代码与大家共享.同一时候.也作为自 ...
- UVA - 11082 Matrix Decompressing(最大流+行列模型)
题目大意:给出一个R行C列的矩阵,如今给出他的前1-R行和 && 前1-C列和,问这个矩阵原来是如何的,要求每一个元素大小在1-20之间 解题思路:将每一行连接到超级源点,容量为该行的 ...
- 掌上快递 APP 项目之概述篇
概述 学习Android开发也有一段时间了,利用业余时间独立制作的一款快递类APP软件.大概2个多星期吧,自己将其定位为"集快递信息追踪.附近快递点查询. 快递公司投诉功能为一体的便民生活类 ...
- POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38980 Acc ...
- leetcode第一刷_Populating Next Right Pointers in Each Node II
很自然的推广,假设去掉全然二叉树的条件呢?由于这个条件不是关键,因此不会影响整体的思路.做法依旧是每次找到一层的起点,然后一层一层的走. 假设是全然二叉树的话,每层的起点就是上一层起点的左孩子,兄弟之 ...
- Java中File的使用
File 代表文件或者目录的类 构造函数 File(File parent,String child)---代表了指定父目录下的指定的子文件或者子目录 File(String pathname)--- ...
- CoreJava逻辑思维-顺时针打印自定义矩阵
CoreJava逻辑思维-顺时针打印自定义矩阵 这两天回顾了一下刚入Java时的一些比较有意思的逻辑题,曾经也费劲脑汁的思考过的一些问题,比如百钱百鸡最简单的算法啦之类的,而今天博主想说的是一个循环打 ...
- myeclipse 2014 customize_Perspective 失效解决方法-有效
1.将9个jar复制到myeclipse安装目录\plugins中 2.删除和这9个jar同包名但是版本号较低的9个文件 3.重启myeclipse 2014 三步走: 到这个地址下载 http:// ...