forked from vieapps/Enyim.Caching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemcachedClient.cs
1854 lines (1645 loc) · 90.3 KB
/
MemcachedClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
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.Net;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Enyim.Caching.Configuration;
using Enyim.Caching.Memcached;
using Enyim.Caching.Memcached.Results;
using Enyim.Caching.Memcached.Results.Factories;
using CacheUtils;
#endregion
#if !SIGN
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("VIEApps.Components.XUnitTests")]
#endif
namespace Enyim.Caching
{
public partial class MemcachedClient : IMemcachedClient, IMemcachedResultsClient, IDistributedCache
{
#region Attributes
readonly ILogger _logger;
public IStoreOperationResultFactory StoreOperationResultFactory { get; set; }
public IGetOperationResultFactory GetOperationResultFactory { get; set; }
public IMutateOperationResultFactory MutateOperationResultFactory { get; set; }
public IConcatOperationResultFactory ConcatOperationResultFactory { get; set; }
public IRemoveOperationResultFactory RemoveOperationResultFactory { get; set; }
protected IServerPool Pool { get; private set; }
protected IKeyTransformer KeyTransformer { get; private set; }
protected ITranscoder Transcoder { get; private set; }
public event Action<IMemcachedNode> NodeFailed;
#endregion
/// <summary>
/// Initializes a new instance of Memcached client (using configuration section of app.config/web.config file)
/// </summary>
/// <param name="configuration"></param>
/// <param name="loggerFactory"></param>
public MemcachedClient(MemcachedClientConfigurationSectionHandler configuration, ILoggerFactory loggerFactory = null)
: this(loggerFactory, configuration == null ? null : new MemcachedClientConfiguration(loggerFactory, configuration)) { }
/// <summary>
/// Initializes a new instance of Memcached client (using configuration section of appsettings.json file)
/// </summary>
/// <param name="loggerFactory"></param>
/// <param name="configuration"></param>
public MemcachedClient(ILoggerFactory loggerFactory, IMemcachedClientConfiguration configuration)
{
if (configuration == null)
throw new ArgumentNullException(nameof(configuration), "Configuration is invalid");
Logger.AssignLoggerFactory(loggerFactory);
this._logger = Logger.CreateLogger<MemcachedClient>();
this.KeyTransformer = configuration.CreateKeyTransformer() ?? new DefaultKeyTransformer();
this.Transcoder = configuration.CreateTranscoder() ?? new DefaultTranscoder();
this.Pool = configuration.CreatePool();
this.Pool.NodeFailed += node => this.NodeFailed?.Invoke(node);
this.Pool.Start();
this.StoreOperationResultFactory = new DefaultStoreOperationResultFactory();
this.GetOperationResultFactory = new DefaultGetOperationResultFactory();
this.MutateOperationResultFactory = new DefaultMutateOperationResultFactory();
this.ConcatOperationResultFactory = new DefaultConcatOperationResultFactory();
this.RemoveOperationResultFactory = new DefaultRemoveOperationResultFactory();
if (this._logger.IsEnabled(LogLevel.Debug))
{
var nodes = this.Pool.GetWorkingNodes().ToList();
this._logger.LogDebug($"The Memcached client's instance was created - {nodes.Count} node(s) => {string.Join(" - ", nodes.Select(node => node.EndPoint))}");
}
}
#region Get instance (singleton)
static MemcachedClient _Instance = null;
internal static MemcachedClient GetInstance(IServiceProvider svcProvider)
=> MemcachedClient._Instance ?? (MemcachedClient._Instance = new MemcachedClient(svcProvider.GetService<ILoggerFactory>(), svcProvider.GetService<IMemcachedClientConfiguration>()));
#endregion
#region Store
protected virtual IStoreOperationResult PerformStore(StoreMode mode, string key, object value, uint expires, ref ulong cas, out int statusCode)
{
var start = DateTime.Now;
if (this._logger.IsEnabled(LogLevel.Trace))
this._logger.LogDebug($"> Start to perform Store command => {key} ({mode})");
var result = this.StoreOperationResultFactory.Create();
statusCode = -1;
if (value == null)
{
this._logger.LogError("Value is null");
result.Fail("Value is null");
return result;
}
var hashedKey = this.KeyTransformer.Transform(key);
var node = this.Pool.Locate(hashedKey);
if (node != null)
{
CacheItem item;
try
{
item = this.Transcoder.Serialize(value);
}
catch (Exception ex)
{
this._logger.LogError(ex, $"Cannot serialize the value of '{key}'");
throw;
}
var command = this.Pool.OperationFactory.Store(mode, hashedKey, item, expires, cas);
var commandResult = node.Execute(command);
result.Cas = cas = command.CasValue;
result.StatusCode = statusCode = command.StatusCode;
if (commandResult.Success)
result.Pass();
else
{
commandResult.Combine(result);
if (this._logger.IsEnabled(LogLevel.Debug))
{
if (result.Message.StartsWith("Too large."))
this._logger.LogWarning(result.Exception, $"Failed to execute Store command: Object too large => {item.Data.Count:###,###,###,##0} bytes ({key})");
else if (result.Message != "Data exists for key.")
this._logger.LogDebug(result.Exception, $"Failed to execute Store command: {result.Message} ({key} - {mode})");
}
}
if (this._logger.IsEnabled(LogLevel.Trace))
this._logger.LogDebug($"Perform Store command successful - Duration: {(DateTime.Now - start).TotalMilliseconds}ms");
return result;
}
this._logger.LogError("Unable to locate node");
result.Fail("Unable to locate node");
return result;
}
IStoreOperationResult PerformStore(StoreMode mode, string key, object value, uint expires, ulong cas = 0)
{
ulong tmp = cas;
return this.PerformStore(mode, key, value, expires, ref tmp, out var status);
}
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <remarks>The item does not expire unless it is removed due memory pressure.</remarks>
/// <returns>true if the item was successfully stored in the cache; false otherwise.</returns>
public bool Store(StoreMode mode, string key, object value)
{
ulong tmp = 0;
return this.PerformStore(mode, key, value, 0, ref tmp, out var status).Success;
}
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <returns>true if the item was successfully stored in the cache; false otherwise.</returns>
public bool Store(StoreMode mode, string key, object value, TimeSpan validFor)
{
ulong tmp = 0;
return this.PerformStore(mode, key, value, validFor.GetExpiration(), ref tmp, out var status).Success;
}
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <returns>true if the item was successfully stored in the cache; false otherwise.</returns>
public bool Store(StoreMode mode, string key, object value, DateTime expiresAt)
{
ulong tmp = 0;
return this.PerformStore(mode, key, value, expiresAt.GetExpiration(), ref tmp, out var status).Success;
}
protected virtual async Task<IStoreOperationResult> PerformStoreAsync(StoreMode mode, string key, object value, uint expires, ulong cas = 0, CancellationToken cancellationToken = default)
{
var start = DateTime.Now;
if (this._logger.IsEnabled(LogLevel.Trace))
this._logger.LogDebug($"> Start to perform Store command => {key} ({mode})");
var result = this.StoreOperationResultFactory.Create();
if (value == null)
{
this._logger.LogError("Value is null");
result.Fail("Value is null");
return result;
}
var hashedKey = this.KeyTransformer.Transform(key);
var node = this.Pool.Locate(hashedKey);
if (node != null)
{
CacheItem item;
try
{
item = this.Transcoder.Serialize(value);
}
catch (Exception ex)
{
this._logger.LogError(ex, $"Cannot serialize the value of '{key}'");
throw ex;
}
var command = this.Pool.OperationFactory.Store(mode, hashedKey, item, expires, cas);
var commandResult = await node.ExecuteAsync(command, cancellationToken).ConfigureAwait(false);
result.Cas = command.CasValue;
result.StatusCode = command.StatusCode;
if (commandResult.Success)
result.Pass();
else
{
commandResult.Combine(result);
if (this._logger.IsEnabled(LogLevel.Debug))
{
if (result.Message.StartsWith("Too large."))
this._logger.LogWarning(result.Exception, $"Failed to execute Store command: Object too large => {item.Data.Count:###,###,###,##0} bytes ({key})");
else if (result.Message != "Data exists for key.")
this._logger.LogDebug(result.Exception, $"Failed to execute Store command: {result.Message} ({key} - {mode})");
}
}
if (this._logger.IsEnabled(LogLevel.Trace))
this._logger.LogDebug($"Perform Store command successful - Duration: {(DateTime.Now - start).TotalMilliseconds}ms");
return result;
}
this._logger.LogError("Unable to locate node");
result.Fail("Unable to locate node");
return result;
}
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <remarks>The item does not expire unless it is removed due memory pressure.</remarks>
/// <returns>true if the item was successfully stored in the cache; false otherwise.</returns>
public async Task<bool> StoreAsync(StoreMode mode, string key, object value, CancellationToken cancellationToken = default)
=> (await this.PerformStoreAsync(mode, key, value, 0, 0, cancellationToken).ConfigureAwait(false)).Success;
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <returns>true if the item was successfully stored in the cache; false otherwise.</returns>
public async Task<bool> StoreAsync(StoreMode mode, string key, object value, TimeSpan validFor, CancellationToken cancellationToken = default)
=> (await this.PerformStoreAsync(mode, key, value, validFor.GetExpiration(), 0, cancellationToken).ConfigureAwait(false)).Success;
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <returns>true if the item was successfully stored in the cache; false otherwise.</returns>
public async Task<bool> StoreAsync(StoreMode mode, string key, object value, DateTime expiresAt, CancellationToken cancellationToken = default)
=> (await this.PerformStoreAsync(mode, key, value, expiresAt.GetExpiration(), 0, cancellationToken).ConfigureAwait(false)).Success;
#endregion
#region CAS (Check And Store)
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location and returns its version.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
/// <remarks>The item does not expire unless it is removed due memory pressure.</remarks>
public CasResult<bool> Cas(StoreMode mode, string key, object value, ulong cas)
{
var result = this.PerformStore(mode, key, value, 0, cas);
return new CasResult<bool>
{
Cas = result.Cas,
Result = result.Success,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location and returns its version.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
/// <remarks>The item does not expire unless it is removed due memory pressure. The text protocol does not support this operation, you need to Store then GetWithCas.</remarks>
public CasResult<bool> Cas(StoreMode mode, string key, object value)
=> this.Cas(mode, key, value, 0);
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location and returns its version.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
public CasResult<bool> Cas(StoreMode mode, string key, object value, TimeSpan validFor, ulong cas)
{
var result = this.PerformStore(mode, key, value, validFor.GetExpiration(), cas);
return new CasResult<bool>
{
Cas = result.Cas,
Result = result.Success,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location and returns its version.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
public CasResult<bool> Cas(StoreMode mode, string key, object value, DateTime expiresAt, ulong cas)
{
var result = this.PerformStore(mode, key, value, expiresAt.GetExpiration(), cas);
return new CasResult<bool>
{
Cas = result.Cas,
Result = result.Success,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location and returns its version.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <remarks>The item does not expire unless it is removed due memory pressure.</remarks>
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
public async Task<CasResult<bool>> CasAsync(StoreMode mode, string key, object value, ulong cas, CancellationToken cancellationToken = default)
{
var result = await this.PerformStoreAsync(mode, key, value, 0, cas, cancellationToken).ConfigureAwait(false);
return new CasResult<bool>
{
Cas = result.Cas,
Result = result.Success,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location and returns its version.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <remarks>The item does not expire unless it is removed due memory pressure. The text protocol does not support this operation, you need to Store then GetWithCas.</remarks>
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
public Task<CasResult<bool>> CasAsync(StoreMode mode, string key, object value, CancellationToken cancellationToken = default)
=> this.CasAsync(mode, key, value, 0, cancellationToken);
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location and returns its version.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
public async Task<CasResult<bool>> CasAsync(StoreMode mode, string key, object value, TimeSpan validFor, ulong cas, CancellationToken cancellationToken = default)
{
var result = await this.PerformStoreAsync(mode, key, value, validFor.GetExpiration(), cas, cancellationToken).ConfigureAwait(false);
return new CasResult<bool>
{
Cas = result.Cas,
Result = result.Success,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location and returns its version.
/// </summary>
/// <param name="mode">Defines how the item is stored in the cache.</param>
/// <param name="key">The key used to reference the item.</param>
/// <param name="value">The object to be inserted into the cache.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>A CasResult object containing the version of the item and the result of the operation (true if the item was successfully stored in the cache; false otherwise).</returns>
public async Task<CasResult<bool>> CasAsync(StoreMode mode, string key, object value, DateTime expiresAt, ulong cas, CancellationToken cancellationToken = default)
{
var result = await this.PerformStoreAsync(mode, key, value, expiresAt.GetExpiration(), cas, cancellationToken).ConfigureAwait(false);
return new CasResult<bool>
{
Cas = result.Cas,
Result = result.Success,
StatusCode = result.StatusCode.Value
};
}
#endregion
#region Set
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="cacheMinutes"></param>
/// <returns>true if the item was successfully added in the cache; false otherwise.</returns>
public bool Set(string key, object value, int cacheMinutes)
=> this.Store(StoreMode.Set, key, value, cacheMinutes < 1 ? TimeSpan.Zero : TimeSpan.FromMinutes(cacheMinutes));
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="cacheMinutes"></param>
/// <returns>true if the item was successfully added in the cache; false otherwise.</returns>
public Task<bool> SetAsync(string key, object value, int cacheMinutes, CancellationToken cancellationToken = default)
=> this.StoreAsync(StoreMode.Set, key, value, cacheMinutes < 1 ? TimeSpan.Zero : TimeSpan.FromMinutes(cacheMinutes), cancellationToken);
#endregion
#region Add
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="cacheMinutes"></param>
/// <returns>true if the item was successfully added in the cache; false otherwise.</returns>
public bool Add(string key, object value, int cacheMinutes)
=> this.Store(StoreMode.Add, key, value, cacheMinutes < 1 ? TimeSpan.Zero : TimeSpan.FromMinutes(cacheMinutes));
/// <summary>
/// Inserts an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="cacheMinutes"></param>
/// <returns>true if the item was successfully added in the cache; false otherwise.</returns>
public Task<bool> AddAsync(string key, object value, int cacheMinutes, CancellationToken cancellationToken = default)
=> this.StoreAsync(StoreMode.Add, key, value, cacheMinutes < 1 ? TimeSpan.Zero : TimeSpan.FromMinutes(cacheMinutes), cancellationToken);
#endregion
#region Replace
/// <summary>
/// Replaces an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="cacheMinutes"></param>
/// <returns>true if the item was successfully replaced in the cache; false otherwise.</returns>
public bool Replace(string key, object value, int cacheMinutes)
=> this.Store(StoreMode.Replace, key, value, cacheMinutes < 1 ? TimeSpan.Zero : TimeSpan.FromMinutes(cacheMinutes));
/// <summary>
/// Replaces an item into the cache with a cache key to reference its location.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="cacheMinutes"></param>
/// <returns>true if the item was successfully replaced in the cache; false otherwise.</returns>
public Task<bool> ReplaceAsync(string key, object value, int cacheMinutes, CancellationToken cancellationToken = default)
=> this.StoreAsync(StoreMode.Replace, key, value, cacheMinutes < 1 ? TimeSpan.Zero : TimeSpan.FromMinutes(cacheMinutes), cancellationToken);
#endregion
#region Mutate
protected virtual IMutateOperationResult PerformMutate(MutationMode mode, string key, ulong defaultValue, ulong delta, uint expires, ulong cas = 0)
{
var start = DateTime.Now;
if (this._logger.IsEnabled(LogLevel.Trace))
this._logger.LogDebug($"> Start to perform Mutate command => {key} ({mode})");
var hashedKey = this.KeyTransformer.Transform(key);
var node = this.Pool.Locate(hashedKey);
var result = this.MutateOperationResultFactory.Create();
if (node != null)
{
var command = this.Pool.OperationFactory.Mutate(mode, hashedKey, defaultValue, delta, expires, cas);
var commandResult = node.Execute(command);
result.Cas = command.CasValue;
result.StatusCode = command.StatusCode;
if (commandResult.Success)
{
result.Value = command.Result;
result.Pass();
}
else
{
result.InnerResult = commandResult;
result.Fail("Mutate operation failed, see InnerResult or StatusCode for more details");
}
if (this._logger.IsEnabled(LogLevel.Trace))
this._logger.LogDebug($"Perform Mutate command successful - Duration: {(DateTime.Now - start).TotalMilliseconds}ms");
return result;
}
this._logger.LogError("Unable to locate node");
result.Fail("Unable to locate node");
return result;
}
IMutateOperationResult CasMutate(MutationMode mode, string key, ulong defaultValue, ulong delta, uint expires, ulong cas)
=> this.PerformMutate(mode, key, defaultValue, delta, expires, cas);
/// <summary>
/// Increments the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public ulong Increment(string key, ulong defaultValue, ulong delta)
=> this.PerformMutate(MutationMode.Increment, key, defaultValue, delta, 0).Value;
/// <summary>
/// Increments the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public ulong Increment(string key, ulong defaultValue, ulong delta, TimeSpan validFor)
=> this.PerformMutate(MutationMode.Increment, key, defaultValue, delta, validFor.GetExpiration()).Value;
/// <summary>
/// Increments the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public ulong Increment(string key, ulong defaultValue, ulong delta, DateTime expiresAt)
=> this.PerformMutate(MutationMode.Increment, key, defaultValue, delta, expiresAt.GetExpiration()).Value;
/// <summary>
/// Increments the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, ulong cas)
{
var result = this.CasMutate(MutationMode.Increment, key, defaultValue, delta, 0, cas);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Increments the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas)
{
var result = this.CasMutate(MutationMode.Increment, key, defaultValue, delta, validFor.GetExpiration(), cas);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Increments the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public CasResult<ulong> Increment(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas)
{
var result = this.CasMutate(MutationMode.Increment, key, defaultValue, delta, expiresAt.GetExpiration(), cas);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Decrements the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public ulong Decrement(string key, ulong defaultValue, ulong delta)
=> this.PerformMutate(MutationMode.Decrement, key, defaultValue, delta, 0).Value;
/// <summary>
/// Decrements the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public ulong Decrement(string key, ulong defaultValue, ulong delta, TimeSpan validFor)
=> this.PerformMutate(MutationMode.Decrement, key, defaultValue, delta, validFor.GetExpiration()).Value;
/// <summary>
/// Decrements the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public ulong Decrement(string key, ulong defaultValue, ulong delta, DateTime expiresAt)
=> this.PerformMutate(MutationMode.Decrement, key, defaultValue, delta, expiresAt.GetExpiration()).Value;
/// <summary>
/// Decrements the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public CasResult<ulong> Decrement(string key, ulong defaultValue, ulong delta, ulong cas)
{
var result = this.CasMutate(MutationMode.Decrement, key, defaultValue, delta, 0, cas);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Decrements the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public CasResult<ulong> Decrement(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas)
{
var result = this.CasMutate(MutationMode.Decrement, key, defaultValue, delta, validFor.GetExpiration(), cas);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Decrements the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public CasResult<ulong> Decrement(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas)
{
var result = this.CasMutate(MutationMode.Decrement, key, defaultValue, delta, expiresAt.GetExpiration(), cas);
return new CasResult<ulong>()
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
protected virtual async Task<IMutateOperationResult> PerformMutateAsync(MutationMode mode, string key, ulong defaultValue, ulong delta, uint expires, ulong cas = 0, CancellationToken cancellationToken = default)
{
var start = DateTime.Now;
if (this._logger.IsEnabled(LogLevel.Trace))
this._logger.LogDebug($"> Start to perform Mutate command => {key} ({mode})");
var hashedKey = this.KeyTransformer.Transform(key);
var node = this.Pool.Locate(hashedKey);
var result = this.MutateOperationResultFactory.Create();
if (node != null)
{
var command = this.Pool.OperationFactory.Mutate(mode, hashedKey, defaultValue, delta, expires, cas);
var commandResult = await node.ExecuteAsync(command, cancellationToken).ConfigureAwait(false);
result.Cas = command.CasValue;
result.StatusCode = command.StatusCode;
if (commandResult.Success)
{
result.Value = command.Result;
result.Pass();
}
else
{
result.InnerResult = commandResult;
result.Fail("Mutate operation failed, see InnerResult or StatusCode for more details");
}
if (this._logger.IsEnabled(LogLevel.Trace))
this._logger.LogDebug($"Perform Mutate command successful - Duration: {(DateTime.Now - start).TotalMilliseconds}ms");
return result;
}
this._logger.LogError("Unable to locate node");
result.Fail("Unable to locate node");
return result;
}
Task<IMutateOperationResult> CasMutateAsync(MutationMode mode, string key, ulong defaultValue, ulong delta, uint expires, ulong cas, CancellationToken cancellationToken = default)
=> this.PerformMutateAsync(mode, key, defaultValue, delta, expires, cas, cancellationToken);
/// <summary>
/// Increments the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<ulong> IncrementAsync(string key, ulong defaultValue, ulong delta, CancellationToken cancellationToken = default)
=> (await this.PerformMutateAsync(MutationMode.Increment, key, defaultValue, delta, 0, 0, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Increments the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<ulong> IncrementAsync(string key, ulong defaultValue, ulong delta, TimeSpan validFor, CancellationToken cancellationToken = default)
=> (await this.PerformMutateAsync(MutationMode.Increment, key, defaultValue, delta, validFor.GetExpiration(), 0, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Increments the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<ulong> IncrementAsync(string key, ulong defaultValue, ulong delta, DateTime expiresAt, CancellationToken cancellationToken = default)
=> (await this.PerformMutateAsync(MutationMode.Increment, key, defaultValue, delta, expiresAt.GetExpiration(), 0, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Increments the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<CasResult<ulong>> IncrementAsync(string key, ulong defaultValue, ulong delta, ulong cas, CancellationToken cancellationToken = default)
{
var result = await this.CasMutateAsync(MutationMode.Increment, key, defaultValue, delta, 0, cas, cancellationToken).ConfigureAwait(false);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Increments the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<CasResult<ulong>> IncrementAsync(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas, CancellationToken cancellationToken = default)
{
var result = await this.CasMutateAsync(MutationMode.Increment, key, defaultValue, delta, validFor.GetExpiration(), cas, cancellationToken).ConfigureAwait(false);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Increments the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to increase the item.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<CasResult<ulong>> IncrementAsync(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas, CancellationToken cancellationToken = default)
{
var result = await this.CasMutateAsync(MutationMode.Increment, key, defaultValue, delta, expiresAt.GetExpiration(), cas, cancellationToken).ConfigureAwait(false);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Decrements the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<ulong> DecrementAsync(string key, ulong defaultValue, ulong delta, CancellationToken cancellationToken = default)
=> (await this.PerformMutateAsync(MutationMode.Decrement, key, defaultValue, delta, 0, 0, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Decrements the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<ulong> DecrementAsync(string key, ulong defaultValue, ulong delta, TimeSpan validFor, CancellationToken cancellationToken = default)
=> (await this.PerformMutateAsync(MutationMode.Decrement, key, defaultValue, delta, validFor.GetExpiration(), 0, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Decrements the value of the specified key by the given amount. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<ulong> DecrementAsync(string key, ulong defaultValue, ulong delta, DateTime expiresAt, CancellationToken cancellationToken = default)
=> (await this.PerformMutateAsync(MutationMode.Decrement, key, defaultValue, delta, expiresAt.GetExpiration(), 0, cancellationToken).ConfigureAwait(false)).Value;
/// <summary>
/// Decrements the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<CasResult<ulong>> DecrementAsync(string key, ulong defaultValue, ulong delta, ulong cas, CancellationToken cancellationToken = default)
{
var result = await this.CasMutateAsync(MutationMode.Decrement, key, defaultValue, delta, 0, cas, cancellationToken).ConfigureAwait(false);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Decrements the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="validFor">The interval after the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<CasResult<ulong>> DecrementAsync(string key, ulong defaultValue, ulong delta, TimeSpan validFor, ulong cas, CancellationToken cancellationToken = default)
{
var result = await this.CasMutateAsync(MutationMode.Decrement, key, defaultValue, delta, validFor.GetExpiration(), cas, cancellationToken).ConfigureAwait(false);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
/// <summary>
/// Decrements the value of the specified key by the given amount, but only if the item's version matches the CAS value provided. The operation is atomic and happens on the server.
/// </summary>
/// <param name="key">The key used to reference the item.</param>
/// <param name="defaultValue">The value which will be stored by the server if the specified item was not found.</param>
/// <param name="delta">The amount by which the client wants to decrease the item.</param>
/// <param name="expiresAt">The time when the item is invalidated in the cache.</param>
/// <param name="cas">The cas value which must match the item's version.</param>
/// <returns>The new value of the item or defaultValue if the key was not found.</returns>
/// <remarks>If the client uses the Text protocol, the item must be inserted into the cache before it can be changed. It must be inserted as a <see cref="System.String"/>. Moreover the Text protocol only works with <see cref="System.UInt32"/> values, so return value -1 always indicates that the item was not found.</remarks>
public async Task<CasResult<ulong>> DecrementAsync(string key, ulong defaultValue, ulong delta, DateTime expiresAt, ulong cas, CancellationToken cancellationToken = default)
{
var result = await this.CasMutateAsync(MutationMode.Decrement, key, defaultValue, delta, expiresAt.GetExpiration(), cas, cancellationToken).ConfigureAwait(false);
return new CasResult<ulong>
{
Cas = result.Cas,
Result = result.Value,
StatusCode = result.StatusCode.Value
};
}
#endregion
#region Concatenate
protected virtual IConcatOperationResult PerformConcatenate(ConcatenationMode mode, string key, ref ulong cas, ArraySegment<byte> data)
{
var start = DateTime.Now;
if (this._logger.IsEnabled(LogLevel.Trace))
this._logger.LogDebug($"> Start to perform Concat command => {key} ({mode})");
var hashedKey = this.KeyTransformer.Transform(key);
var node = this.Pool.Locate(hashedKey);
var result = this.ConcatOperationResultFactory.Create();
if (node != null)
{
var command = this.Pool.OperationFactory.Concat(mode, hashedKey, cas, data);
var commandResult = node.Execute(command);