-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeakCache.cs
301 lines (263 loc) · 11.3 KB
/
WeakCache.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace DNQ.VirtualizedItemsSource
{
/// <summary> A base class for specialized weak caches based on a key-value pair paradigm where
/// values are stored as weak references. The cache will not prevent items from being
/// reclaimed by the Garbage Collector. </summary>
///
/// <seealso cref="T:System.Collections.IEnumerable"/>
public abstract class WeakCache
: System.Collections.IEnumerable
, IDisposable
{
private static List<WeakReference> _registeredCaches = new List<WeakReference>();
static WeakCache()
{
}
/// <summary> Creates a new parameterized weak cache and sets its <see cref="AutoPurgeInterval"/> property. </summary>
///
/// <typeparam name="TKeyType"> Type of the keys that will be used to reference items in the cache. </typeparam>
/// <typeparam name="TItemType"> Type of the items that will be stored in the new cache. </typeparam>
/// <param name="autoPurgeInterval"> The automatic purge interval for this cache. Set to
/// <see cref="TimeSpan.Zero"/> to disable automatic purging. </param>
///
/// <returns> . </returns>
public static WeakCache<TKeyType, TItemType> Create<TKeyType, TItemType>(TimeSpan autoPurgeInterval) where TItemType : class
{
var newCache = new WeakCache<TKeyType, TItemType>(autoPurgeInterval);
lock (_registeredCaches)
{
_registeredCaches.Add(new WeakReference(newCache));
}
if (autoPurgeInterval != TimeSpan.Zero)
{
AdjustAutoPurgeTimer();
}
return newCache;
}
/// <summary> Creates a new parameterized weak cache with auto purging disabled. </summary>
///
/// <typeparam name="TKeyType"> Type of the keys that will be used to reference items in the cache. </typeparam>
/// <typeparam name="TItemType"> Type of the items that will be stored in the new cache. </typeparam>
///
/// <returns> . </returns>
public static WeakCache<TKeyType, TItemType> Create<TKeyType, TItemType>() where TItemType : class
{
return Create<TKeyType, TItemType>(TimeSpan.Zero);
}
/// <summary> Specialised constructor for use only by derived classes. It is invoked by the
/// non-generic WeakCache object via the <see cref="Create"/> methods.</summary>
///
/// <param name="autoPurgeInterval"> The automatic purge interval for this cache.
/// Set to <see cref="TimeSpan.Zero"/> to disable automatic purging.</param>
protected WeakCache(TimeSpan autoPurgeInterval)
{
AutoPurgeInterval = autoPurgeInterval;
}
/// <summary> Gets the automatic purge interval for this weak cache (the time interval when
/// this cache is automatically purged of expired items).
/// </summary>
/// <remarks> If set to Zero this cache is not automatically purged.</remarks>
/// <value> The automatic purge interval for this cache. </value>
public TimeSpan AutoPurgeInterval
{
get;
private set;
}
/// <summary> Clears the cache by removing all the items. </summary>
public abstract void Clear();
/// <summary> Gets the number of elements in the cache (including potentially expired ones). </summary>
public abstract int Count { get; }
/// <summary> Gets the number of elements in the cache, excluding expired ones. This operation
/// is more expensive than a simple count. </summary>
public abstract int GetValidCount();
/// <summary> Purges the expired items from this cache. </summary>
/// <returns> A <see cref="PurgeResult"/> value indicating the number of items removed, or that an error occured. </returns>
public abstract PurgeResult Purge();
/// <summary> Gets the time when this cache was last purged. </summary>
public abstract DateTimeOffset LastPurgeTime { get; }
/// <summary> Returns an enumerator that iterates through all items stored in the cache. </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator" /> object that can be used to iterate through
/// the collection of items in the cache, containing KeyValuePair elements.
/// </returns>
public System.Collections.IEnumerator GetEnumerator()
{
return InternalGetEnumerator();
}
/// <summary> Gets the internal enumerator. </summary>
protected abstract System.Collections.IEnumerator InternalGetEnumerator();
/// <summary> Forces a purge of all active weak caches and removes any references to caches
/// that are no longer references anywhere elese in memory. </summary>
public static void PurgeAll()
{
bool registeredCachesChanged = false;
lock (_registeredCaches)
{
for (int idx = _registeredCaches.Count - 1; idx >= 0; idx--)
{
WeakReference wr = _registeredCaches[idx];
if (wr != null && wr.IsAlive && wr.Target != null)
{
((WeakCache)wr.Target).Purge();
}
else
{
registeredCachesChanged = true;
_registeredCaches.RemoveAt(idx);
}
}
}
if (registeredCachesChanged)
{
AdjustAutoPurgeTimer();
}
}
/// <summary>
/// Disposes the resources associated with this cache and removes the cache from the list of registered caches.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private bool _disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
try
{
bool registeredCachesChanged = false;
lock (_registeredCaches)
{
for (int idx = _registeredCaches.Count - 1; idx >= 0; idx--)
{
if (ReferenceEquals(_registeredCaches[idx].Target, this) || _registeredCaches[idx].IsAlive == false || _registeredCaches[idx].Target == null)
{
_registeredCaches.RemoveAt(idx);
registeredCachesChanged = true;
break;
}
}
}
if (registeredCachesChanged)
{
AdjustAutoPurgeTimer();
}
}
catch
{
// do nothing if an exception occurs here.. we're not supposed to throw from Dispose
}
}
_disposed = true;
}
}
#region Auto Purging Functionality
private static int ComputeGCD(int a, int b)
{
int c;
while (a != 0)
{
c = a;
a = b % a;
b = c;
}
return b;
}
private static int ComputeTimerFrequency()
{
lock (_registeredCaches)
{
if (_registeredCaches.Count == 0)
return 0;
int lastgcd = 0;
for (int idx = _registeredCaches.Count - 1; idx >= 0; idx--)
{
WeakReference wr = _registeredCaches[idx];
if (wr != null && wr.IsAlive && wr.Target != null)
{
WeakCache cache = (WeakCache)wr.Target;
TimeSpan interval = cache.AutoPurgeInterval;
if (interval != TimeSpan.Zero)
{
int minutes = (int)Math.Ceiling(interval.TotalMinutes);
if (lastgcd == 0)
lastgcd = minutes;
else if (minutes != 0)
{
lastgcd = ComputeGCD(minutes, lastgcd);
}
}
}
else
{
_registeredCaches.RemoveAt(idx);
}
}
return lastgcd;
}
}
private static int _lastTimerFrequency = 0;
private static Timer _autoPurgeTimer;
public static void AdjustAutoPurgeTimer()
{
int timerFrequency = ComputeTimerFrequency();
if (timerFrequency != _lastTimerFrequency)
{
if (_lastTimerFrequency == 0) // the timer has just been started
{
_autoPurgeTimer = new Timer(AutoPurgeCallback, null, timerFrequency * 60000, timerFrequency * 60000);
}
else if(timerFrequency == 0) // the timer has just been stopped
{
_autoPurgeTimer.Dispose();
}
else
{
_autoPurgeTimer.Change(timerFrequency * 60000, timerFrequency * 60000);
}
_lastTimerFrequency = timerFrequency;
}
}
public static void AutoPurgeCallback(object state)
{
bool registeredCachesChanged = false;
lock (_registeredCaches)
{
for (int idx = _registeredCaches.Count - 1; idx >= 0; idx--)
{
WeakReference wr = _registeredCaches[idx];
if (wr != null && wr.IsAlive && wr.Target != null)
{
WeakCache cache = (WeakCache)wr.Target;
if (cache.AutoPurgeInterval != TimeSpan.Zero
&&
cache.LastPurgeTime.Add(cache.AutoPurgeInterval) < DateTimeOffset.Now // only if it's up to be purged!
)
{
cache.Purge();
}
}
else
{
registeredCachesChanged = true;
_registeredCaches.RemoveAt(idx);
}
}
}
if (registeredCachesChanged)
{
AdjustAutoPurgeTimer();
}
}
#endregion
}
}