關于緩存的設計
1、什麼情況下用緩存
緩存是提高應用程式性能的最好方法之一。運用緩存可以優化資料查詢,避免不必要的網絡資料回傳,和避免執行不必要的完全相同的資料處理邏輯。在實作緩存的時候我們要确定什麼時候裝入緩存資料。用異步裝入緩存或用批處理方式來避免出現用戶端資料延遲。
一般來說在一定時間内請求了相同的業務邏輯而沒有變更的話,可以采用緩存來設計。資料請求頻繁的的請求不适合采用緩存,如論壇的回複,但是論壇的主題是可以采用緩存設計的。
2、緩存設計的步驟
确定緩存資料結構:即設計中哪些資料用到了緩存,設計這些資料的緩存結構
确定緩存什麼資料
确定緩存過期規則和清理
确定如何裝入緩存資料
3、示例 Community Server的緩存類

1
using System;
2
using System.Collections;
3
using System.Text.RegularExpressions;
4
using System.Web;
5
using System.Web.Caching;
6
7
namespace Larry.Cache
8

{
9
/// <summary>
10
/// 緩存類 Community Server的緩存類
11
/// </summary>
12
public class BaseCache
13
{
14
/// <summary>
15
/// CacheDependency 說明
16
/// 如果您向 Cache 中添加某個具有依賴項的項,當依賴項更改時,
17
/// 該項将自動從 Cache 中删除。例如,假設您向 Cache 中添加某項,
18
/// 并使其依賴于檔案名數組。當該數組中的某個檔案更改時,
19
/// 與該數組關聯的項将從緩存中删除。
20
/// [C#]
21
/// Insert the cache item.
22
/// CacheDependency dep = new CacheDependency(fileName, dt);
23
/// cache.Insert("key", "value", dep);
24
/// </summary>
25
public static readonly int DayFactor = 17280;
26
public static readonly int HourFactor = 720;
27
public static readonly int MinuteFactor = 12;
28
public static readonly double SecondFactor = 0.2;
29
30
private static readonly System.Web.Caching.Cache _cache;
31
32
private static int Factor = 1440;
33
34
35
/// 單件模式
36
37
static BaseCache()
38
{
39
HttpContext context = HttpContext.Current;
40
if (context != null)
41
{
42
_cache = context.Cache;
43
}
44
else
45
46
_cache = HttpRuntime.Cache;
47
48
}
49
50
51
/// 一次性清除所有緩存
52
53
public static void Clear()
54
55
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
56
ArrayList al = new ArrayList();
57
while (CacheEnum.MoveNext()) //逐個清除
58
59
al.Add(CacheEnum.Key);
60
61
62
foreach (string key in al)
63
64
_cache.Remove(key);
65
66
67
68
69
70
71
public static void RemoveByPattern(string pattern)
72
73
74
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
75
while (CacheEnum.MoveNext())
76
77
if (regex.IsMatch(CacheEnum.Key.ToString()))
78
_cache.Remove(CacheEnum.Key.ToString());
79
80
81
82
83
/// 清除特定的緩存
84
85
/// <param name="key"></param>
86
public static void Remove(string key)
87
88
_cache.Remove(key);
89
90
91
92
/// 緩存OBJECT.
93
94
95
/// <param name="obj"></param>
96
public static void Insert(string key, object obj)
97
98
Insert(key, obj, null, 1);
99
100
101
102
/// 緩存obj 并建立依賴項
103
104
105
106
/// <param name="dep"></param>
107
public static void Insert(string key, object obj, CacheDependency dep)
108
109
Insert(key, obj, dep, MinuteFactor * 3);
110
111
112
113
/// 按秒緩存對象
114
115
116
117
/// <param name="seconds"></param>
118
public static void Insert(string key, object obj, int seconds)
119
120
Insert(key, obj, null, seconds);
121
122
123
124
/// 按秒緩存對象 并存儲優先級
125
126
127
128
129
/// <param name="priority"></param>
130
public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
131
132
Insert(key, obj, null, seconds, priority);
133
134
135
136
/// 按秒緩存對象 并建立依賴項
137
138
139
140
141
142
public static void Insert(string key, object obj, CacheDependency dep, int seconds)
143
144
Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
145
146
147
148
/// 按秒緩存對象 并建立具有優先級的依賴項
149
150
151
152
153
154
155
public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
156
157
if (obj != null)
158
159
_cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(Factor * seconds), TimeSpan.Zero, priority, null);
160
161
162
163
164
165
public static void MicroInsert(string key, object obj, int secondFactor)
166
167
168
169
_cache.Insert(key, obj, null, DateTime.Now.AddSeconds(Factor * secondFactor), TimeSpan.Zero);
170
171
172
173
174
/// 最大時間緩存
175
176
177
178
public static void Max(string key, object obj)
179
180
Max(key, obj, null);
181
182
183
184
/// 具有依賴項的最大時間緩存
185
186
187
188
189
public static void Max(string key, object obj, CacheDependency dep)
190
191
192
193
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
194
195
196
197
198
/// Insert an item into the cache for the Maximum allowed time
199
200
201
202
public static void Permanent(string key, object obj)
203
204
Permanent(key, obj, null);
205
206
207
public static void Permanent(string key, object obj, CacheDependency dep)
208
209
210
211
_cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
212
213
214
215
public static object Get(string key)
216
217
return _cache[key];
218
219
220
221
/// Return int of seconds * SecondFactor
222
223
public static int SecondFactorCalculate(int seconds)
224
225
// Insert method below takes integer seconds, so we have to round any fractional values
226
return Convert.ToInt32(Math.Round((double)seconds * SecondFactor));
227
228
}
229
}
230
其實這個類就是一個單件模式的設計 和緩存的公共操作方法,其中CacheDependency表示建立緩存依賴項,CacheItemPriority表示緩存的優先級。S使用如下
1
public static CardShop.Model.Systems GetConfig()
2

3
const string cacheKey = "WebConfig";
4
CardShop.Model.Systems sampleCacheTable = Larry.Cache.BaseCache.Get(cacheKey) as CardShop.Model.Systems;
5
if (sampleCacheTable == null)
6
7
OprationCheck.Message("第一次加載使用緩存");
8
sampleCacheTable = model;
9
Larry.Cache.BaseCache.Insert(cacheKey, sampleCacheTable, 24 * Larry.Cache.BaseCache.MinuteFactor);
10
11
else
12
13
OprationCheck.Message("已經加載了緩存不需要再加載");
14
15
return sampleCacheTable;
16
本文轉自左正部落格園部落格,原文連結:http://www.cnblogs.com/soundcode/archive/2013/03/12/2955330.html,如需轉載請自行聯系原作者