码迷,mamicode.com
首页 > 其他好文 > 详细

[工具类]缓存辅助类

时间:2015-04-30 19:47:10      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

写在前面

在项目中的统计模块中,查询耗费的时间,实在是太长了,通过优化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/p/4469600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!