缓存依赖类,依赖于XML,供初学者使用
1using System;
2using System.Collections.Generic;
3using System.Web;
4using System.Web.Caching;
5
6/// <summary>
7///CacheProvider 的摘要说明
8/// </summary>
9public class CacheProvider
10{
11 public CacheProvider()
12 {
13
14 }
15
16 /// <summary>
17 /// 更新某个缓存,请确保缓存键与依赖的文件名称相同
18 /// </summary>
19 /// <param name="cacheKey">缓存键</param>
20 /// <param name="obj">该键缓存所存的对象</param>
21 public static void UpdateCache(string cacheKey, object obj)
22 {
23 lock (typeof(CacheProvider))
24 {
25 try
26 {
27 if (HttpContext.Current.Cache[cacheKey] == null)
28 {
29 CreateCache(cacheKey, obj);
30 }
31 }
32 catch (Exception ex)
33 {
34 Tools.Loger("", ex.Message.ToString());
35 }
36 }
37 }
38
39 /// <summary>
40 /// 创建新缓存
41 /// </summary>
42 /// <param name="cacheKey">缓存键</param>
43 /// <param name="obj">该键缓存所存的对象</param>
44 static void CreateCache(string cacheKey, object obj)
45 {
46 lock (typeof(CacheProvider))
47 {
48 try
49 {
50 string filename = SetDepencyFilePath(cacheKey);
51 if (filename.Trim().Length == 0)
52 {
53 HttpContext.Current.Response.Write("缓存依赖的文件不存在,导致此原因的问题可能是缓存的键同所依赖的文件的名称不同。");
54 return;
55 }
56 HttpRuntime.Cache.Insert(cacheKey, obj, new CacheDependency(filename), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, new CacheItemRemovedCallback(CacheItemRemoved));
57 }
58 catch (Exception ex)
59 {
60 Tools.Loger("", ex.Message.ToString());
61 }
62 }
63 }
64
65 /// <summary>
66 /// 取出依赖的文件的文件路径
67 /// </summary>
68 /// <param name="cacheKey">依赖取出文件的缓存的键</param>
69 /// <returns>依赖文件的文件路径</returns>
70 static string SetDepencyFilePath(string cacheKey)
71 {
72 try
73 {
74 string virtualPath = string.Format("~/App_Data/{0}.xml", cacheKey);
75 string filePath = HttpContext.Current.Server.MapPath(virtualPath);
76
77 if (System.IO.File.Exists(filePath))
78 {
79 return filePath;
80 }
81 else
82 {
83 return string.Empty;
84 }
85 }
86 catch (Exception ex)
87 {
88 Tools.Loger("", ex.Message.ToString());
89 return string.Empty;
90 }
91 }
92
93 /// <summary>
94 /// 从 System.Web.Caching.Cache 移除缓存项时通知应用程序的回调方法。
95 /// </summary>
96 /// <param name="key">从缓存中移除的键。</param>
97 /// <param name="obj">与从缓存中移除的键关联的 System.Object 项。</param>
98 /// <param name="reason"> System.Web.Caching.CacheItemRemovedReason 枚举指定的、从缓存移除项的原因。</param>
99 static void CacheItemRemoved(string key, object obj, CacheItemRemovedReason reason)
100 {
101 try
102 {
103 if (CacheItemRemovedReason.DependencyChanged == reason)
104 {
105 CreateCache(key, obj);
106 }
107 }
108 catch (Exception ex)
109 {
110 Tools.Loger("", ex.Message.ToString());
111 }
112 }
113}
2using System.Collections.Generic;
3using System.Web;
4using System.Web.Caching;
5
6/// <summary>
7///CacheProvider 的摘要说明
8/// </summary>
9public class CacheProvider
10{
11 public CacheProvider()
12 {
13
14 }
15
16 /// <summary>
17 /// 更新某个缓存,请确保缓存键与依赖的文件名称相同
18 /// </summary>
19 /// <param name="cacheKey">缓存键</param>
20 /// <param name="obj">该键缓存所存的对象</param>
21 public static void UpdateCache(string cacheKey, object obj)
22 {
23 lock (typeof(CacheProvider))
24 {
25 try
26 {
27 if (HttpContext.Current.Cache[cacheKey] == null)
28 {
29 CreateCache(cacheKey, obj);
30 }
31 }
32 catch (Exception ex)
33 {
34 Tools.Loger("", ex.Message.ToString());
35 }
36 }
37 }
38
39 /// <summary>
40 /// 创建新缓存
41 /// </summary>
42 /// <param name="cacheKey">缓存键</param>
43 /// <param name="obj">该键缓存所存的对象</param>
44 static void CreateCache(string cacheKey, object obj)
45 {
46 lock (typeof(CacheProvider))
47 {
48 try
49 {
50 string filename = SetDepencyFilePath(cacheKey);
51 if (filename.Trim().Length == 0)
52 {
53 HttpContext.Current.Response.Write("缓存依赖的文件不存在,导致此原因的问题可能是缓存的键同所依赖的文件的名称不同。");
54 return;
55 }
56 HttpRuntime.Cache.Insert(cacheKey, obj, new CacheDependency(filename), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, new CacheItemRemovedCallback(CacheItemRemoved));
57 }
58 catch (Exception ex)
59 {
60 Tools.Loger("", ex.Message.ToString());
61 }
62 }
63 }
64
65 /// <summary>
66 /// 取出依赖的文件的文件路径
67 /// </summary>
68 /// <param name="cacheKey">依赖取出文件的缓存的键</param>
69 /// <returns>依赖文件的文件路径</returns>
70 static string SetDepencyFilePath(string cacheKey)
71 {
72 try
73 {
74 string virtualPath = string.Format("~/App_Data/{0}.xml", cacheKey);
75 string filePath = HttpContext.Current.Server.MapPath(virtualPath);
76
77 if (System.IO.File.Exists(filePath))
78 {
79 return filePath;
80 }
81 else
82 {
83 return string.Empty;
84 }
85 }
86 catch (Exception ex)
87 {
88 Tools.Loger("", ex.Message.ToString());
89 return string.Empty;
90 }
91 }
92
93 /// <summary>
94 /// 从 System.Web.Caching.Cache 移除缓存项时通知应用程序的回调方法。
95 /// </summary>
96 /// <param name="key">从缓存中移除的键。</param>
97 /// <param name="obj">与从缓存中移除的键关联的 System.Object 项。</param>
98 /// <param name="reason"> System.Web.Caching.CacheItemRemovedReason 枚举指定的、从缓存移除项的原因。</param>
99 static void CacheItemRemoved(string key, object obj, CacheItemRemovedReason reason)
100 {
101 try
102 {
103 if (CacheItemRemovedReason.DependencyChanged == reason)
104 {
105 CreateCache(key, obj);
106 }
107 }
108 catch (Exception ex)
109 {
110 Tools.Loger("", ex.Message.ToString());
111 }
112 }
113}