-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayeredCache.cs
More file actions
502 lines (438 loc) · 16.6 KB
/
Copy pathLayeredCache.cs
File metadata and controls
502 lines (438 loc) · 16.6 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
namespace DigitalRuby.SimpleCache;
/// <summary>
/// Layered cache interface. A layered cache aggregates multiple caches, such as memory, file and distributed cache (redis, etc.).<br/>
/// Internally, keys are prefixed with the entry assembyly name and the type full name. You can change the entry assembly by specifying a KeyPrefix in the configuration.<br/>
/// </summary>
public interface ILayeredCache : IDisposable
{
/// <summary>
/// Get or create an item from the cache.
/// </summary>
/// <typeparam name="T">Type of item</typeparam>
/// <param name="key">Cache key</param>
/// <param name="factory">Factory method to create the item if no item is in the cache for the key. This factory is guaranteed to execute only one per key.<br/>
/// Inside your factory, you should set the CacheParameters on the GetOrCreateAsyncContext to a duration and size tuple: (TimeSpan duration, int size)</param>
/// <param name="state">Object to set on the get or create context to avoid capturing variables</param>
/// <param name="cancelToken">Cancel token</param>
/// <returns>Task of return of type T, can have a null value if the get or create returned null</returns>
Task<T?> GetOrCreateAsync<T>(string key, Func<GetOrCreateAsyncContext, Task<T?>> factory,
object? state = null, CancellationToken cancelToken = default);
/// <summary>
/// Attempts to retrieve value of T by key.
/// </summary>
/// <typeparam name="T">Type of object to get</typeparam>
/// <param name="key">Cache key</param>
/// <param name="cancelToken">Cancel token</param>
/// <returns>Result of type T or null if nothing found for the key</returns>
Task<T?> GetAsync<T>(string key, CancellationToken cancelToken = default);
/// <summary>
/// Sets value T by key.
/// </summary>
/// <typeparam name="T">Type of object</typeparam>
/// <param name="key">Cache key to set</param>
/// <param name="value">Value to set</param>
/// <param name="cacheParam">Cache parameters</param>
/// <param name="cancelToken">Cancel token</param>
/// <returns>Task</returns>
Task SetAsync<T>(string key, T value, CacheParameters cacheParam, CancellationToken cancelToken = default);
/// <summary>
/// Attempts to delete an entry of T type by key. If there is no key found, nothing happens.
/// </summary>
/// <typeparam name="T">The type of object to delete</typeparam>
/// <param name="key">The key to delete</param>
/// <param name="cancelToken">Cancel token</param>
/// <returns>Task</returns>
Task DeleteAsync<T>(string key, CancellationToken cancelToken = default);
}
/// <summary>
/// Null layered cache, always executes factory
/// </summary>
public sealed class NullLayeredCache : ILayeredCache
{
/// <inheritdoc />
public void Dispose() { }
/// <inheritdoc />
public Task DeleteAsync<T>(string key, CancellationToken cancelToken = default) => Task.CompletedTask;
/// <inheritdoc />
public Task<T?> GetAsync<T>(string key, CancellationToken cancelToken = default) => Task.FromResult<T?>(default);
/// <inheritdoc />
public Task<T?> GetOrCreateAsync<T>(string key, Func<GetOrCreateAsyncContext, Task<T?>> factory,
object? state = null, CancellationToken cancelToken = default) =>
factory(new GetOrCreateAsyncContext(key, state, cancelToken));
/// <inheritdoc />
public Task SetAsync<T>(string key, T obj, CacheParameters cacheParam, CancellationToken cancelToken = default) => Task.CompletedTask;
}
/// <summary>
/// Layered cache options
/// </summary>
public sealed class LayeredCacheOptions
{
/// <summary>
/// Key prefix, all keys will be automatically prefixed with this value. You could use your service name for example.
/// </summary>
public string KeyPrefix { get; set; } = string.Empty;
}
/// <inheritdoc />
public sealed class LayeredCache : ILayeredCache, IKeyStrategy, IDisposable
{
private static readonly TimeSpan defaultCacheTime = TimeSpan.FromMinutes(5.0);
private readonly string keyPrefix;
private readonly ISerializer serializer;
private readonly IMemoryCache memoryCache;
private readonly IFileCache fileCache;
private readonly IDistributedCache distributedCache;
private readonly TimeProvider clock;
private readonly ILogger logger;
private readonly IAsyncRequestCollapserPolicy cachePolicy;
private readonly AsyncPolicy distributedCacheCircuitBreakPolicy;
/// <summary>
/// Constructor
/// </summary>
/// <param name="options">Options.</param>
/// <param name="serializer">Serializer. This must be the same serializer that was used to create the file cache.</param>
/// <param name="memoryCache">Memory cache</param>
/// <param name="fileCache">File cache. Can pass NullFileCache to skip file caching layer. Recommend to use SSD only for this.</param>
/// <param name="distributedCache">Distributed cache</param>
/// <param name="clock">System clock</param>
/// <param name="logger">Logger</param>
public LayeredCache(LayeredCacheOptions options,
ISerializer serializer,
IMemoryCache memoryCache,
IFileCache fileCache,
IDistributedCache distributedCache,
TimeProvider clock,
ILogger<LayeredCache> logger)
{
this.keyPrefix = (options.KeyPrefix ?? string.Empty) + ":";
this.serializer = serializer;
this.memoryCache = memoryCache;
this.fileCache = fileCache;
this.distributedCache = distributedCache;
this.clock = clock;
this.logger = logger;
// create collapser, this will ensure keys do not cache storm
// wrap this class (the cache policy) behind the collapser policy
this.cachePolicy = AsyncRequestCollapserPolicy.Create(this);
// circuit break if distributed cache goes down, re-enable circuit attempts after 5 seconds
distributedCacheCircuitBreakPolicy = Policy.Handle<Exception>().CircuitBreakerAsync(5, TimeSpan.FromSeconds(5.0));
this.distributedCache.KeyChanged += DistributedCacheKeyChanged;
}
/// <inheritdoc />
public void Dispose()
{
distributedCache.KeyChanged -= DistributedCacheKeyChanged;
}
/// <inheritdoc />
public async Task<T?> GetAsync<T>(string key, CancellationToken cancelToken = default)
{
ValidateType<T>();
key = FormatKey<T>(key);
// L1 lookup (RAM)
if (memoryCache.TryGetValue<T>(key, out var memoryResult))
{
logger.LogDebug("Memory cache hit for {key}", key);
return memoryResult;
}
logger.LogDebug("Memory cache miss for {key}", key);
// L2 lookup (file)
var fileResult = await fileCache.GetAsync<T>(key, cancelToken);
if (fileResult is not null)
{
return fileResult.Item;
}
// L3 lookup (redis)
DistributedCacheItem distributedCacheItem = default;
try
{
distributedCacheItem = await distributedCacheCircuitBreakPolicy.ExecuteAsync(() => distributedCache.GetAsync(key, cancelToken));
}
catch (Exception ex)
{
var method = nameof(GetAsync);
var type = typeof(T).FullName ?? string.Empty;
logger.LogError(ex, "Distributed cache error on {method}, {key}, {type}", method, key, type);
}
// not found from distributed cache, give up
if (!distributedCacheItem.HasValue)
{
return default;
}
// deserialize and return value from distributed cache
var deserializedResult = DeserializeObject<T>(distributedCacheItem.Bytes);
return deserializedResult;
}
/// <inheritdoc />
public async Task SetAsync<T>(string key, T obj, CacheParameters cacheParam, CancellationToken cancelToken = default)
{
ValidateType<T>();
if (obj is null)
{
return;
}
key = FormatKey<T>(key);
var distributedCacheBytes = SerializeObject(obj);
// L1 cache (RAM)
memoryCache.Set(key, obj, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = cacheParam.Duration,
Size = cacheParam.Size
});
logger.LogDebug("Memory cache set {key}", key);
// L2 cache (file)
await fileCache.SetAsync(key, distributedCacheBytes, cacheParam, cancelToken);
// L3 cache (redis)
try
{
await distributedCacheCircuitBreakPolicy.ExecuteAsync(() => distributedCache.SetAsync(key, new DistributedCacheItem
{
Bytes = distributedCacheBytes,
Expiry = cacheParam.Duration
}));
}
catch (Exception ex)
{
// don't fail the call, we can stomach redis being down
var method = nameof(SetAsync);
var type = typeof(T).FullName ?? string.Empty;
logger.LogError(ex, "Distributed cache error on {method}, {key}, {type}", method, key, type);
}
}
/// <inheritdoc />
public async Task DeleteAsync<T>(string key, CancellationToken cancelToken = default)
{
ValidateType<T>();
key = FormatKey<T>(key);
// L1 cache (RAM)
memoryCache.Remove(key);
logger.LogDebug("Memory cache deleted {key}", key);
// L2 cache (file)
await fileCache.RemoveAsync(key, cancelToken);
// L3 cache (redis)
// note- unlike SetAsync, we don't catch the exception here, a deletion that fails in distributed cache is bad and we need it to propagate all the way out
await distributedCacheCircuitBreakPolicy.ExecuteAsync(() => distributedCache.DeleteAsync(key, cancelToken));
}
/// <inheritdoc />
public Task<T?> GetOrCreateAsync<T>(string key, Func<GetOrCreateAsyncContext, Task<T?>> factory,
object? state = null, CancellationToken cancelToken = default)
{
logger.LogDebug("Layered cache get or create {key}", key);
ValidateType<T>();
key = FormatKey<T>(key);
var pollyContext = new Context(key);
// collapse with polly to prevent duplicate callers for the same key
var outterLazy = cachePolicy.ExecuteAsync<Lazy<Task<T?>>>(async (pollyContext, cancelToken) =>
{
// fast path, check memory cache first
if (memoryCache.TryGetValue<T>(key, out var fastPathObj))
{
logger.LogDebug("Layered cache get or create {key} fast path hit", key);
return new Lazy<Task<T?>>(Task.FromResult<T?>(fastPathObj));
}
// not in the memory cache, slow path...
var getOrCreateContext = new GetOrCreateAsyncContext(key, state, cancelToken);
// factory method that gets the item from other caches, or creates the item if needed
async Task<T?> innerFactory()
{
logger.LogDebug("Memory cache get or create miss {key}", key);
try
{
// check file cache (L2)
var fileItem = await fileCache.GetAsync<T>(key, cancelToken);
if (fileItem is not null)
{
// set the size and expiration
getOrCreateContext.Size = fileItem.Size * 2;
getOrCreateContext.Duration = fileItem.Expires - clock.GetUtcNow();
return fileItem.Item;
}
}
catch (Exception ex)
{
// eat error but log it, we don't want serializer or disk error to fail the entire call
var method = nameof(GetOrCreateAsync);
var type = typeof(T).FullName ?? string.Empty;
logger.LogError(ex, "File cache read error on {method}, {key}, {type}", method, key, type);
}
try
{
// check distributed cache (L3)
DistributedCacheItem distributedCacheItem = await distributedCacheCircuitBreakPolicy.ExecuteAsync(() => distributedCache.GetAsync(key, cancelToken));
if (distributedCacheItem.HasValue)
{
logger.LogDebug("Get or create {key} in distributed cache", key);
// grabbed from distributed cache, use that value and don't invoke the factory
var result = DeserializeObject<T>(distributedCacheItem.Bytes) ?? throw new IOException("Failed to deserialize object of type " + typeof(T).FullName);
getOrCreateContext.Size = distributedCacheItem.Bytes.Length * 2;
getOrCreateContext.Duration = distributedCacheItem.Expiry ?? CacheParameters.DefaultDuration;
return result;
}
}
catch (Exception ex)
{
// eat error but log it, we don't want serializer or redis to fail the entire call
var method = nameof(GetOrCreateAsync);
var type = typeof(T).FullName ?? string.Empty;
logger.LogError(ex, "Distributed cache read error on {method}, {key}, {type}", method, key, type);
}
// get the item from the factory
return await factory(getOrCreateContext);
}
// store a memory cache entry for the lazy task so that future callers collapse on to it first
string lazyKey = key + "_Lazy";
var innerLazy = await memoryCache.GetOrCreateAsync<Lazy<Task<T?>>>(lazyKey, cacheEntry =>
{
// estimate of state and reference objects captured in inner factory
cacheEntry.Size = 256;
cacheEntry.AbsoluteExpirationRelativeToNow = defaultCacheTime;
var lazyEntry = new Lazy<Task<T?>>(async () =>
{
try
{
var result = await innerFactory();
if (result is null)
{
// no item found, do not set in any caches
return default;
}
// L1 cache (RAM)
memoryCache.Set(key, result, new MemoryCacheEntryOptions
{
Size = getOrCreateContext.Size,
AbsoluteExpirationRelativeToNow = getOrCreateContext.Duration
});
// a serialization error here is a problem
var distributedCacheBytes = SerializeObject(result);
try
{
// L2 cache (file)
// file cache can take raw bytes and will not additional serialization on them
await fileCache.SetAsync(key, distributedCacheBytes, getOrCreateContext.CacheParameters.Duration, cancelToken);
// L3 cache (redis)
await distributedCacheCircuitBreakPolicy.ExecuteAsync(() =>
{
return distributedCache.SetAsync(key, new DistributedCacheItem
{
Bytes = distributedCacheBytes,
Expiry = getOrCreateContext.CacheParameters.Duration
});
});
}
catch (Exception ex)
{
// don't fail the call, we can stomach file or redis cache being down
var method = nameof(GetOrCreateAsync);
var type = typeof(T).FullName ?? string.Empty;
logger.LogError(ex, "Distributed cache write error on {method}, {key}, {type}", method, key, type);
}
return result;
}
catch (Exception ex)
{
string type = typeof(T).FullName ?? string.Empty;
logger.LogError(ex, "Error executing factory or serializer for type {type}", type);
await DeleteAsync<T>(key, cancelToken);
throw;
}
finally
{
// all done, get the lazy key out of there
memoryCache.Remove(lazyKey);
}
});
return Task.FromResult<Lazy<Task<T?>>>(lazyEntry);
});
return innerLazy!;
}, pollyContext, cancelToken);
return outterLazy.Result.Value;
}
private void DistributedCacheKeyChanged(string key)
{
// magic key to indicate a flush all operation
if (key.Contains("__flushall__"))
{
logger.LogWarning("Received a flush all command, purging memory and file cache");
(memoryCache as MemoryCache)?.Compact(1.0);
fileCache.ClearAsync().GetAwaiter(); // this can run in the background
}
else if (key.StartsWith(keyPrefix))
{
memoryCache.Remove(key);
fileCache.RemoveAsync(key).GetAwaiter().GetResult();
logger.LogDebug("Distributed cache key changed: {key}, removed from memory and file cache", key);
}
else
{
logger.LogDebug("Ignoring distributed cache key change: {key}, not prefixed with {keyPrefix}", key, keyPrefix);
}
}
/// <inheritdoc />
string IKeyStrategy.GetKey(Context context)
{
// get the key to collapse on
return context.OperationKey;
}
private T? DeserializeObject<T>(byte[] bytes)
{
return (T?)serializer.Deserialize(bytes, typeof(T?));
}
private byte[] SerializeObject(object obj)
{
return serializer.Serialize(obj) ?? throw new IOException("Failed to serialize object of type " + obj.GetType().FullName); ;
}
private string FormatKey<T>(string key)
{
return $"{keyPrefix}{typeof(T).FullName}:{serializer.Description}:{key}";
}
private static void ValidateType<T>()
{
Type t = typeof(T);
if (t.IsInterface)
{
throw new InvalidOperationException("Interfaces cannot be cached");
}
}
}
/// <summary>
/// Context for GetOrCreateAsync
/// </summary>
/// <remarks>
/// Constructor
/// </remarks>
/// <param name="key">Key</param>
/// <param name="state">State</param>
/// <param name="cancelToken">Cancel token</param>
public class GetOrCreateAsyncContext(string key, object? state, CancellationToken cancelToken)
{
/// <summary>
/// Cache key
/// </summary>
public string Key { get; } = key;
/// <summary>
/// State
/// </summary>
public object? State { get; } = state;
/// <summary>
/// Cache parameters. You can set these to a new value for duration and size inside the factory method
/// </summary>
public CacheParameters CacheParameters { get; set; }
/// <summary>
/// Cancellation token
/// </summary>
public CancellationToken CancelToken { get; } = cancelToken;
/// <summary>
/// Get/set duration on the cache parameters
/// </summary>
public TimeSpan Duration
{
get => CacheParameters.Duration;
set => CacheParameters = new(value, CacheParameters.Size);
}
/// <summary>
/// Get/set size on the cache parameters
/// </summary>
public int Size
{
get => CacheParameters.Size;
set => CacheParameters = new(CacheParameters.Duration, value);
}
}