我是一个菜鸟,我只是在努力,2021正视自己,面对2021!

ASP.NET HttpRuntime.Cache缓存类使用总结

  1.高性能文件缓存key-value存储—Redis

  2.高性能文件缓存key-value存储—Memcached

  备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地址。

1.前言

  a.在Web开发中,我们经常能够使用到缓存对象(Cache),在ASP.NET中提供了两种缓存对象,HttpContext.Current.Cache和HttpRuntime.Cache,那么他们有什么区别呢?下面简单描述一下:

    (1):HttpContext.Current.Cache 为当前Http请求获取Cache对象,通俗来说就是由于此缓存封装在了HttpContenxt中,而HttpContext只局限于Web中,所以此缓存信息只能够在Web中使用。

    (2):HttpRuntime.Cache 获取当前应用程序的Cache,通俗来说就是此缓存信息虽然被放在了System.Web命名空间下,但是非Web程序也可以使用此缓存。

    上面两种类型作对比,我们就能够看出,一般情况下我们都建议使用HttpRuntime.Cache 。

  b.在缓存领域中,现在不止只有ASP.NET提供的缓存,还有Redis和Memcached等开源的Key_Value存储系统,也是基于内存去管理的,那么我们在这些文章中基本也都会有说到。

  c.那么这篇文章我们就简单的封装一下HttpRuntime.Cache类的公用类,提供给别人直接使用,如有错误或者问题,请留言,必在第一时间去处理。

  d.缓存详解:http://www.cnblogs.com/caoxch/archive/2006/11/20/566236.html

  e.GitHub地址:https://github.com/kencery/Common/tree/master/KenceryCommonMethod/%E7%BC%93%E5%AD%98

2.为什么使用缓存

  a.那么说了这么多,最终要的一个问题是我们为什么要使用缓存呢?

    (1):降低延迟,使响应速度加快。

    (2):降低网络传输,使响应速度加快。

    ...........................

    简单总结就是一句话,使用缓存是为了使系统更加稳定和快速。

  b.上面我们知道了为什么使用缓存,那么在什么情况下我们能够使用缓存呢?

    (1):数据可能会被频繁的使用。

    (2):数据的访问不频繁,但是它的生命周期很长,这样的数据建议也缓存起来,比如:淘宝的商品明细。

    ......................

  c.当然一般系统中我们建议使用Memcached和Redis键值对来存储缓存信息。

  d.那么是不是我们在写程序的时候想写缓存就写缓存呢?当然不是,我们还要判断哪里该用,哪里不该用,比如:如果我们整页输出缓存的话,会影响我们数据的更新等等.......

  e.这篇博文我们整理了HttpRuntime Cache的共同类,将如何使用此类都已经在代码中展示出来,代码如下:

3.代码展示

  1 // 源文件头信息:
  2 // <copyright file="HttpRuntimeCache.cs">
  3 // Copyright(c)2014-2034 Kencery.All rights reserved.
  4 // 个人博客:http://www.cnblogs.com/hanyinglong
  5 // 创建人:韩迎龙(kencery)
  6 // 创建时间:2015-8-11
  7 // </copyright>
  8 
  9 using System;
 10 using System.Collections;
 11 using System.Web;
 12 using System.Web.Caching;
 13 
 14 namespace KenceryCommonMethod
 15 {
 16     /// <summary>
 17     /// HttpRuntime Cache读取设置缓存信息封装
 18     /// <auther>
 19     ///     <name>Kencery</name>
 20     ///     <date>2015-8-11</date>
 21     /// </auther>
 22     /// 使用描述:给缓存赋值使用HttpRuntimeCache.Set(key,value....)等参数(第三个参数可以传递文件的路径(HttpContext.Current.Server.MapPath()))
 23     /// 读取缓存中的值使用JObject jObject=HttpRuntimeCache.Get(key) as JObject,读取到值之后就可以进行一系列判断
 24     /// </summary>
 25     public class HttpRuntimeCache
 26     {
 27         /// <summary>
 28         /// 设置缓存时间,配置(从配置文件中读取)
 29         /// </summary>
 30         private const double Seconds = 30*24*60*60;
 31 
 32         /// <summary>
 33         /// 缓存指定对象,设置缓存
 34         /// </summary>
 35         public static bool Set(string key, object value)
 36         {
 37             return Set(key, value, null, DateTime.Now.AddSeconds(Seconds), Cache.NoSlidingExpiration,
 38                 CacheItemPriority.Default, null);
 39         }
 40 
 41         /// <summary>
 42         ///  缓存指定对象,设置缓存
 43         /// </summary>
 44         public static bool Set(string key, object value, string path)
 45         {
 46             try
 47             {
 48                 var cacheDependency = new CacheDependency(path);
 49                 return Set(key, value, cacheDependency);
 50             }
 51             catch
 52             {
 53                 return false;
 54             }
 55         }
 56 
 57         /// <summary>
 58         /// 缓存指定对象,设置缓存
 59         /// </summary>
 60         public static bool Set(string key, object value, CacheDependency cacheDependency)
 61         {
 62             return Set(key, value, cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
 63                 CacheItemPriority.Default, null);
 64         }
 65 
 66         /// <summary>
 67         /// 缓存指定对象,设置缓存
 68         /// </summary>
 69         public static bool Set(string key, object value, double seconds, bool isAbsulute)
 70         {
 71             return Set(key, value, null, (isAbsulute ? DateTime.Now.AddSeconds(seconds) : Cache.NoAbsoluteExpiration),
 72                 (isAbsulute ? Cache.NoSlidingExpiration : TimeSpan.FromSeconds(seconds)), CacheItemPriority.Default,
 73                 null);
 74         }
 75 
 76         /// <summary>
 77         /// 获取缓存对象
 78         /// </summary>
 79         public static object Get(string key)
 80         {
 81             return GetPrivate(key);
 82         }
 83 
 84         /// <summary>
 85         /// 判断缓存中是否含有缓存该键
 86         /// </summary>
 87         public static bool Exists(string key)
 88         {
 89             return (GetPrivate(key) != null);
 90         }
 91 
 92         /// <summary>
 93         /// 移除缓存对象
 94         /// </summary>
 95         /// <param name="key"></param>
 96         /// <returns></returns>
 97         public static bool Remove(string key)
 98         {
 99             if (string.IsNullOrEmpty(key))
100             {
101                 return false;
102             }
103             HttpRuntime.Cache.Remove(key);
104             return true;
105         }
106 
107         /// <summary>
108         /// 移除所有缓存
109         /// </summary>
110         /// <returns></returns>
111         public static bool RemoveAll()
112         {
113             IDictionaryEnumerator iDictionaryEnumerator = HttpRuntime.Cache.GetEnumerator();
114             while (iDictionaryEnumerator.MoveNext())
115             {
116                 HttpRuntime.Cache.Remove(Convert.ToString(iDictionaryEnumerator.Key));
117             }
118             return true;
119         }
120 
121         //------------------------提供给上面方法进行调用-----------------------------------
122         /// <summary>
123         /// 设置缓存
124         /// </summary>
125         public static bool Set(string key, object value, CacheDependency cacheDependency, DateTime dateTime,
126             TimeSpan timeSpan, CacheItemPriority cacheItemPriority, CacheItemRemovedCallback cacheItemRemovedCallback)
127         {
128             if (string.IsNullOrEmpty(key) || value == null)
129             {
130                 return false;
131             }
132             HttpRuntime.Cache.Insert(key, value, cacheDependency, dateTime, timeSpan, cacheItemPriority,
133                 cacheItemRemovedCallback);
134             return true;
135         }
136 
137         /// <summary>
138         /// 获取缓存
139         /// </summary>
140         private static object GetPrivate(string key)
141         {
142             return string.IsNullOrEmpty(key) ? null : HttpRuntime.Cache.Get(key);
143         }
144     }
145 }
posted @ 2015-08-11 17:52  Kencery  阅读(17389)  评论(4编辑  收藏  举报
友情链接:初心商城