-
-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathBaseCacheManager.cs
More file actions
860 lines (719 loc) · 30.1 KB
/
BaseCacheManager.cs
File metadata and controls
860 lines (719 loc) · 30.1 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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using CacheManager.Core.Internal;
using Microsoft.Extensions.Logging;
using static CacheManager.Core.Utility.Guard;
namespace CacheManager.Core
{
/// <summary>
/// The <see cref="BaseCacheManager{TCacheValue}"/> implements <see cref="ICacheManager{TCacheValue}"/> and is the main class
/// of this library.
/// The cache manager delegates all cache operations to the list of <see cref="BaseCacheHandle{T}"/>'s which have been
/// added. It will keep them in sync according to rules and depending on the configuration.
/// </summary>
/// <typeparam name="TCacheValue">The type of the cache value.</typeparam>
public partial class BaseCacheManager<TCacheValue> : BaseCache<TCacheValue>, ICacheManager<TCacheValue>, IDisposable
{
private class NullLoggerFactory : ILoggerFactory
{
public void AddProvider(ILoggerProvider provider)
{
}
public ILogger CreateLogger(string categoryName)
{
return new NullLogger();
}
public void Dispose()
{
}
}
private class NullLogger : ILogger<BaseCacheManager<TCacheValue>>
{
public IDisposable BeginScope<TState>(TState state) => null;
public bool IsEnabled(LogLevel logLevel) => false;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
{
// do nothing;
}
}
private readonly bool _logTrace = false;
private readonly BaseCacheHandle<TCacheValue>[] _cacheHandles;
private readonly CacheBackplane _cacheBackplane;
/// <summary>
/// Initializes a new instance of the <see cref="BaseCacheManager{TCacheValue}"/> class
/// using the specified <paramref name="configuration"/>.
/// If the name of the <paramref name="configuration"/> is defined, the cache manager will
/// use it. Otherwise a random string will be generated.
/// </summary>
/// <param name="configuration">
/// The configuration which defines the structure and complexity of the cache manager.
/// </param>
/// <param name="loggerFactory">Optional logger factory.</param>
/// <exception cref="System.ArgumentNullException">
/// When <paramref name="configuration"/> is null.
/// </exception>
/// <see cref="CacheFactory"/>
/// <see cref="CacheConfigurationBuilder"/>
/// <see cref="BaseCacheHandle{TCacheValue}"/>
public BaseCacheManager(ICacheManagerConfiguration configuration, ILoggerFactory loggerFactory = null)
: this(configuration?.Name ?? Guid.NewGuid().ToString(), configuration, loggerFactory ?? new NullLoggerFactory())
{
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2200:RethrowToPreserveStackDetails", Justification = "fine for now")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "nope")]
private BaseCacheManager(string name, ICacheManagerConfiguration configuration, ILoggerFactory loggerFactory)
{
NotNullOrWhiteSpace(name, nameof(name));
NotNull(configuration, nameof(configuration));
NotNull(loggerFactory, nameof(loggerFactory));
Name = name;
Configuration = configuration;
var serializer = CacheReflectionHelper.CreateSerializer(configuration, loggerFactory);
Logger = loggerFactory.CreateLogger(this.GetType());
_logTrace = Logger.IsEnabled(LogLevel.Trace);
Logger.LogInformation("Cache manager: adding cache handles...");
try
{
_cacheHandles = CacheReflectionHelper.CreateCacheHandles(this, loggerFactory, serializer).ToArray();
var index = 0;
foreach (var handle in _cacheHandles)
{
var handleIndex = index;
handle.OnCacheSpecificRemove += (sender, args) =>
{
// added sync for using backplane with in-memory caches on cache specific removal
// but commented for now, this is not really needed if all instances use the same expiration etc, would just cause duplicated events
////if (_cacheBackplane != null && handle.Configuration.IsBackplaneSource && !handle.IsDistributedCache)
////{
//// if (string.IsNullOrEmpty(args.Region))
//// {
//// _cacheBackplane.NotifyRemove(args.Key);
//// }
//// else
//// {
//// _cacheBackplane.NotifyRemove(args.Key, args.Region);
//// }
////}
// base cache handle does logging for this
if (Configuration.UpdateMode == CacheUpdateMode.Up)
{
if (_logTrace)
{
Logger.LogTrace("Cleaning handles above '{0}' because of remove event.", handleIndex);
}
EvictFromHandlesAbove(args.Key, args.Region, handleIndex);
}
// moving down below cleanup, otherwise the item could still be in memory
TriggerOnRemoveByHandle(args.Key, args.Region, args.Reason, handleIndex + 1, args.Value);
};
index++;
}
_cacheBackplane = CacheReflectionHelper.CreateBackplane(configuration, loggerFactory);
if (_cacheBackplane != null)
{
RegisterCacheBackplane(_cacheBackplane);
}
}
catch (Exception ex)
{
Logger.LogError(ex, "Error occurred while creating the cache manager.");
throw ex.InnerException ?? ex;
}
}
/// <inheritdoc />
public event EventHandler<CacheActionEventArgs> OnAdd;
/// <inheritdoc />
public event EventHandler<CacheClearEventArgs> OnClear;
/// <inheritdoc />
public event EventHandler<CacheClearRegionEventArgs> OnClearRegion;
/// <inheritdoc />
public event EventHandler<CacheActionEventArgs> OnGet;
/// <inheritdoc />
public event EventHandler<CacheActionEventArgs> OnPut;
/// <inheritdoc />
public event EventHandler<CacheActionEventArgs> OnRemove;
/// <inheritdoc />
public event EventHandler<CacheItemRemovedEventArgs> OnRemoveByHandle;
/// <inheritdoc />
public event EventHandler<CacheActionEventArgs> OnUpdate;
/// <inheritdoc />
public IReadOnlyCacheManagerConfiguration Configuration { get; }
/// <inheritdoc />
public IEnumerable<BaseCacheHandle<TCacheValue>> CacheHandles
=> new ReadOnlyCollection<BaseCacheHandle<TCacheValue>>(
new List<BaseCacheHandle<TCacheValue>>(
_cacheHandles));
/// <summary>
/// Gets the configured cache backplane.
/// </summary>
/// <value>The backplane.</value>
public CacheBackplane Backplane => _cacheBackplane;
/// <summary>
/// Gets the cache name.
/// </summary>
/// <value>The name of the cache.</value>
public string Name { get; }
/// <inheritdoc />
protected override ILogger Logger { get; }
/// <inheritdoc />
public override void Clear()
{
CheckDisposed();
if (_logTrace)
{
Logger.LogTrace("Clear: flushing cache...");
}
foreach (var handle in _cacheHandles)
{
if (_logTrace)
{
Logger.LogTrace("Clear: clearing handle {0}.", handle.Configuration.Name);
}
handle.Clear();
handle.Stats.OnClear();
}
if (_cacheBackplane != null)
{
if (_logTrace)
{
Logger.LogTrace("Clear: notifies backplane.");
}
_cacheBackplane.NotifyClear();
}
TriggerOnClear();
}
/// <inheritdoc />
public override void ClearRegion(string region)
{
NotNullOrWhiteSpace(region, nameof(region));
CheckDisposed();
if (_logTrace)
{
Logger.LogTrace("Clear region: {0}.", region);
}
foreach (var handle in _cacheHandles)
{
if (_logTrace)
{
Logger.LogTrace("Clear region: {0} in handle {1}.", region, handle.Configuration.Name);
}
handle.ClearRegion(region);
handle.Stats.OnClearRegion(region);
}
if (_cacheBackplane != null)
{
if (_logTrace)
{
Logger.LogTrace("Clear region: {0}: notifies backplane [clear region].", region);
}
_cacheBackplane.NotifyClearRegion(region);
}
TriggerOnClearRegion(region);
}
/// <inheritdoc />
public override bool Exists(string key)
{
foreach (var handle in _cacheHandles)
{
if (_logTrace)
{
Logger.LogTrace("Checking if [{0}] exists on handle '{1}'.", key, handle.Configuration.Name);
}
if (handle.Exists(key))
{
return true;
}
}
return false;
}
/// <inheritdoc />
public override bool Exists(string key, string region)
{
foreach (var handle in _cacheHandles)
{
if (_logTrace)
{
Logger.LogTrace("Checking if [{0}:{1}] exists on handle '{2}'.", region, key, handle.Configuration.Name);
}
if (handle.Exists(key, region))
{
return true;
}
}
return false;
}
/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="string" /> that represents this instance.
/// </returns>
public override string ToString() =>
string.Format(CultureInfo.InvariantCulture, "Name: {0}, Handles: [{1}]", Name, string.Join(",", _cacheHandles.Select(p => p.GetType().Name)));
/// <inheritdoc />
protected internal override bool AddInternal(CacheItem<TCacheValue> item)
{
NotNull(item, nameof(item));
CheckDisposed();
if (_logTrace)
{
Logger.LogTrace("Add [{0}] started.", item);
}
var handleIndex = _cacheHandles.Length - 1;
var result = AddItemToHandle(item, _cacheHandles[handleIndex]);
// evict from other handles in any case because if it exists, it might be a different version
// if not exist, its just a sanity check to invalidate other versions in upper layers.
EvictFromOtherHandles(item.Key, item.Region, handleIndex);
if (result)
{
// update backplane
if (_cacheBackplane != null)
{
if (string.IsNullOrWhiteSpace(item.Region))
{
_cacheBackplane.NotifyChange(item.Key, CacheItemChangedEventAction.Add);
}
else
{
_cacheBackplane.NotifyChange(item.Key, item.Region, CacheItemChangedEventAction.Add);
}
if (_logTrace)
{
Logger.LogTrace("Notified backplane 'change' because [{0}] was added.", item);
}
}
// trigger only once and not per handle and only if the item was added!
TriggerOnAdd(item.Key, item.Region);
}
return result;
}
/// <inheritdoc />
protected internal override void PutInternal(CacheItem<TCacheValue> item)
{
NotNull(item, nameof(item));
CheckDisposed();
if (_logTrace)
{
Logger.LogTrace("Put [{0}] started.", item);
}
foreach (var handle in _cacheHandles)
{
if (handle.Configuration.EnableStatistics)
{
// check if it is really a new item otherwise the items count is crap because we
// count it every time, but use only the current handle to retrieve the item,
// otherwise we would trigger gets and find it in another handle maybe
var oldItem = string.IsNullOrWhiteSpace(item.Region) ?
handle.GetCacheItem(item.Key) :
handle.GetCacheItem(item.Key, item.Region);
handle.Stats.OnPut(item, oldItem == null);
}
if (_logTrace)
{
Logger.LogTrace(
"Put [{0}:{1}] successfully to handle '{2}'.",
item.Region,
item.Key,
handle.Configuration.Name);
}
handle.Put(item);
}
// update backplane
if (_cacheBackplane != null)
{
if (_logTrace)
{
Logger.LogTrace("Put [{0}:{1}] was scuccessful. Notifying backplane [change].", item.Region, item.Key);
}
if (string.IsNullOrWhiteSpace(item.Region))
{
_cacheBackplane.NotifyChange(item.Key, CacheItemChangedEventAction.Put);
}
else
{
_cacheBackplane.NotifyChange(item.Key, item.Region, CacheItemChangedEventAction.Put);
}
}
TriggerOnPut(item.Key, item.Region);
}
/// <inheritdoc />
protected override void Dispose(bool disposeManaged)
{
if (disposeManaged)
{
foreach (var handle in _cacheHandles)
{
handle.Dispose();
}
if (_cacheBackplane != null)
{
_cacheBackplane.Dispose();
}
}
base.Dispose(disposeManaged);
}
/// <inheritdoc />
protected override CacheItem<TCacheValue> GetCacheItemInternal(string key) =>
GetCacheItemInternal(key, null);
/// <inheritdoc />
protected override CacheItem<TCacheValue> GetCacheItemInternal(string key, string region)
{
CheckDisposed();
CacheItem<TCacheValue> cacheItem = null;
if (_logTrace)
{
Logger.LogTrace("Get [{0}:{1}] started.", region, key);
}
for (var handleIndex = 0; handleIndex < _cacheHandles.Length; handleIndex++)
{
var handle = _cacheHandles[handleIndex];
if (string.IsNullOrWhiteSpace(region))
{
cacheItem = handle.GetCacheItem(key);
}
else
{
cacheItem = handle.GetCacheItem(key, region);
}
handle.Stats.OnGet(region);
if (cacheItem != null)
{
if (_logTrace)
{
Logger.LogTrace("Get [{0}:{1}], found in handle[{2}] '{3}'.", region, key, handleIndex, handle.Configuration.Name);
}
// update last accessed, might be used for custom sliding implementations
cacheItem.LastAccessedUtc = DateTime.UtcNow;
// update other handles if needed
AddToHandles(cacheItem, handleIndex);
handle.Stats.OnHit(region);
TriggerOnGet(key, region);
break;
}
else
{
if (_logTrace)
{
Logger.LogTrace("Get [{0}:{1}], item NOT found in handle[{2}] '{3}'.", region, key, handleIndex, handle.Configuration.Name);
}
handle.Stats.OnMiss(region);
}
}
return cacheItem;
}
/// <inheritdoc />
protected override bool RemoveInternal(string key) =>
RemoveInternal(key, null);
/// <inheritdoc />
protected override bool RemoveInternal(string key, string region)
{
CheckDisposed();
var result = false;
if (_logTrace)
{
Logger.LogTrace("Removing [{0}:{1}].", region, key);
}
foreach (var handle in _cacheHandles)
{
var handleResult = false;
if (!string.IsNullOrWhiteSpace(region))
{
handleResult = handle.Remove(key, region);
}
else
{
handleResult = handle.Remove(key);
}
if (handleResult)
{
if (_logTrace)
{
Logger.LogTrace(
"Remove [{0}:{1}], successfully removed from handle '{2}'.",
region,
key,
handle.Configuration.Name);
}
result = true;
handle.Stats.OnRemove(region);
}
}
// See #311, send remove events anyways if the backlane is configured but no distributed cache is used.
var inProcOnly = _cacheHandles.Any(p => !p.IsDistributedCache);
if (result || inProcOnly)
{
// update backplane
if (_cacheBackplane != null)
{
if (_logTrace)
{
Logger.LogTrace("Removed [{0}:{1}], notifying backplane [remove].", region, key);
}
if (string.IsNullOrWhiteSpace(region))
{
_cacheBackplane.NotifyRemove(key);
}
else
{
_cacheBackplane.NotifyRemove(key, region);
}
}
if (result)
{
// trigger only once and not per handle
TriggerOnRemove(key, region);
}
}
return result;
}
private static bool AddItemToHandle(CacheItem<TCacheValue> item, BaseCacheHandle<TCacheValue> handle)
{
if (handle.Add(item))
{
handle.Stats.OnAdd(item);
return true;
}
return false;
}
private static void ClearHandles(BaseCacheHandle<TCacheValue>[] handles)
{
foreach (var handle in handles)
{
handle.Clear();
handle.Stats.OnClear();
}
////this.TriggerOnClear();
}
private static void ClearRegionHandles(string region, BaseCacheHandle<TCacheValue>[] handles)
{
foreach (var handle in handles)
{
handle.ClearRegion(region);
handle.Stats.OnClearRegion(region);
}
////this.TriggerOnClearRegion(region);
}
private void EvictFromHandles(string key, string region, BaseCacheHandle<TCacheValue>[] handles)
{
foreach (var handle in handles)
{
EvictFromHandle(key, region, handle);
}
}
private void EvictFromHandle(string key, string region, BaseCacheHandle<TCacheValue> handle)
{
if (Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug(
"Evicting '{0}:{1}' from handle '{2}'.",
region,
key,
handle.Configuration.Name);
}
bool result;
if (string.IsNullOrWhiteSpace(region))
{
result = handle.Remove(key);
}
else
{
result = handle.Remove(key, region);
}
if (result)
{
handle.Stats.OnRemove(region);
}
}
private void AddToHandles(CacheItem<TCacheValue> item, int foundIndex)
{
if (_logTrace)
{
Logger.LogTrace(
"Start updating handles with [{0}].",
item);
}
if (foundIndex == 0)
{
return;
}
// update all cache handles with lower order, up the list
for (var handleIndex = 0; handleIndex < _cacheHandles.Length; handleIndex++)
{
if (handleIndex < foundIndex)
{
if (_logTrace)
{
Logger.LogTrace("Updating handles, added [{0}] to handle '{1}'.", item, _cacheHandles[handleIndex].Configuration.Name);
}
_cacheHandles[handleIndex].Add(item);
}
}
}
private void AddToHandlesBelow(CacheItem<TCacheValue> item, int foundIndex)
{
if (item == null)
{
return;
}
if (_logTrace)
{
Logger.LogTrace("Add [{0}] to handles below handle '{1}'.", item, foundIndex);
}
for (var handleIndex = 0; handleIndex < _cacheHandles.Length; handleIndex++)
{
if (handleIndex > foundIndex)
{
if (_cacheHandles[handleIndex].Add(item))
{
_cacheHandles[handleIndex].Stats.OnAdd(item);
}
}
}
}
private void EvictFromOtherHandles(string key, string region, int excludeIndex)
{
if (excludeIndex < 0 || excludeIndex >= _cacheHandles.Length)
{
throw new ArgumentOutOfRangeException(nameof(excludeIndex));
}
if (_logTrace)
{
Logger.LogTrace("Evict [{0}:{1}] from other handles excluding handle '{2}'.", region, key, excludeIndex);
}
for (var handleIndex = 0; handleIndex < _cacheHandles.Length; handleIndex++)
{
if (handleIndex != excludeIndex)
{
EvictFromHandle(key, region, _cacheHandles[handleIndex]);
}
}
}
private void EvictFromHandlesAbove(string key, string region, int excludeIndex)
{
if (_logTrace)
{
Logger.LogTrace("Evict from handles above: {0} {1}: above handle {2}.", key, region, excludeIndex);
}
if (excludeIndex < 0 || excludeIndex >= _cacheHandles.Length)
{
throw new ArgumentOutOfRangeException(nameof(excludeIndex));
}
for (var handleIndex = 0; handleIndex < _cacheHandles.Length; handleIndex++)
{
if (handleIndex < excludeIndex)
{
EvictFromHandle(key, region, _cacheHandles[handleIndex]);
}
}
}
private void RegisterCacheBackplane(CacheBackplane backplane)
{
NotNull(backplane, nameof(backplane));
// this should have been checked during activation already, just to be totally sure...
if (_cacheHandles.Any(p => p.Configuration.IsBackplaneSource))
{
// added includeSource param to get the handles which need to be synced.
// in case the backplane source is non-distributed (in-memory), only remotely triggered remove and clear should also
// trigger a sync locally. For distributed caches, we expect that the distributed cache is already the source and in sync
// as that's the layer which triggered the event. In this case, only other in-memory handles above the distributed, would be synced.
var handles = new Func<bool, BaseCacheHandle<TCacheValue>[]>((includeSource) =>
{
var handleList = new List<BaseCacheHandle<TCacheValue>>();
foreach (var handle in _cacheHandles)
{
if (!handle.Configuration.IsBackplaneSource ||
(includeSource && handle.Configuration.IsBackplaneSource && !handle.IsDistributedCache))
{
handleList.Add(handle);
}
}
return handleList.ToArray();
});
backplane.Changed += (sender, args) =>
{
if (Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug("Backplane event: [Changed] for '{1}:{0}'.", args.Key, args.Region);
}
EvictFromHandles(args.Key, args.Region, handles(false));
switch (args.Action)
{
case CacheItemChangedEventAction.Add:
TriggerOnAdd(args.Key, args.Region, CacheActionEventArgOrigin.Remote);
break;
case CacheItemChangedEventAction.Put:
TriggerOnPut(args.Key, args.Region, CacheActionEventArgOrigin.Remote);
break;
case CacheItemChangedEventAction.Update:
TriggerOnUpdate(args.Key, args.Region, CacheActionEventArgOrigin.Remote);
break;
}
};
backplane.Removed += (sender, args) =>
{
if (_logTrace)
{
Logger.LogTrace("Backplane event: [Remove] of {0} {1}.", args.Key, args.Region);
}
EvictFromHandles(args.Key, args.Region, handles(true));
TriggerOnRemove(args.Key, args.Region, CacheActionEventArgOrigin.Remote);
};
backplane.Cleared += (sender, args) =>
{
if (_logTrace)
{
Logger.LogTrace("Backplane event: [Clear].");
}
ClearHandles(handles(true));
TriggerOnClear(CacheActionEventArgOrigin.Remote);
};
backplane.ClearedRegion += (sender, args) =>
{
if (_logTrace)
{
Logger.LogTrace("Backplane event: [Clear Region] region: {0}.", args.Region);
}
ClearRegionHandles(args.Region, handles(true));
TriggerOnClearRegion(args.Region, CacheActionEventArgOrigin.Remote);
};
}
}
private void TriggerOnAdd(string key, string region, CacheActionEventArgOrigin origin = CacheActionEventArgOrigin.Local)
{
OnAdd?.Invoke(this, new CacheActionEventArgs(key, region, origin));
}
private void TriggerOnClear(CacheActionEventArgOrigin origin = CacheActionEventArgOrigin.Local)
{
OnClear?.Invoke(this, new CacheClearEventArgs(origin));
}
private void TriggerOnClearRegion(string region, CacheActionEventArgOrigin origin = CacheActionEventArgOrigin.Local)
{
OnClearRegion?.Invoke(this, new CacheClearRegionEventArgs(region, origin));
}
private void TriggerOnGet(string key, string region, CacheActionEventArgOrigin origin = CacheActionEventArgOrigin.Local)
{
OnGet?.Invoke(this, new CacheActionEventArgs(key, region, origin));
}
private void TriggerOnPut(string key, string region, CacheActionEventArgOrigin origin = CacheActionEventArgOrigin.Local)
{
OnPut?.Invoke(this, new CacheActionEventArgs(key, region, origin));
}
private void TriggerOnRemove(string key, string region, CacheActionEventArgOrigin origin = CacheActionEventArgOrigin.Local)
{
NotNullOrWhiteSpace(key, nameof(key));
OnRemove?.Invoke(this, new CacheActionEventArgs(key, region, origin));
}
private void TriggerOnRemoveByHandle(string key, string region, CacheItemRemovedReason reason, int level, object value)
{
NotNullOrWhiteSpace(key, nameof(key));
OnRemoveByHandle?.Invoke(this, new CacheItemRemovedEventArgs(key, region, reason, value, level));
}
private void TriggerOnUpdate(string key, string region, CacheActionEventArgOrigin origin = CacheActionEventArgOrigin.Local)
{
OnUpdate?.Invoke(this, new CacheActionEventArgs(key, region, origin));
}
}
}