[工具类]缓存辅助类

写在前面

在项目中的统计模块中,查询耗费的时间,实在是太长了,通过优化sql语句或者添加缓存来提高查询的速度,自己就弄了一个缓存的辅助类,方便操作缓存中的数据。

CacheHelper

复制代码
 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Web;
 8 using System.Web.Caching;
 9 namespace WebSite.Statistics.Core.Utilities
10 {
11     /// <summary>
12     /// 缓存辅助类
13     /// </summary>
14     public static class CacheHelper
15     {
16         /// <summary>
17         /// 根据键获取缓存数据
18         /// </summary>
19         /// <param name="cacheKey"></param>
20         /// <returns></returns>
21         public static object GetCache(string cacheKey)
22         {
23             Cache objCache = HttpRuntime.Cache;
24             return objCache.Get(cacheKey);
25         }
26         /// <summary>
27         ///  设置缓存
28         /// </summary>
29         /// <param name="cacheKey">缓存键</param>
30         /// <param name="objValue">缓存键</param>
31         public static void SetCache(string cacheKey, object objValue)
32         {
33             Cache cache = HttpRuntime.Cache;
34             cache.Insert(cacheKey, objValue);
35         }
36         /// <summary>
37         /// 设置缓存
38         /// </summary>
39         /// <param name="cacheKey">缓存键</param>
40         /// <param name="objValue">缓存的值</param>
41         /// <param name="timeout">过期时间</param>
42         public static void SetCache(string cacheKey, object objValue, TimeSpan timeout)
43         {
44             Cache cache = HttpRuntime.Cache;
45             cache.Insert(cacheKey, objValue, null, DateTime.MaxValue, timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
46         }
47         /// <summary>
48         /// 设置缓存
49         /// </summary>
50         /// <param name="cacheKey">缓存键</param>
51         /// <param name="objValue">缓存的value</param>
52         /// <param name="absoluteExpiration">绝对过期时间</param>
53         /// <param name="slidingExpiration">滑动过期时间</param>
54         public static void SetCache(string cacheKey, object objValue, DateTime absoluteExpiration, TimeSpan slidingExpiration)
55         {
56             Cache cache = HttpRuntime.Cache;
57             cache.Insert(cacheKey, objValue, null, absoluteExpiration, slidingExpiration);
58         }
59         /// <summary>
60         /// 移除指定的缓存
61         /// </summary>
62         /// <param name="cacheKey"></param>
63         public static void RemoveCache(string cacheKey)
64         {
65             System.Web.Caching.Cache cache = HttpRuntime.Cache;
66             cache.Remove(cacheKey);
67         }
68         /// <summary>
69         /// 移除全部缓存
70         /// </summary>
71         public static void RemoveAllCache()
72         {
73             System.Web.Caching.Cache cache = HttpRuntime.Cache;
74             IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
75             while (CacheEnum.MoveNext())
76             {
77                 cache.Remove(CacheEnum.Key.ToString());
78             }
79         }
80     }
81 }
复制代码

总结

缓存用到的地方很多,封装的这个类,也算很基础的东西了,算是记录一下,以后用到的时候,方便查找。

博客地址:http://www.cnblogs.com/wolf-sun/
博客版权:本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。
如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步!
再次感谢您耐心的读完本篇文章。http://www.cnblogs.com/wolf-sun/p/4469600.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值