-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
HybridCacheClient.cs
340 lines (292 loc) · 15.2 KB
/
HybridCacheClient.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Messaging;
using Foundatio.Utility;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Foundatio.Caching;
public interface IHybridCacheClient : ICacheClient { }
public class HybridCacheClient : IHybridCacheClient
{
protected readonly ICacheClient _distributedCache;
protected readonly IMessageBus _messageBus;
private readonly string _cacheId = Guid.NewGuid().ToString("N");
private readonly InMemoryCacheClient _localCache;
private readonly ILogger _logger;
private long _localCacheHits;
private long _invalidateCacheCalls;
public HybridCacheClient(ICacheClient distributedCacheClient, IMessageBus messageBus, InMemoryCacheClientOptions localCacheOptions = null, ILoggerFactory loggerFactory = null)
{
_logger = loggerFactory?.CreateLogger<HybridCacheClient>() ?? NullLogger<HybridCacheClient>.Instance;
_distributedCache = distributedCacheClient;
_messageBus = messageBus;
_messageBus.SubscribeAsync<InvalidateCache>(OnRemoteCacheItemExpiredAsync).AnyContext().GetAwaiter().GetResult();
if (localCacheOptions == null)
localCacheOptions = new InMemoryCacheClientOptions { LoggerFactory = loggerFactory };
_localCache = new InMemoryCacheClient(localCacheOptions);
_localCache.ItemExpired.AddHandler(OnLocalCacheItemExpiredAsync);
}
public InMemoryCacheClient LocalCache => _localCache;
public long LocalCacheHits => _localCacheHits;
public long InvalidateCacheCalls => _invalidateCacheCalls;
private Task OnLocalCacheItemExpiredAsync(object sender, ItemExpiredEventArgs args)
{
if (!args.SendNotification)
return Task.CompletedTask;
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Local cache expired event: key={Key}", args.Key);
return _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { args.Key }, Expired = true });
}
private Task OnRemoteCacheItemExpiredAsync(InvalidateCache message)
{
if (!String.IsNullOrEmpty(message.CacheId) && String.Equals(_cacheId, message.CacheId))
return Task.CompletedTask;
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Invalidating local cache from remote: id={CacheId} expired={Expired} keys={Keys}", message.CacheId, message.Expired, String.Join(",", message.Keys ?? new string[] { }));
Interlocked.Increment(ref _invalidateCacheCalls);
if (message.FlushAll)
{
_logger.LogTrace("Flushed local cache");
return _localCache.RemoveAllAsync();
}
if (message.Keys != null && message.Keys.Length > 0)
{
var tasks = new List<Task>(message.Keys.Length);
var keysToRemove = new List<string>(message.Keys.Length);
foreach (string key in message.Keys)
{
if (message.Expired)
_localCache.RemoveExpiredKey(key, false);
else if (key.EndsWith("*"))
tasks.Add(_localCache.RemoveByPrefixAsync(key.Substring(0, key.Length - 1)));
else
keysToRemove.Add(key);
}
if (keysToRemove.Count > 0)
tasks.Add(_localCache.RemoveAllAsync(keysToRemove));
return Task.WhenAll(tasks);
}
_logger.LogWarning("Unknown invalidate cache message");
return Task.CompletedTask;
}
public async Task<bool> RemoveAsync(string key)
{
var removed = await _distributedCache.RemoveAsync(key).AnyContext();
await _localCache.RemoveAsync(key).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return removed;
}
public async Task<bool> RemoveIfEqualAsync<T>(string key, T expected)
{
var removed = await _distributedCache.RemoveAsync(key).AnyContext();
await _localCache.RemoveAsync(key).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return removed;
}
public async Task<int> RemoveAllAsync(IEnumerable<string> keys = null)
{
var items = keys?.ToArray();
bool flushAll = items == null || items.Length == 0;
var removed = await _distributedCache.RemoveAllAsync(items).AnyContext();
await _localCache.RemoveAllAsync(items).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, FlushAll = flushAll, Keys = items }).AnyContext();
return removed;
}
public async Task<int> RemoveByPrefixAsync(string prefix)
{
var removed = await _distributedCache.RemoveByPrefixAsync(prefix).AnyContext();
await _localCache.RemoveByPrefixAsync(prefix).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { prefix + "*" } }).AnyContext();
return removed;
}
public async Task<CacheValue<T>> GetAsync<T>(string key)
{
var cacheValue = await _localCache.GetAsync<T>(key).AnyContext();
if (cacheValue.HasValue)
{
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Local cache hit: {Key}", key);
Interlocked.Increment(ref _localCacheHits);
return cacheValue;
}
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Local cache miss: {Key}", key);
cacheValue = await _distributedCache.GetAsync<T>(key).AnyContext();
if (cacheValue.HasValue)
{
var expiration = await _distributedCache.GetExpirationAsync(key).AnyContext();
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Setting Local cache key: {Key} with expiration: {Expiration}", key, expiration);
await _localCache.SetAsync(key, cacheValue.Value, expiration).AnyContext();
return cacheValue;
}
return cacheValue.HasValue ? cacheValue : CacheValue<T>.NoValue;
}
public Task<IDictionary<string, CacheValue<T>>> GetAllAsync<T>(IEnumerable<string> keys)
{
return _distributedCache.GetAllAsync<T>(keys);
}
public async Task<bool> AddAsync<T>(string key, T value, TimeSpan? expiresIn = null)
{
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Adding key {Key} to local cache with expiration: {Expiration}", key, expiresIn);
bool added = await _distributedCache.AddAsync(key, value, expiresIn).AnyContext();
if (added)
await _localCache.SetAsync(key, value, expiresIn).AnyContext();
return added;
}
public async Task<bool> SetAsync<T>(string key, T value, TimeSpan? expiresIn = null)
{
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Setting key {Key} to local cache with expiration: {Expiration}", key, expiresIn);
await _localCache.SetAsync(key, value, expiresIn).AnyContext();
var set = await _distributedCache.SetAsync(key, value, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return set;
}
public async Task<int> SetAllAsync<T>(IDictionary<string, T> values, TimeSpan? expiresIn = null)
{
if (values == null || values.Count == 0)
return 0;
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Adding keys {Keys} to local cache with expiration: {Expiration}", values.Keys, expiresIn);
await _localCache.SetAllAsync(values, expiresIn).AnyContext();
var set = await _distributedCache.SetAllAsync(values, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = values.Keys.ToArray() }).AnyContext();
return set;
}
public async Task<bool> ReplaceAsync<T>(string key, T value, TimeSpan? expiresIn = null)
{
await _localCache.ReplaceAsync(key, value, expiresIn).AnyContext();
bool replaced = await _distributedCache.ReplaceAsync(key, value, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return replaced;
}
public async Task<bool> ReplaceIfEqualAsync<T>(string key, T value, T expected, TimeSpan? expiresIn = null)
{
await _localCache.ReplaceAsync(key, value, expiresIn).AnyContext();
bool replaced = await _distributedCache.ReplaceAsync(key, value, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return replaced;
}
public async Task<double> IncrementAsync(string key, double amount, TimeSpan? expiresIn = null)
{
double incremented = await _distributedCache.IncrementAsync(key, amount, expiresIn).AnyContext();
await _localCache.ReplaceAsync(key, incremented, expiresIn);
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return incremented;
}
public async Task<long> IncrementAsync(string key, long amount, TimeSpan? expiresIn = null)
{
long incremented = await _distributedCache.IncrementAsync(key, amount, expiresIn).AnyContext();
await _localCache.ReplaceAsync(key, incremented, expiresIn);
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return incremented;
}
public Task<bool> ExistsAsync(string key)
{
return _distributedCache.ExistsAsync(key);
}
public Task<TimeSpan?> GetExpirationAsync(string key)
{
return _distributedCache.GetExpirationAsync(key);
}
public async Task SetExpirationAsync(string key, TimeSpan expiresIn)
{
await _localCache.SetExpirationAsync(key, expiresIn).AnyContext();
await _distributedCache.SetExpirationAsync(key, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
}
public async Task<double> SetIfHigherAsync(string key, double value, TimeSpan? expiresIn = null)
{
await _localCache.RemoveAsync(key).AnyContext();
double difference = await _distributedCache.SetIfHigherAsync(key, value, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return difference;
}
public async Task<long> SetIfHigherAsync(string key, long value, TimeSpan? expiresIn = null)
{
await _localCache.RemoveAsync(key).AnyContext();
long difference = await _distributedCache.SetIfHigherAsync(key, value, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return difference;
}
public async Task<double> SetIfLowerAsync(string key, double value, TimeSpan? expiresIn = null)
{
await _localCache.RemoveAsync(key).AnyContext();
double difference = await _distributedCache.SetIfLowerAsync(key, value, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return difference;
}
public async Task<long> SetIfLowerAsync(string key, long value, TimeSpan? expiresIn = null)
{
await _localCache.RemoveAsync(key).AnyContext();
long difference = await _distributedCache.SetIfLowerAsync(key, value, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return difference;
}
public async Task<long> ListAddAsync<T>(string key, IEnumerable<T> values, TimeSpan? expiresIn = null)
{
if (values is string stringValue)
{
await _localCache.ListAddAsync(key, stringValue, expiresIn).AnyContext();
long set = await _distributedCache.ListAddAsync(key, stringValue, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return set;
}
else
{
var items = values?.ToArray();
await _localCache.ListAddAsync(key, items, expiresIn).AnyContext();
long set = await _distributedCache.ListAddAsync(key, items, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return set;
}
}
public async Task<long> ListRemoveAsync<T>(string key, IEnumerable<T> values, TimeSpan? expiresIn = null)
{
if (values is string stringValue)
{
await _localCache.ListRemoveAsync(key, stringValue, expiresIn).AnyContext();
long removed = await _distributedCache.ListRemoveAsync(key, stringValue, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return removed;
}
else
{
var items = values?.ToArray();
await _localCache.ListRemoveAsync(key, items, expiresIn).AnyContext();
long removed = await _distributedCache.ListRemoveAsync(key, items, expiresIn).AnyContext();
await _messageBus.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = new[] { key } }).AnyContext();
return removed;
}
}
public async Task<CacheValue<ICollection<T>>> GetListAsync<T>(string key, int? page = null, int pageSize = 100)
{
var cacheValue = await _localCache.GetListAsync<T>(key, page, pageSize).AnyContext();
if (cacheValue.HasValue)
{
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Local cache hit: {Key}", key);
Interlocked.Increment(ref _localCacheHits);
return cacheValue;
}
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Local cache miss: {Key}", key);
cacheValue = await _distributedCache.GetListAsync<T>(key, page, pageSize).AnyContext();
if (cacheValue.HasValue)
{
var expiration = await _distributedCache.GetExpirationAsync(key).AnyContext();
if (_logger.IsEnabled(LogLevel.Trace)) _logger.LogTrace("Setting Local cache key: {Key} with expiration: {Expiration}", key, expiration);
await _localCache.ListAddAsync(key, cacheValue.Value, expiration).AnyContext();
return cacheValue;
}
return cacheValue.HasValue ? cacheValue : CacheValue<ICollection<T>>.NoValue;
}
public virtual void Dispose()
{
_localCache.ItemExpired.RemoveHandler(OnLocalCacheItemExpiredAsync);
_localCache.Dispose();
// TODO: unsubscribe handler from messagebus.
}
public class InvalidateCache
{
public string CacheId { get; set; }
public string[] Keys { get; set; }
public bool FlushAll { get; set; }
public bool Expired { get; set; }
}
}