.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. (一)—Linux安装与硬盘分区

    相信很多人对linux充满了喜爱,尤其是技术人员,玩惯了windows更想尝试一下这个系统.关于它的优点多多,当然,你的好你的坏,用过才明白!下面我这个菜鸟也来玩装个玩玩. 我不会那么冲动,上来就把自 ...

  2. C++教程之autokeyword的使用

    一.autokeyword的前世 从C语言開始,autokeyword就被当作是一个变量的存储类型修饰符,表示自己主动变量(局部变量).它不能被单独使用,否则编译器会给出警告. #include &l ...

  3. Android ListView Adapter的getItemViewType和getViewTypeCount多种布局

     <Android ListView Adapter的getItemViewType和getViewTypeCount多种布局> 在Android的ListView中.假设在一个Lis ...

  4. HDU 4162 Shape Number(字符串,最小表示法)

    HDU 4162 题意: 给一个数字串(length <= 300,000),数字由0~7构成,求出一阶差分码,然后输出与该差分码循环同构的最小字典序差分码. 思路: 第一步是将差分码求出:s[ ...

  5. Office Add-in 架构和入门

    作者:陈希章 发表于2017年7月12日 前言 从2月26日开始写这个Office 365开发概览系列文章-- https://aka.ms/office365devguide,到现在已经有17篇文章 ...

  6. gunicorn geventworker 解析

    在前面的文章曾介绍过gunicorn的syncworker,本文介绍其中一种asyncworker:GeventWorker.类图如下:   可见GeventWorker重载了init_process ...

  7. IntelliJ IDEA 左侧列表设置忽略文件格式

    什么问题 idea 中设置忽略文件 Unity开发过程中使用Lua做逻辑开发 Unity会自动生成xx.meta文件 这种文件再使用Idea开发过程中没有用处 显示文件列表中会看着比较乱 如何设置 F ...

  8. Python网络爬虫与信息提取(三)—— Re模块

    regular expression / regex / RE 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配.Python 自1.5版本起增加了re 模块,它提供 ...

  9. Swift3.0 自定义tableView复用cell 的写法,与CollectionViewCell的不同,数据model

    Model数据 class HospitalModel: NSObject { //后边不赋值 会报错 var imgurl :String = "" var introducti ...

  10. js获取图片的EXIF,解决图片旋转问题

    相信大家在做项目的时候会遇到在canvas里加入图片时,图片发生90°,180°的旋转.当时的你肯定时懵逼的,为毛. 其实这就是图片的EXIF搞的鬼. 什么是EXIF 简单来说,Exif 信息就是由数 ...