-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoRefreshCache.cs
387 lines (350 loc) · 13.1 KB
/
AutoRefreshCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace jiangyi1985
{
/// <summary>
/// A Memory key/value cache that:
/// 1. load data on first access (thread block)
/// 2. automatically refresh data when cache entry expired (in async mono thread)
/// Usage:
/// 1. one class calling TryRegisterNewCacheEntry first to register ValueFactory and then other classes calling Get to get value
/// 2. every class calling GetOrRegisterNew to get value or register ValueFactory
/// (only the first guy that register will successfully register the ValueFactory, other registration action will be ignored)
/// Note:
/// 1.first load thread-block
/// 2.refreshing triggered only when cache hit/accessed
/// 3.cache entry need to be added first by calling register method
/// 4.expected_minimum_cache_updating_period (when under constant requesting) = load_data_time + cache_entry_life_span
/// </summary>
public class AutoRefreshCache
{
private AutoRefreshCache() { }
private static readonly Lazy<AutoRefreshCache> Lazy = new Lazy<AutoRefreshCache>(() => new AutoRefreshCache());
public static AutoRefreshCache Instance => Lazy.Value;
private readonly Dictionary<string, CacheItem> _dic = new Dictionary<string, CacheItem>();
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
public int Count => _dic.Count;
public string Status
{
get
{
var sb = new StringBuilder();
foreach (var pair in _dic)
{
sb.Append(
pair.Key + " " + pair.Value.Status + " " + pair.Value.ExpireAt + " " + pair.Value.LifeSpan);
}
return sb.ToString();
}
}
/// <summary>
/// Register a new cache entry with a value factory
/// (this method holds a lock on the entire cache!!)
/// (Throws when key already exists)
/// </summary>
private void RegisterNewCacheEntry(string key, Func<string, object> valueFactory, TimeSpan lifeSpan)
{
_lock.EnterWriteLock();
try
{
_dic.Add(key, new CacheItem()
{
LifeSpan = lifeSpan,
ValueFactory = valueFactory,
ExpireAt = DateTime.MinValue,
Status = InProcCacheItemStatus.Empty,
Value = null,
});
}
finally { _lock.ExitWriteLock(); }
}
/// <summary>
/// Try registering a new cache entry with a value factory that will be run when
/// 1. the cache entry is empty
/// 2. when the cache entry expires
/// </summary>
/// <param name="key">cache entry key</param>
/// <param name="valueFactory">value factory that produce the cache entry value</param>
/// <param name="lifeSpan">life span of the cache entry</param>
/// <returns>true if entry added, false if key already exists</returns>
public bool TryRegisterNewCacheEntry(string key, Func<string, object> valueFactory, TimeSpan lifeSpan)
{
_lock.EnterReadLock();
try
{
if (_dic.ContainsKey(key))
return false;
_lock.ExitReadLock();
try
{
RegisterNewCacheEntry(key, valueFactory, lifeSpan);
return true;
}
catch (ArgumentException e)
{
return false;
}
}
finally { if (_lock.IsReadLockHeld) _lock.ExitReadLock(); }
}
public T Get<T>(string key)
{
return (T)Get(key);
}
public object Get(string key)
{
_lock.EnterReadLock();
try
{
CacheItem cacheItem = _dic[key];
switch (cacheItem.Status)
{
case InProcCacheItemStatus.Empty:
_lock.ExitReadLock();
_lock.EnterWriteLock();
//load data (thread blocking)
try
{
if (cacheItem.Status == InProcCacheItemStatus.Empty)
{
cacheItem.Value = cacheItem.ValueFactory(key);
cacheItem.ExpireAt = DateTime.UtcNow.Add(cacheItem.LifeSpan);
cacheItem.Status = InProcCacheItemStatus.Healthy;
}
}
finally { _lock.ExitWriteLock(); }
break;
case InProcCacheItemStatus.Healthy:
if (DateTime.UtcNow >= cacheItem.ExpireAt) //expired
{
_lock.ExitReadLock();
_lock.EnterWriteLock();
//start mono-thread async task to load data
try
{
if (cacheItem.Status != InProcCacheItemStatus.Refreshing)
{
cacheItem.Status = InProcCacheItemStatus.Refreshing;
//queue task
Task.Factory.StartNew(() =>
{
//get data
object newValue = null;
bool hasException = false;
try
{
newValue = cacheItem.ValueFactory(key);
}
catch (Exception e)
{
hasException = true;
//write log here
}
//update cache
_lock.EnterWriteLock();
try
{
if (!hasException)
{
cacheItem.Value = newValue;
cacheItem.ExpireAt = DateTime.UtcNow.Add(cacheItem.LifeSpan);
}
//no need to change cache status here because we are doing it in the finally clause
}
finally
{
//change the status from refreshing to healthy, even when there's an exception
//because we want another thread to be able to trigger the refreshing process later
cacheItem.Status = InProcCacheItemStatus.Healthy;
_lock.ExitWriteLock();
}
});
}
}
finally { _lock.ExitWriteLock(); }
}
break;
case InProcCacheItemStatus.Refreshing:
//do nothing
break;
default:
throw new ArgumentOutOfRangeException();
}
return cacheItem.Value;
}
finally { if (_lock.IsReadLockHeld) _lock.ExitReadLock(); }
}
public T GetOrRegisterNew<T>(string key, Func<string, object> valueFactory, TimeSpan lifeSpan)
{
return (T)GetOrRegisterNew(key, valueFactory, lifeSpan);
}
public object GetOrRegisterNew(string key, Func<string, object> valueFactory, TimeSpan lifeSpan)
{
try
{
return Get(key);
}
catch (KeyNotFoundException)
{
TryRegisterNewCacheEntry(key,valueFactory,lifeSpan);
return Get(key);
}
}
//public string GetOrAdd(string key, Func<string, string> valueFactory, int periodicUpdateSeconds = 0)
//{
// _lock.EnterReadLock();
// try
// {
// return _dic[key];
// }
// catch (KeyNotFoundException)
// {
// _lock.ExitReadLock();
// _lock.EnterWriteLock();
// try
// {
// if (!_dic.ContainsKey(key))
// {
// var value = valueFactory.Invoke(key);
// _dic.Add(key, value);
// return value;
// }
// else
// {
// return _dic[key];
// }
// }
// finally
// {
// _lock.ExitWriteLock();
// }
// }
// finally
// {
// if (_lock.IsReadLockHeld)
// _lock.ExitReadLock();
// }
//}
//public void Add(string key, string value)
//{
// _lock.EnterWriteLock();
// try
// {
// _dic.Add(key, value);
// }
// finally
// {
// _lock.ExitWriteLock();
// }
//}
//public bool AddWithTimeout(string key, string value, int timeout)
//{
// if (_lock.TryEnterWriteLock(timeout))
// {
// try
// {
// _dic.Add(key, value);
// }
// finally
// {
// _lock.ExitWriteLock();
// }
// return true;
// }
// else
// {
// return false;
// }
//}
//public AddOrUpdateStatus AddOrUpdate(string key, string value)
//{
// _lock.EnterUpgradeableReadLock();
// try
// {
// string result = null;
// if (_dic.TryGetValue(key, out result))
// {
// if (result == value)
// {
// return AddOrUpdateStatus.Unchanged;
// }
// else
// {
// _lock.EnterWriteLock();
// try
// {
// _dic[key] = value;
// }
// finally
// {
// _lock.ExitWriteLock();
// }
// return AddOrUpdateStatus.Updated;
// }
// }
// else
// {
// _lock.EnterWriteLock();
// try
// {
// _dic.Add(key, value);
// }
// finally
// {
// _lock.ExitWriteLock();
// }
// return AddOrUpdateStatus.Added;
// }
// }
// finally
// {
// _lock.ExitUpgradeableReadLock();
// }
//}
//public void Delete(string key)
//{
// _lock.EnterWriteLock();
// try
// {
// _dic.Remove(key);
// }
// finally
// {
// _lock.ExitWriteLock();
// }
//}
~AutoRefreshCache()
{
_lock?.Dispose();
}
}
enum InProcCacheItemStatus
{
/// <summary>
/// cache entry is created but cache value is never populated
/// </summary>
Empty,
/// <summary>
/// cache value ready and not expired
/// </summary>
Healthy,
/// <summary>
/// cache value ready but expired, new value is being fetched
/// </summary>
Refreshing,
}
class CacheItem
{
public object Value { get; set; }
public InProcCacheItemStatus Status { get; set; }
public DateTime ExpireAt { get; set; }
public TimeSpan LifeSpan { get; set; }
public Func<string, object> ValueFactory { get; set; }
}
}