Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathBaseCacheManager.cs
More file actions
Latest commit
860 lines (719 loc) · 30.1 KB
/
Copy pathBaseCacheManager.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
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Collections.ObjectModel;
usingSystem.Globalization;
usingSystem.Linq;
usingCacheManager.Core.Internal;
usingMicrosoft.Extensions.Logging;
usingstaticCacheManager.Core.Utility.Guard;
namespaceCacheManager.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>
publicpartialclassBaseCacheManager<TCacheValue>:BaseCache<TCacheValue>,ICacheManager<TCacheValue>,IDisposable
{
privateclassNullLoggerFactory:ILoggerFactory
{
publicvoidAddProvider(ILoggerProviderprovider)
{
}
publicILoggerCreateLogger(stringcategoryName)
{
returnnewNullLogger();
}
publicvoidDispose()
{
}
}
privateclassNullLogger:ILogger<BaseCacheManager<TCacheValue>>
{
publicIDisposableBeginScope<TState>(TStatestate)=>null;
publicboolIsEnabled(LogLevellogLevel)=>false;
publicvoidLog<TState>(LogLevellogLevel,EventIdeventId,TStatestate,Exceptionexception,Func<TState,Exception,string>formatter)
{
// do nothing;
}
}
privatereadonlybool_logTrace=false;
privatereadonlyBaseCacheHandle<TCacheValue>[]_cacheHandles;
privatereadonlyCacheBackplane_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}"/>
publicBaseCacheManager(ICacheManagerConfigurationconfiguration,ILoggerFactoryloggerFactory=null)
:this(configuration?.Name??Guid.NewGuid().ToString(),configuration,loggerFactory??newNullLoggerFactory())
{
}
[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")]
privateBaseCacheManager(stringname,ICacheManagerConfigurationconfiguration,ILoggerFactoryloggerFactory)
{
NotNullOrWhiteSpace(name,nameof(name));
NotNull(configuration,nameof(configuration));
NotNull(loggerFactory,nameof(loggerFactory));
Name=name;
Configuration=configuration;
varserializer=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();
varindex=0;
foreach(varhandlein_cacheHandles)
{
varhandleIndex=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(Exceptionex)
{
Logger.LogError(ex,"Error occurred while creating the cache manager.");
throwex.InnerException??ex;
}
}
/// <inheritdoc />
publiceventEventHandler<CacheActionEventArgs>OnAdd;
/// <inheritdoc />
publiceventEventHandler<CacheClearEventArgs>OnClear;
/// <inheritdoc />
publiceventEventHandler<CacheClearRegionEventArgs>OnClearRegion;
/// <inheritdoc />
publiceventEventHandler<CacheActionEventArgs>OnGet;
/// <inheritdoc />
publiceventEventHandler<CacheActionEventArgs>OnPut;
/// <inheritdoc />
publiceventEventHandler<CacheActionEventArgs>OnRemove;
/// <inheritdoc />
publiceventEventHandler<CacheItemRemovedEventArgs>OnRemoveByHandle;
/// <inheritdoc />
publiceventEventHandler<CacheActionEventArgs>OnUpdate;
/// <inheritdoc />
publicIReadOnlyCacheManagerConfigurationConfiguration{get;}
/// <inheritdoc />
publicIEnumerable<BaseCacheHandle<TCacheValue>>CacheHandles
=>newReadOnlyCollection<BaseCacheHandle<TCacheValue>>(
newList<BaseCacheHandle<TCacheValue>>(
_cacheHandles));
/// <summary>
/// Gets the configured cache backplane.
/// </summary>
/// <value>The backplane.</value>
publicCacheBackplaneBackplane=>_cacheBackplane;
/// <summary>
/// Gets the cache name.
/// </summary>
/// <value>The name of the cache.</value>
publicstringName{get;}
/// <inheritdoc />
protectedoverrideILoggerLogger{get;}
/// <inheritdoc />
publicoverridevoidClear()
{
CheckDisposed();
if(_logTrace)
{
Logger.LogTrace("Clear: flushing cache...");
}
foreach(varhandlein_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 />
publicoverridevoidClearRegion(stringregion)
{
NotNullOrWhiteSpace(region,nameof(region));
CheckDisposed();
if(_logTrace)
{
Logger.LogTrace("Clear region: {0}.",region);
}
foreach(varhandlein_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 />
publicoverrideboolExists(stringkey)
{
foreach(varhandlein_cacheHandles)
{
if(_logTrace)
{
Logger.LogTrace("Checking if [{0}] exists on handle '{1}'.",key,handle.Configuration.Name);
}
if(handle.Exists(key))
{
returntrue;
}
}
returnfalse;
}
/// <inheritdoc />
publicoverrideboolExists(stringkey,stringregion)
{
foreach(varhandlein_cacheHandles)
{
if(_logTrace)
{
Logger.LogTrace("Checking if [{0}:{1}] exists on handle '{2}'.",region,key,handle.Configuration.Name);
}
if(handle.Exists(key,region))
{
returntrue;
}
}
returnfalse;
}
/// <summary>
/// Returns a <see cref="string" /> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="string" /> that represents this instance.
/// </returns>
publicoverridestringToString()=>
string.Format(CultureInfo.InvariantCulture,"Name: {0}, Handles: [{1}]",Name,string.Join(",",_cacheHandles.Select(p =>p.GetType().Name)));
/// <inheritdoc />
protectedinternaloverrideboolAddInternal(CacheItem<TCacheValue>item)
{
NotNull(item,nameof(item));
CheckDisposed();
if(_logTrace)
{
Logger.LogTrace("Add [{0}] started.",item);
}
varhandleIndex=_cacheHandles.Length-1;
varresult=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);
}
returnresult;
}
/// <inheritdoc />
protectedinternaloverridevoidPutInternal(CacheItem<TCacheValue>item)
{
NotNull(item,nameof(item));
CheckDisposed();
if(_logTrace)
{
Logger.LogTrace("Put [{0}] started.",item);
}
foreach(varhandlein_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
varoldItem=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 />
protectedoverridevoidDispose(booldisposeManaged)
{
if(disposeManaged)
{
foreach(varhandlein_cacheHandles)
{
handle.Dispose();
}
if(_cacheBackplane!=null)
{
_cacheBackplane.Dispose();
}
}
base.Dispose(disposeManaged);
}
/// <inheritdoc />
protectedoverrideCacheItem<TCacheValue>GetCacheItemInternal(stringkey)=>
GetCacheItemInternal(key,null);
/// <inheritdoc />
protectedoverrideCacheItem<TCacheValue>GetCacheItemInternal(stringkey,stringregion)
{
CheckDisposed();
CacheItem<TCacheValue>cacheItem=null;
if(_logTrace)
{
Logger.LogTrace("Get [{0}:{1}] started.",region,key);
}
for(varhandleIndex=0;handleIndex<_cacheHandles.Length;handleIndex++)
{
varhandle=_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);
}
}
returncacheItem;
}
/// <inheritdoc />
protectedoverrideboolRemoveInternal(stringkey)=>
RemoveInternal(key,null);
/// <inheritdoc />
protectedoverrideboolRemoveInternal(stringkey,stringregion)
{
CheckDisposed();
varresult=false;
if(_logTrace)
{
Logger.LogTrace("Removing [{0}:{1}].",region,key);
}
foreach(varhandlein_cacheHandles)
{
varhandleResult=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.
varinProcOnly=_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);
}
}
returnresult;
}
privatestaticboolAddItemToHandle(CacheItem<TCacheValue>item,BaseCacheHandle<TCacheValue>handle)
{
if(handle.Add(item))
{
handle.Stats.OnAdd(item);
returntrue;
}
returnfalse;
}
privatestaticvoidClearHandles(BaseCacheHandle<TCacheValue>[]handles)
{
foreach(varhandleinhandles)
{
handle.Clear();
handle.Stats.OnClear();
}
////this.TriggerOnClear();
}
privatestaticvoidClearRegionHandles(stringregion,BaseCacheHandle<TCacheValue>[]handles)
{
foreach(varhandleinhandles)
{
handle.ClearRegion(region);
handle.Stats.OnClearRegion(region);
}
////this.TriggerOnClearRegion(region);
}
privatevoidEvictFromHandles(stringkey,stringregion,BaseCacheHandle<TCacheValue>[]handles)
{
foreach(varhandleinhandles)
{
EvictFromHandle(key,region,handle);
}
}
privatevoidEvictFromHandle(stringkey,stringregion,BaseCacheHandle<TCacheValue>handle)
{
if(Logger.IsEnabled(LogLevel.Debug))
{
Logger.LogDebug(
"Evicting '{0}:{1}' from handle '{2}'.",
region,
key,
handle.Configuration.Name);
}
boolresult;
if(string.IsNullOrWhiteSpace(region))
{
result=handle.Remove(key);
}
else
{
result=handle.Remove(key,region);
}
if(result)
{
handle.Stats.OnRemove(region);
}
}
privatevoidAddToHandles(CacheItem<TCacheValue>item,intfoundIndex)
{
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(varhandleIndex=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);
}
}
}
privatevoidAddToHandlesBelow(CacheItem<TCacheValue>item,intfoundIndex)
{
if(item==null)
{
return;
}
if(_logTrace)
{
Logger.LogTrace("Add [{0}] to handles below handle '{1}'.",item,foundIndex);
}
for(varhandleIndex=0;handleIndex<_cacheHandles.Length;handleIndex++)
{
if(handleIndex>foundIndex)
{
if(_cacheHandles[handleIndex].Add(item))
{
_cacheHandles[handleIndex].Stats.OnAdd(item);
}
}
}
}
privatevoidEvictFromOtherHandles(stringkey,stringregion,intexcludeIndex)
{
if(excludeIndex<0||excludeIndex>=_cacheHandles.Length)
{
thrownewArgumentOutOfRangeException(nameof(excludeIndex));
}
if(_logTrace)
{
Logger.LogTrace("Evict [{0}:{1}] from other handles excluding handle '{2}'.",region,key,excludeIndex);
}
for(varhandleIndex=0;handleIndex<_cacheHandles.Length;handleIndex++)
{
if(handleIndex!=excludeIndex)
{
EvictFromHandle(key,region,_cacheHandles[handleIndex]);
}
}
}
privatevoidEvictFromHandlesAbove(stringkey,stringregion,intexcludeIndex)
{
if(_logTrace)
{
Logger.LogTrace("Evict from handles above: {0} {1}: above handle {2}.",key,region,excludeIndex);
}
if(excludeIndex<0||excludeIndex>=_cacheHandles.Length)
{
thrownewArgumentOutOfRangeException(nameof(excludeIndex));
}
for(varhandleIndex=0;handleIndex<_cacheHandles.Length;handleIndex++)
{
if(handleIndex<excludeIndex)
{
EvictFromHandle(key,region,_cacheHandles[handleIndex]);
}
}
}
privatevoidRegisterCacheBackplane(CacheBackplanebackplane)
{
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.
varhandles=newFunc<bool,BaseCacheHandle<TCacheValue>[]>((includeSource)=>
{
varhandleList=newList<BaseCacheHandle<TCacheValue>>();
foreach(varhandlein_cacheHandles)
{
if(!handle.Configuration.IsBackplaneSource||
(includeSource&&handle.Configuration.IsBackplaneSource&&!handle.IsDistributedCache))
{
handleList.Add(handle);
}
}
returnhandleList.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)
{
caseCacheItemChangedEventAction.Add:
TriggerOnAdd(args.Key,args.Region,CacheActionEventArgOrigin.Remote);
break;
caseCacheItemChangedEventAction.Put:
TriggerOnPut(args.Key,args.Region,CacheActionEventArgOrigin.Remote);
break;
caseCacheItemChangedEventAction.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);
};
}
}
privatevoidTriggerOnAdd(stringkey,stringregion,CacheActionEventArgOriginorigin=CacheActionEventArgOrigin.Local)
{
OnAdd?.Invoke(this,newCacheActionEventArgs(key,region,origin));
}
privatevoidTriggerOnClear(CacheActionEventArgOriginorigin=CacheActionEventArgOrigin.Local)
{
OnClear?.Invoke(this,newCacheClearEventArgs(origin));
}
privatevoidTriggerOnClearRegion(stringregion,CacheActionEventArgOriginorigin=CacheActionEventArgOrigin.Local)
{
OnClearRegion?.Invoke(this,newCacheClearRegionEventArgs(region,origin));
}
privatevoidTriggerOnGet(stringkey,stringregion,CacheActionEventArgOriginorigin=CacheActionEventArgOrigin.Local)
{
OnGet?.Invoke(this,newCacheActionEventArgs(key,region,origin));
}
privatevoidTriggerOnPut(stringkey,stringregion,CacheActionEventArgOriginorigin=CacheActionEventArgOrigin.Local)
{
OnPut?.Invoke(this,newCacheActionEventArgs(key,region,origin));
}
privatevoidTriggerOnRemove(stringkey,stringregion,CacheActionEventArgOriginorigin=CacheActionEventArgOrigin.Local)
{
NotNullOrWhiteSpace(key,nameof(key));
OnRemove?.Invoke(this,newCacheActionEventArgs(key,region,origin));
}
privatevoidTriggerOnRemoveByHandle(stringkey,stringregion,CacheItemRemovedReasonreason,intlevel,objectvalue)
{
NotNullOrWhiteSpace(key,nameof(key));
OnRemoveByHandle?.Invoke(this,newCacheItemRemovedEventArgs(key,region,reason,value,level));
}
privatevoidTriggerOnUpdate(stringkey,stringregion,CacheActionEventArgOriginorigin=CacheActionEventArgOrigin.Local)
{
OnUpdate?.Invoke(this,newCacheActionEventArgs(key,region,origin));
}
}
}