-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCache.cs
More file actions
1310 lines (1179 loc) · 69.4 KB
/
Cache.cs
File metadata and controls
1310 lines (1179 loc) · 69.4 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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Related components
using System;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Generic;
using System.Collections.Concurrent;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using CacheUtils;
#endregion
#if !SIGN
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("VIEApps.Components.XUnitTests")]
#endif
namespace net.vieapps.Components.Caching
{
/// <summary>
/// Manipulates objects in isolated regions with distributed cache servers (support Redis & Memcached)
/// </summary>
[DebuggerDisplay("{Name} ({ExpirationTime} minutes)")]
public sealed class Cache : IDistributedCache, ICache
{
internal static ConcurrentDictionary<string, long> Sizes { get; } = new ConcurrentDictionary<string, long>(StringComparer.OrdinalIgnoreCase);
internal readonly ICache _distributedCache;
internal readonly MemoryCache _L1Cache;
/// <summary>
/// Create a new instance of distributed cache with isolated region
/// </summary>
/// <param name="name">The string that presents name of isolated region</param>
/// <param name="loggerFactory">The logger factory for working with logs</param>
/// <param name="useL1Cache">true to use L-1 Cache (in-process memory)</param>
public Cache(string name = null, ILoggerFactory loggerFactory = null, bool useL1Cache = false)
: this(name, Cache.Configuration, loggerFactory, useL1Cache) { }
/// <summary>
/// Create a new instance of distributed cache with isolated region
/// </summary>
/// <param name="name">The string that presents name of isolated region</param>
/// <param name="configuration">The cache configuration</param>
/// <param name="loggerFactory">The logger factory for working with logs</param>
/// <param name="useL1Cache">true to use L-1 Cache (in-process memory)</param>
public Cache(string name, ICacheConfiguration configuration, ILoggerFactory loggerFactory, bool useL1Cache = false)
: this(name ?? configuration?.RegionName, configuration != null ? configuration.ExpirationTime : 25, configuration?.Provider, useL1Cache || (configuration != null && configuration.UseL1Cache), configuration != null ? configuration.MaxL1CacheSize : (byte)0, configuration?.ModeL1CacheExpires, configuration != null && configuration.UseL1Cache && configuration.PrefetchL1Cache, configuration != null ? configuration.PrefetchL1CacheDelay : 0, loggerFactory) { }
/// <summary>
/// Create a new instance of distributed cache with isolated region
/// </summary>
/// <param name="name">The string that presents name of isolated region</param>
/// <param name="expirationTime">Time for caching an item (in minutes)</param>
/// <param name="provider">The string that presents the caching provider ('Redis' or 'Memcached') - the default provider is 'Redis'</param>
/// <param name="useL1Cache">true to use L-1 Cache (in-process memory)</param>
/// <param name="maxL1CacheSize">Max memory size of L-1 Cache (in giga-bytes)</param>
/// <param name="storeKeys">true to active store all keys of the region (to clear or use with other purposes further)</param>
public Cache(string name, int expirationTime, string provider, bool useL1Cache, byte maxL1CacheSize, string modeL1CacheExpires, bool prefetchL1Cache, int prefetchL1CacheDelay, ILoggerFactory loggerFactory = null, bool storeKeys = false)
{
this._distributedCache = (string.IsNullOrWhiteSpace(provider) ? "Redis" : provider).Trim().ToLower().Equals("memcached")
? new Memcached(name, expirationTime, storeKeys)
: new Redis(name, expirationTime, storeKeys) as ICache;
this.UseL1Cache = useL1Cache;
this.ModeL1CacheExpires = modeL1CacheExpires;
this.PrefetchL1Cache = prefetchL1Cache;
this.PrefetchL1CacheDelay = prefetchL1CacheDelay > 0 ? prefetchL1CacheDelay : 1234;
if (useL1Cache)
this._L1Cache = new MemoryCache(key => this.SendL1CacheRequest?.Invoke(key, "update"), key => this.SendL1CacheRequest?.Invoke(key, "remove"), key => Helper.GetCacheKey(this.Name, key), maxL1CacheSize, loggerFactory);
(loggerFactory ?? Enyim.Caching.Logger.GetLoggerFactory()).CreateLogger<Cache>().LogInformation($"A new instance of caching was created [{this.Provider}: {this.Name} ({this.ExpirationTime} minutes) - L1-Cache: {this.UseL1Cache && this._L1Cache != null}/{this.PrefetchL1Cache}]");
}
public void Dispose()
{
this._distributedCache.Dispose();
this._L1Cache?.Dispose();
}
#region Singleton
internal static Cache _Instance { get; set; }
static ICacheConfiguration _Configuration { get; set; }
/// <summary>
/// Gets the global settings of the caching component
/// </summary>
public static ICacheConfiguration Configuration => Cache._Configuration ?? (Cache._Configuration = new CacheConfiguration(ConfigurationManager.GetSection("net.vieapps.cache") is CacheConfigurationSectionHandler config ? config : ConfigurationManager.GetSection("cache") as CacheConfigurationSectionHandler));
/// <summary>
/// Creates new an instance of caching component
/// </summary>
/// <param name="name">The string that presents name of isolated region</param>
/// <param name="configuration">The caching configuration</param>
/// <param name="loggerFactory">The logger factory</param>
/// <param name="useL1Cache">true to use L-1 Cache (in-process memory)</param>
/// <returns></returns>
public static Cache CreateInstance(string name, ICacheConfiguration configuration, ILoggerFactory loggerFactory = null, bool useL1Cache = false)
{
loggerFactory = loggerFactory ?? Enyim.Caching.Logger.GetLoggerFactory();
if (configuration != null && configuration.Servers != null)
{
if (configuration.Servers.Where(server => server.Type.ToLower().Equals("redis")).Any())
Redis.GetClient(configuration.GetRedisConfiguration(), loggerFactory);
if (configuration.Servers.Where(server => server.Type.ToLower().Equals("memcached")).Any())
Memcached.GetClient(configuration.GetMemcachedConfiguration(loggerFactory), loggerFactory);
}
return new Cache(name, configuration, loggerFactory, useL1Cache);
}
/// <summary>
/// Creates new an instance of caching component
/// </summary>
/// <param name="name">The string that presents name of isolated region</param>
/// <param name="loggerFactory">The logger factory</param>
/// <param name="useL1Cache">true to use L-1 Cache (in-process memory)</param>
/// <returns></returns>
public static Cache CreateInstance(string name, ILoggerFactory loggerFactory = null, bool useL1Cache = false)
=> Cache.CreateInstance(name, Cache.Configuration, loggerFactory, useL1Cache);
/// <summary>
/// Gets the singleton instance of caching component
/// </summary>
/// <param name="configuration">The caching configuration</param>
/// <param name="loggerFactory">The logger factory</param>
/// <param name="useL1Cache">true to use L-1 Cache (in-process memory)</param>
/// <returns></returns>
public static Cache GetInstance(ICacheConfiguration configuration, ILoggerFactory loggerFactory = null, bool useL1Cache = false)
{
if (Cache._Instance == null)
{
Cache._Configuration = configuration;
Cache._Instance = Cache.CreateInstance(null, loggerFactory, useL1Cache);
}
return Cache._Instance;
}
/// <summary>
/// Gets the singleton instance of caching component
/// </summary>
/// <param name="configurationSection"></param>
/// <param name="loggerFactory"></param>
/// <returns></returns>
public static Cache GetInstance(CacheConfigurationSectionHandler configurationSection, ILoggerFactory loggerFactory = null)
=> Cache.GetInstance(new CacheConfiguration(configurationSection), loggerFactory);
/// <summary>
/// Gets the singleton instance of caching componentGets the singleton instance of caching component
/// </summary>
/// <param name="svcProvider"></param>
/// <returns></returns>
public static Cache GetInstance(IServiceProvider svcProvider)
=> Cache.GetInstance(svcProvider.GetService<ICacheConfiguration>(), svcProvider.GetService<ILoggerFactory>());
#endregion
#region Properties
/// <summary>
/// Gets the name of the isolated region
/// </summary>
public string Name => this._distributedCache.Name;
/// <summary>
/// Gets the name of the cache provider
/// </summary>
public string Provider => this._distributedCache.GetType().ToString().Split('.').Last();
/// <summary>
/// Gets the expiration time (in minutes)
/// </summary>
public int ExpirationTime => this._distributedCache.ExpirationTime;
/// <summary>
/// Gets the collection of keys
/// </summary>
public HashSet<string> Keys => this._distributedCache.Keys;
#endregion
#region L1-Cache
/// <summary>
/// Gets or Sets state to use MemoryCache as L1-Cache
/// </summary>
public bool UseL1Cache { get; set; } = false;
/// <summary>
/// Gets or Sets expires mode of L1-Cache items
/// </summary>
public string ModeL1CacheExpires { get; set; } = "auto";
/// <summary>
/// Gets or Sets state to pre-fetch L1-Cache item
/// </summary>
public bool PrefetchL1Cache { get; set; } = false;
/// <summary>
/// Gets or Sets delaying times (miliseconds) before pre-fetching L1-Cache item
/// </summary>
public int PrefetchL1CacheDelay { get; set; } = 0;
/// <summary>
/// Sends the request for invalidating a L1-Cache itemm
/// </summary>
public Action<string, string> SendL1CacheRequest { get; set; }
/// <summary>
/// Does the action to process the request for invalidating a L1-Cache item
/// </summary>
public async Task ProcessL1CacheRequestAsync(string key, string reason = null)
{
if (!this.UseL1Cache || this._L1Cache == null || string.IsNullOrWhiteSpace(key))
return;
if ("clear".Equals(key.ToLower()))
this._L1Cache.Clear();
else
{
this._L1Cache.Remove(key, false);
if (this.PrefetchL1Cache && "remove" != reason)
{
if (this.PrefetchL1CacheDelay > 0)
await Task.Delay(this.PrefetchL1CacheDelay).ConfigureAwait(false);
if (!this._L1Cache.Exists(key))
this._L1Cache.Set(key, await this._distributedCache.GetAsync(key).ConfigureAwait(false), this.GetExpiresAt(), false);
}
}
}
bool SendL1CacheRequests(IEnumerable<string> keys, string keyPrefix = null, string reason = "update")
{
keys?.Where(key => !string.IsNullOrWhiteSpace(key)).Select(key => (string.IsNullOrWhiteSpace(keyPrefix) ? "" : keyPrefix) + key).ToList().ForEach(key => this.SendL1CacheRequest?.Invoke(key, reason));
return keys != null && keys.Any();
}
bool SendL1CacheRequests(string key, string reason = "update")
=> this.SendL1CacheRequests(new[] { key }, null, reason);
DateTime GetExpiresAt(DateTime? expiresAt)
{
var minutes = expiresAt == null ? this.ExpirationTime : (expiresAt.Value - DateTime.Now).TotalMinutes;
return DateTime.Now.AddMinutes("original" == this.ModeL1CacheExpires?.ToLower() ? minutes - 2 : minutes > 2 && minutes < 13 ? minutes / 2 : 3);
}
DateTime GetExpiresAt(TimeSpan? validFor)
=> this.GetExpiresAt(validFor == null || validFor.Value.Equals(TimeSpan.Zero) ? DateTime.Now.AddMinutes(this.ExpirationTime) : DateTime.Now.AddSeconds(validFor.Value.TotalSeconds));
DateTime GetExpiresAt(int expirationTime = 0)
=> this.GetExpiresAt(DateTime.Now.AddMinutes(expirationTime > 0 ? expirationTime : this.ExpirationTime));
/// <summary>
/// Sets a cache item of L1-Cache
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="validFor"></param>
/// <param name="fireCallbackHandler"></param>
/// <returns></returns>
public bool SetL1CacheItem<T>(string key, T value, TimeSpan? validFor = null, bool fireCallbackHandler = false)
=> this._L1Cache != null && this._L1Cache.Set(key, value, this.GetExpiresAt(validFor), fireCallbackHandler);
/// <summary>
/// Sets a cache item of L1-Cache
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiresAt"></param>
/// <param name="fireCallbackHandler"></param>
/// <returns></returns>
public bool SetL1CacheItem<T>(string key, T value, DateTime expiresAt, bool fireCallbackHandler = false)
=> this._L1Cache != null && this._L1Cache.Set(key, value, this.GetExpiresAt(expiresAt), fireCallbackHandler);
/// <summary>
/// Gets a cache item of L1-Cache
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object GetL1CacheItem(string key)
=> this._L1Cache?.Get(key);
/// <summary>
/// Gets a cache item of L1-Cache
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public T GetL1CacheItem<T>(string key)
=> this._L1Cache == null ? default : this._L1Cache.Get<T>(key);
/// <summary>
/// Removes a cache item of L1-Cache
/// </summary>
/// <param name="key"></param>
/// <param name="fireCallbackHandler"></param>
/// <returns></returns>
public bool RemoveL1CacheItem(string key, bool fireCallbackHandler = false)
=> this._L1Cache != null && this._L1Cache.Remove(key, fireCallbackHandler);
/// <summary>
/// Clears L1-Cache
/// </summary>
public void ClearL1Cache()
{
this._L1Cache?.Clear();
this.SendL1CacheRequests("clear", "remove");
}
/// <summary>
/// Gets the collection of L1-Cache keys
/// </summary>
public HashSet<string> GetL1CacheKeys()
=> new HashSet<string>(this._L1Cache?.Keys ?? Array.Empty<string>());
#endregion
#region Keys
/// <summary>
/// Gets the collection of keys
/// </summary>
public HashSet<string> GetKeys()
=> this._distributedCache.GetKeys();
/// <summary>
/// Gets the collection of keys
/// </summary>
public Task<HashSet<string>> GetKeysAsync(CancellationToken cancellationToken = default)
=> this._distributedCache.GetKeysAsync(cancellationToken);
#endregion
#region Set
/// <summary>
/// Adds an item into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool Set(string key, object value, int expirationTime = 0)
=> this._distributedCache.Set(key, value, expirationTime) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expirationTime)) : this.SendL1CacheRequests(key));
/// <summary>
/// Adds an item into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool Set(string key, object value, TimeSpan validFor)
=> this._distributedCache.Set(key, value, validFor) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(validFor)) : this.SendL1CacheRequests(key));
/// <summary>
/// Adds an item into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool Set(string key, object value, DateTime expiresAt)
=> this._distributedCache.Set(key, value, expiresAt) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expiresAt)) : this.SendL1CacheRequests(key));
/// <summary>
/// Adds an item into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> SetAsync(string key, object value, int expirationTime = 0, CancellationToken cancellationToken = default)
=> this._distributedCache.SetAsync(key, value, expirationTime, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expirationTime)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds an item into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> SetAsync(string key, object value, CancellationToken cancellationToken)
=> this.SetAsync(key, value, 0, cancellationToken);
/// <summary>
/// Adds an item into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> SetAsync(string key, object value, TimeSpan validFor, CancellationToken cancellationToken = default)
=> this._distributedCache.SetAsync(key, value, validFor, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(validFor)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds an item into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> SetAsync(string key, object value, DateTime expiresAt, CancellationToken cancellationToken = default)
=> this._distributedCache.SetAsync(key, value, expiresAt, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expiresAt)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
#endregion
#region Set (Multiple)
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
public void Set(IDictionary<string, object> items, string keyPrefix, DateTime? expiresAt)
{
this._distributedCache.Set(items, keyPrefix, expiresAt != null ? (int)expiresAt.Value.ToTimeSpan().TotalMinutes : this.ExpirationTime);
if (this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(items, keyPrefix, expiresAt != null ? this.GetExpiresAt(expiresAt.Value) : this.GetExpiresAt(this.ExpirationTime));
else
this.SendL1CacheRequests(items?.Select(kvp => kvp.Key), keyPrefix);
}
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
public void Set(IDictionary<string, object> items, string keyPrefix, TimeSpan validFor)
{
this._distributedCache.Set(items, keyPrefix, validFor != TimeSpan.Zero ? (int)validFor.TotalMinutes : this.ExpirationTime);
if (this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(items, keyPrefix, this.GetExpiresAt(validFor != TimeSpan.Zero ? (int)validFor.TotalMinutes : this.ExpirationTime));
else
this.SendL1CacheRequests(items?.Select(kvp => kvp.Key), keyPrefix);
}
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
public void Set(IDictionary<string, object> items, string keyPrefix, int expirationTime)
=> this.Set(items, keyPrefix, DateTime.Now.AddMinutes(expirationTime > 0 ? expirationTime : this.ExpirationTime));
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <typeparam name="T">The type for casting the cached item</typeparam>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
public void Set<T>(IDictionary<string, T> items, string keyPrefix, DateTime? expiresAt)
{
this._distributedCache.Set(items, keyPrefix, expiresAt != null ? (int)expiresAt.Value.ToTimeSpan().TotalMinutes : this.ExpirationTime);
if (this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(items, keyPrefix, expiresAt != null ? this.GetExpiresAt(expiresAt.Value) : this.GetExpiresAt(this.ExpirationTime));
else
this.SendL1CacheRequests(items?.Select(kvp => kvp.Key), keyPrefix);
}
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <typeparam name="T">The type for casting the cached item</typeparam>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
public void Set<T>(IDictionary<string, T> items, string keyPrefix, TimeSpan validFor)
{
this._distributedCache.Set(items, keyPrefix, validFor != TimeSpan.Zero ? (int)validFor.TotalMinutes : this.ExpirationTime);
if (this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(items, keyPrefix, this.GetExpiresAt(validFor != TimeSpan.Zero ? (int)validFor.TotalMinutes : this.ExpirationTime));
else
this.SendL1CacheRequests(items?.Select(kvp => kvp.Key), keyPrefix);
}
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <typeparam name="T">The type for casting the cached item</typeparam>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
public void Set<T>(IDictionary<string, T> items, string keyPrefix, int expirationTime)
=> this.Set(items, keyPrefix, DateTime.Now.AddMinutes(expirationTime > 0 ? expirationTime : this.ExpirationTime));
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
/// <param name="cancellationToken">The cancellation token</param>
public Task SetAsync(IDictionary<string, object> items, string keyPrefix, DateTime? expiresAt, CancellationToken cancellationToken = default)
=> this._distributedCache.SetAsync(items, keyPrefix, expiresAt != null ? (int)expiresAt.Value.ToTimeSpan().TotalMinutes : this.ExpirationTime, cancellationToken)
.ContinueWith(_ =>
{
if (this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(items, keyPrefix, expiresAt != null ? this.GetExpiresAt(expiresAt.Value) : this.GetExpiresAt(this.ExpirationTime));
else
this.SendL1CacheRequests(items?.Select(kvp => kvp.Key), keyPrefix);
}, TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
/// <param name="cancellationToken">The cancellation token</param>
public Task SetAsync(IDictionary<string, object> items, string keyPrefix, TimeSpan validFor, CancellationToken cancellationToken = default)
=> this._distributedCache.SetAsync(items, keyPrefix, validFor != TimeSpan.Zero ? (int)validFor.TotalMinutes : this.ExpirationTime, cancellationToken)
.ContinueWith(_ =>
{
if (this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(items, keyPrefix, this.GetExpiresAt(validFor != TimeSpan.Zero ? (int)validFor.TotalMinutes : this.ExpirationTime));
else
this.SendL1CacheRequests(items?.Select(kvp => kvp.Key), keyPrefix);
}, TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <param name="cancellationToken">The cancellation token</param>
public Task SetAsync(IDictionary<string, object> items, string keyPrefix, int expirationTime, CancellationToken cancellationToken = default)
=> this.SetAsync(items, keyPrefix, DateTime.Now.AddMinutes(expirationTime > 0 ? expirationTime : this.ExpirationTime), cancellationToken);
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <typeparam name="T">The type for casting the cached item</typeparam>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
/// <param name="cancellationToken">The cancellation token</param>
public Task SetAsync<T>(IDictionary<string, T> items, string keyPrefix, DateTime? expiresAt, CancellationToken cancellationToken = default)
=> this._distributedCache.SetAsync(items, keyPrefix, expiresAt != null ? (int)expiresAt.Value.ToTimeSpan().TotalMinutes : this.ExpirationTime, cancellationToken)
.ContinueWith(_ =>
{
if (this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(items, keyPrefix, expiresAt != null ? this.GetExpiresAt(expiresAt.Value) : this.GetExpiresAt(this.ExpirationTime));
else
this.SendL1CacheRequests(items?.Select(kvp => kvp.Key), keyPrefix);
}, TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <typeparam name="T">The type for casting the cached item</typeparam>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
/// <param name="cancellationToken">The cancellation token</param>
public Task SetAsync<T>(IDictionary<string, T> items, string keyPrefix, TimeSpan validFor, CancellationToken cancellationToken = default)
=> this._distributedCache.SetAsync(items, keyPrefix, validFor != TimeSpan.Zero ? (int)validFor.TotalMinutes : this.ExpirationTime, cancellationToken)
.ContinueWith(_ =>
{
if (this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(items, keyPrefix, this.GetExpiresAt(validFor != TimeSpan.Zero ? (int)validFor.TotalMinutes : this.ExpirationTime));
else
this.SendL1CacheRequests(items?.Select(kvp => kvp.Key), keyPrefix);
}, TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds a collection of items into cache
/// </summary>
/// <typeparam name="T">The type for casting the cached item</typeparam>
/// <param name="items">The collection of items to add</param>
/// <param name="keyPrefix">The string that presents prefix of all keys</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <param name="cancellationToken">The cancellation token</param>
public Task SetAsync<T>(IDictionary<string, T> items, string keyPrefix, int expirationTime, CancellationToken cancellationToken = default)
=> this.SetAsync(items, keyPrefix, DateTime.Now.AddMinutes(expirationTime > 0 ? expirationTime : this.ExpirationTime), cancellationToken);
#endregion
#region Set (Fragment)
/// <summary>
/// Adds an item (as fragments) into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="fragments">The collection that contains all fragments (object that serialized as binary - array bytes)</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool SetFragments(string key, List<byte[]> fragments, int expirationTime = 0)
=> this._distributedCache.SetFragments(key, fragments, expirationTime);
/// <summary>
/// Adds an item (as fragments) into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="fragments">The collection that contains all fragments (object that serialized as binary - array bytes)</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> SetFragmentsAsync(string key, List<byte[]> fragments, int expirationTime = 0, CancellationToken cancellationToken = default)
=> this._distributedCache.SetFragmentsAsync(key, fragments, expirationTime, cancellationToken);
/// <summary>
/// Adds an item (as fragments) into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="fragments">The collection that contains all fragments (object that serialized as binary - array bytes)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> SetFragmentsAsync(string key, List<byte[]> fragments, CancellationToken cancellationToken)
=> this.SetFragmentsAsync(key, fragments, 0, cancellationToken);
/// <summary>
/// Serializes object into array of bytes, splits into one or more fragments and updates into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool SetAsFragments(string key, object value, int expirationTime = 0)
=> this._distributedCache.SetAsFragments(key, value, expirationTime) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expirationTime)) : this.SendL1CacheRequests(key));
/// <summary>
/// Serializes object into array of bytes, splits into one or more fragments and updates into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> SetAsFragmentsAsync(string key, object value, int expirationTime = 0, CancellationToken cancellationToken = default)
=> this._distributedCache.SetAsFragmentsAsync(key, value, expirationTime, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expirationTime)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Serializes object into array of bytes, splits into one or more fragments and updates into cache with a specified key (if the key is already existed, then old cached item will be overriden)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> SetAsFragmentsAsync(string key, object value, CancellationToken cancellationToken)
=> this.SetAsFragmentsAsync(key, value, 0, cancellationToken);
#endregion
#region Add
/// <summary>
/// Adds an item into cache with a specified key when the the key is not existed
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool Add(string key, object value, int expirationTime = 0)
=> this._distributedCache.Add(key, value, expirationTime) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expirationTime)) : this.SendL1CacheRequests(key));
/// <summary>
/// Adds an item into cache with a specified key when the the key is not existed
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool Add(string key, object value, TimeSpan validFor)
=> this._distributedCache.Add(key, value, validFor) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(validFor)) : this.SendL1CacheRequests(key));
/// <summary>
/// Adds an item into cache with a specified key when the the key is not existed
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool Add(string key, object value, DateTime expiresAt)
=> this._distributedCache.Add(key, value, expiresAt) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expiresAt)) : this.SendL1CacheRequests(key));
/// <summary>
/// Adds an item into cache with a specified key when the the key is not existed
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> AddAsync(string key, object value, int expirationTime = 0, CancellationToken cancellationToken = default)
=> this._distributedCache.AddAsync(key, value, expirationTime, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expirationTime)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds an item into cache with a specified key when the the key is not existed
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> AddAsync(string key, object value, CancellationToken cancellationToken)
=> this.AddAsync(key, value, 0, cancellationToken);
/// <summary>
/// Adds an item into cache with a specified key when the the key is not existed
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> AddAsync(string key, object value, TimeSpan validFor, CancellationToken cancellationToken = default)
=> this._distributedCache.AddAsync(key, value, validFor, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(validFor)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds an item into cache with a specified key when the the key is not existed
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> AddAsync(string key, object value, DateTime expiresAt, CancellationToken cancellationToken = default)
=> this._distributedCache.AddAsync(key, value, expiresAt, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expiresAt)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
#endregion
#region Replace
/// <summary>
/// Adds an item into cache with a specified key when the the key is existed (means update existed item)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool Replace(string key, object value, int expirationTime = 0)
=> this._distributedCache.Replace(key, value, expirationTime) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expirationTime)) : this.SendL1CacheRequests(key));
/// <summary>
/// Adds an item into cache with a specified key when the the key is existed (means update existed item)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool Replace(string key, object value, TimeSpan validFor)
=> this._distributedCache.Replace(key, value, validFor) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(validFor)) : this.SendL1CacheRequests(key));
/// <summary>
/// Adds an item into cache with a specified key when the the key is existed (means update existed item)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public bool Replace(string key, object value, DateTime expiresAt)
=> this._distributedCache.Replace(key, value, expiresAt) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expiresAt)) : this.SendL1CacheRequests(key));
/// <summary>
/// Adds an item into cache with a specified key when the the key is existed (means update existed item)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expirationTime">The time (in minutes) that the object will expired (from added time)</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> ReplaceAsync(string key, object value, int expirationTime = 0, CancellationToken cancellationToken = default)
=> this._distributedCache.ReplaceAsync(key, value, expirationTime, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expirationTime)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds an item into cache with a specified key when the the key is existed (means update existed item)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> ReplaceAsync(string key, object value, CancellationToken cancellationToken)
=> this.ReplaceAsync(key, value, 0, cancellationToken);
/// <summary>
/// Adds an item into cache with a specified key when the the key is existed (means update existed item)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="validFor">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> ReplaceAsync(string key, object value, TimeSpan validFor, CancellationToken cancellationToken = default)
=> this._distributedCache.ReplaceAsync(key, value, validFor, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(validFor)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
/// <summary>
/// Adds an item into cache with a specified key when the the key is existed (means update existed item)
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <param name="value">The object that is to be cached</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache</param>
/// <returns>Returns a boolean value indicating if the item is added into cache successful or not</returns>
public Task<bool> ReplaceAsync(string key, object value, DateTime expiresAt, CancellationToken cancellationToken = default)
=> this._distributedCache.ReplaceAsync(key, value, expiresAt, cancellationToken)
.ContinueWith(task => task.Result && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Set(key, value, this.GetExpiresAt(expiresAt)) : this.SendL1CacheRequests(key)), TaskContinuationOptions.OnlyOnRanToCompletion);
#endregion
#region Refresh
/// <summary>
/// Refreshs an existed item
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <returns>Returns a boolean value indicating if the item is refreshed or not</returns>
public bool Refresh(string key)
=> this._distributedCache.Refresh(key) && (!this.UseL1Cache || this.RemoveL1CacheItem(key, true));
/// <summary>
/// Refreshs an existed item
/// </summary>
/// <param name="key">The string that presents key of item</param>
/// <returns>Returns a boolean value indicating if the item is refreshed or not</returns>
public Task<bool> RefreshAsync(string key, CancellationToken cancellationToken = default)
=> this._distributedCache.RefreshAsync(key, cancellationToken)
.ContinueWith(task => task.Result && (!this.UseL1Cache || this.RemoveL1CacheItem(key, true)), TaskContinuationOptions.OnlyOnRanToCompletion);
#endregion
#region Get
/// <summary>
/// Retreives a cached item
/// </summary>
/// <param name="key">The string that presents key of cached item need to retreive</param>
/// <returns>The retrieved cache item, or a null reference if the key is not found</returns>
public object Get(string key)
{
var value = this.UseL1Cache && this._L1Cache != null
? this._L1Cache.Get(key)
: this._distributedCache.Get(key);
if (value == null && this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(key, value = this._distributedCache.Get(key), this.GetExpiresAt(), false);
return value;
}
/// <summary>
/// Retreives a cached item
/// </summary>
/// <typeparam name="T">The type for casting the cached item</typeparam>
/// <param name="key">The string that presents key of cached item need to retreive</param>
/// <returns>The retrieved cache item, or a null reference if the key is not found</returns>
public T Get<T>(string key)
{
var value = this.UseL1Cache && this._L1Cache != null
? this._L1Cache.Get<T>(key)
: this._distributedCache.Get<T>(key);
if (value == null && this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(key, value = this._distributedCache.Get<T>(key), this.GetExpiresAt(), false);
return value;
}
/// <summary>
/// Retreives a cached item
/// </summary>
/// <param name="key">The string that presents key of cached item need to retreive</param>
/// <returns>The retrieved cache item, or a null reference if the key is not found</returns>
public async Task<object> GetAsync(string key, CancellationToken cancellationToken = default)
{
var value = this.UseL1Cache && this._L1Cache != null
? this._L1Cache.Get(key)
: await this._distributedCache.GetAsync(key, cancellationToken).ConfigureAwait(false);
if (value == null && this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(key, value = await this._distributedCache.GetAsync(key, cancellationToken).ConfigureAwait(false), this.GetExpiresAt(), false);
return value;
}
/// <summary>
/// Retreives a cached item
/// </summary>
/// <typeparam name="T">The type for casting the cached item</typeparam>
/// <param name="key">The string that presents key of cached item need to retreive</param>
/// <returns>The retrieved cache item, or a null reference if the key is not found</returns>
public async Task<T> GetAsync<T>(string key, CancellationToken cancellationToken = default)
{
var value = this.UseL1Cache && this._L1Cache != null
? this._L1Cache.Get<T>(key)
: await this._distributedCache.GetAsync<T>(key, cancellationToken).ConfigureAwait(false);
if (value == null && this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(key, value = await this._distributedCache.GetAsync<T>(key, cancellationToken).ConfigureAwait(false), this.GetExpiresAt(), false);
return value;
}
#endregion
#region Get (Multiple)
/// <summary>
/// Retreives a collection of cached items
/// </summary>
/// <param name="keys">The collection of items' keys</param>
/// <returns>The collection of cache items</returns>
public IDictionary<string, object> Get(IEnumerable<string> keys)
{
var value = this.UseL1Cache && this._L1Cache != null
? this._L1Cache.Get(keys)
: keys == null ? null : this._distributedCache.Get(keys);
if ((value == null || value.Count != keys.Count()) && this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(value = keys == null ? null : this._distributedCache.Get(keys), null, this.GetExpiresAt(), false);
return value;
}
/// <summary>
/// Retreives a collection of cached items
/// </summary>
/// <param name="keys">The collection of items' keys</param>
/// <returns>The collection of cache items</returns>
public IDictionary<string, T> Get<T>(IEnumerable<string> keys)
{
var value = this.UseL1Cache && this._L1Cache != null
? this._L1Cache.Get<T>(keys)
: keys == null ? null : this._distributedCache.Get<T>(keys);
if ((value == null || value.Count != keys.Count()) && this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(value = keys == null ? null : this._distributedCache.Get<T>(keys), null, this.GetExpiresAt(), false);
return value;
}
/// <summary>
/// Retreives a collection of cached items
/// </summary>
/// <param name="keys">The collection of items' keys</param>
/// <returns>The collection of cache items</returns>
public async Task<IDictionary<string, object>> GetAsync(IEnumerable<string> keys, CancellationToken cancellationToken = default)
{
var value = this.UseL1Cache && this._L1Cache != null
? this._L1Cache.Get(keys)
: keys == null ? null : await this._distributedCache.GetAsync(keys, cancellationToken).ConfigureAwait(false);
if ((value == null || value.Count != keys.Count()) && this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(value = keys == null ? null : await this._distributedCache.GetAsync(keys, cancellationToken).ConfigureAwait(false), null, this.GetExpiresAt(), false);
return value;
}
/// <summary>
/// Retreives a collection of cached items
/// </summary>
/// <param name="keys">The collection of items' keys</param>
/// <returns>The collection of cache items</returns>
public async Task<IDictionary<string, T>> GetAsync<T>(IEnumerable<string> keys, CancellationToken cancellationToken = default)
{
var value = this.UseL1Cache && this._L1Cache != null
? this._L1Cache.Get<T>(keys)
: keys == null ? null : await this._distributedCache.GetAsync<T>(keys, cancellationToken).ConfigureAwait(false);
if ((value == null || value.Count != keys.Count()) && this.UseL1Cache && this._L1Cache != null)
this._L1Cache.Set(value = keys == null ? null : await this._distributedCache.GetAsync<T>(keys, cancellationToken).ConfigureAwait(false), null, this.GetExpiresAt(), false);
return value;
}
#endregion
#region Get (Fragment)
/// <summary>
/// Gets fragment information that associates with the key
/// </summary>
/// <param name="key">The string that presents key of fragment information</param>
/// <returns>The information of fragments, first element is total number of fragments, second element is total length of data</returns>
public (int Blocks, int Length) GetFragments(string key)
=> this._distributedCache.GetFragments(key);
/// <summary>
/// Gets fragment information that associates with the key
/// </summary>
/// <param name="key">The string that presents key of fragment information</param>
/// <returns>The information of fragments, first element is total number of fragments, second element is total length of data</returns>
public Task<(int Blocks, int Length)> GetFragmentsAsync(string key, CancellationToken cancellationToken = default)
=> this._distributedCache.GetFragmentsAsync(key, cancellationToken);
/// <summary>
/// Gets cached of fragmented items that associates with the key and indexes
/// </summary>
/// <param name="key">The string that presents key of all fragmented items</param>
/// <param name="indexes">The collection that presents indexes of all fragmented items need to get</param>
/// <returns>The collection of array of bytes that presents serialized information of fragmented items</returns>
public List<byte[]> GetAsFragments(string key, List<int> indexes)
=> this._distributedCache.GetAsFragments(key, indexes);
/// <summary>
/// Gets cached of fragmented items that associates with the key and indexes
/// </summary>
/// <param name="key">The string that presents key of all fragmented items</param>
/// <param name="indexes">The collection that presents indexes of all fragmented items need to get</param>
/// <returns>The collection of array of bytes that presents serialized information of fragmented items</returns>
public List<byte[]> GetAsFragments(string key, params int[] indexes)
=> this._distributedCache.GetAsFragments(key, indexes);
/// <summary>
/// Gets cached of fragmented items that associates with the key and indexes
/// </summary>
/// <param name="key">The string that presents key of all fragmented items</param>
/// <param name="indexes">The collection that presents indexes of all fragmented items need to get</param>
/// <returns>The collection of array of bytes that presents serialized information of fragmented items</returns>
public Task<List<byte[]>> GetAsFragmentsAsync(string key, List<int> indexes, CancellationToken cancellationToken = default)
=> this._distributedCache.GetAsFragmentsAsync(key, indexes, cancellationToken);
/// <summary>
/// Gets cached of fragmented items that associates with the key and indexes
/// </summary>
/// <param name="key">The string that presents key of all fragmented items</param>
/// <param name="indexes">The collection that presents indexes of all fragmented items need to get</param>
/// <returns>The collection of array of bytes that presents serialized information of fragmented items</returns>
public Task<List<byte[]>> GetAsFragmentsAsync(string key, CancellationToken cancellationToken = default, params int[] indexes)
=> this._distributedCache.GetAsFragmentsAsync(key, cancellationToken, indexes);
#endregion
#region Remove
/// <summary>
/// Removes a cached item
/// </summary>
/// <param name="key">The string that presents key of cached item need to remove</param>
/// <returns>Returns a boolean value indicating if the item is removed or not</returns>
public bool Remove(string key)
=> this._distributedCache.Remove(key) && (this.UseL1Cache && this._L1Cache != null ? this._L1Cache.Remove(key) : this.SendL1CacheRequests(key, "remove"));
/// <summary>
/// Removes a cached item
/// </summary>
/// <param name="key">The string that presents key of cached item need to remove</param>