近期在刚开始一个微信二次开发,发觉手机微信的Access_Token获得每日频次是比较有限的,随后想起缓存文件,恰好见到手机微信实例教程里边强烈推荐HttpRuntime.Cache缓存文件就顺带看过下。

写了(Copy)了一个輔助类,现阶段只包含建立,获得,及清除

下边是编码:

 1     using System;
 2     using System.Collections;
 3     using System.Collections.Generic;
 4     using System.Linq;
 5     using System.Web;
 6     using System.Web.Caching;
 7 
 8     namespace TEST.Public
 9     {
10         public class CacheHelper
11         {
12 
13             /// <summary>
14             /// 建立缓存文件
15             /// </summary>
16             /// <param name="key">缓存文件的Key</param>
17             /// <param name="value">缓存文件的数据信息</param>
18             /// <param name="cacheDependency">依靠项,一般为null</param>
19             /// <param name="dateTime">缓存文件到期時间</param>
20             /// <param name="timeSpan">设定缓存文件不是应用到期還是到時间就到期</param>
21             /// <param name="cacheItemPriority">缓存文件优先</param>
22             /// <param name="cacheItemRemovedCallback">回调函数方式,一般为null</param>
23             /// <returns></returns>
24             public bool CreateCache(string key, object value, CacheDependency cacheDependency, DateTime dateTime, TimeSpan timeSpan,
25                 CacheItemPriority cacheItemPriority, CacheItemRemovedCallback cacheItemRemovedCallback)
26             {
27                 if (string.IsNullOrEmpty(key) || value == null)
28                 {
29                     return false;
30                 }
31                 HttpRuntime.Cache.Insert(key, value, cacheDependency, dateTime, timeSpan, cacheItemPriority, cacheItemRemovedCallback);
32                 return true;
33             }
34 
35             /// <summary>
36             /// 获得缓存文件
37             /// </summary>
38             /// <param name="key"></param>
39             /// <returns></returns>
40             public object GetCache(string key)
41             {
42                 return string.IsNullOrEmpty(key) ? null : HttpRuntime.Cache.Get(key);
43             }
44 
45 
46             /// <summary>
47             /// 清除全部缓存文件
48             /// </summary>
49             /// <returns></returns>
50             public bool RemoveAll()
51             {
52                 IDictionaryEnumerator iDictionaryEnumerator = HttpRuntime.Cache.GetEnumerator();
53                 while (iDictionaryEnumerator.MoveNext())
54                 {
55                     HttpRuntime.Cache.Remove(Convert.ToString(iDictionaryEnumerator.Key));
56                 }
57                 return true;
58             }
59         }
60     }