@@ -171,15 +171,17 @@ public virtual object Get(object key)
171
171
{
172
172
return null ;
173
173
}
174
+
174
175
var cacheKey = GetCacheKey ( key ) ;
176
+ Log . Debug ( "Fetching object with key: '{0}'." , cacheKey ) ;
175
177
RedisValue result ;
176
178
if ( string . IsNullOrEmpty ( GetScript ) )
177
179
{
178
180
result = Database . StringGet ( cacheKey ) ;
179
181
}
180
182
else
181
183
{
182
- var keys = AppendAdditionalKeys ( new RedisKey [ ] { cacheKey } ) ;
184
+ var keys = AppendAdditionalKeys ( new RedisKey [ ] { cacheKey } ) ;
183
185
var values = AppendAdditionalValues ( new RedisValue [ ]
184
186
{
185
187
UseSlidingExpiration && ExpirationEnabled ,
@@ -188,6 +190,7 @@ public virtual object Get(object key)
188
190
var results = ( RedisValue [ ] ) Database . ScriptEvaluate ( GetScript , keys , values ) ;
189
191
result = results [ 0 ] ;
190
192
}
193
+
191
194
return result . IsNullOrEmpty ? null : Serializer . Deserialize ( result ) ;
192
195
}
193
196
@@ -202,11 +205,15 @@ public virtual object[] GetMany(object[] keys)
202
205
{
203
206
return null ;
204
207
}
208
+
205
209
var cacheKeys = new RedisKey [ keys . Length ] ;
210
+ Log . Debug ( "Fetching {0} objects..." , keys . Length ) ;
206
211
for ( var i = 0 ; i < keys . Length ; i ++ )
207
212
{
208
213
cacheKeys [ i ] = GetCacheKey ( keys [ i ] ) ;
214
+ Log . Debug ( "Fetching object with key: '{0}'." , cacheKeys [ i ] ) ;
209
215
}
216
+
210
217
RedisValue [ ] results ;
211
218
if ( string . IsNullOrEmpty ( GetManyScript ) )
212
219
{
@@ -232,6 +239,7 @@ public virtual object[] GetMany(object[] keys)
232
239
objects [ i ] = Serializer . Deserialize ( result ) ;
233
240
}
234
241
}
242
+
235
243
return objects ;
236
244
}
237
245
@@ -246,11 +254,14 @@ public virtual void Put(object key, object value)
246
254
{
247
255
throw new ArgumentNullException ( nameof ( key ) ) ;
248
256
}
257
+
249
258
if ( value == null )
250
259
{
251
260
throw new ArgumentNullException ( nameof ( value ) ) ;
252
261
}
262
+
253
263
var cacheKey = GetCacheKey ( key ) ;
264
+ Log . Debug ( "Putting object with key: '{0}'." , cacheKey ) ;
254
265
RedisValue serializedValue = Serializer . Serialize ( value ) ;
255
266
256
267
if ( string . IsNullOrEmpty ( PutScript ) )
@@ -264,7 +275,7 @@ public virtual void Put(object key, object value)
264
275
{
265
276
serializedValue ,
266
277
ExpirationEnabled ,
267
- ( long ) Expiration . TotalMilliseconds
278
+ ( long ) Expiration . TotalMilliseconds
268
279
} ) ;
269
280
Database . ScriptEvaluate ( PutScript , keys , values ) ;
270
281
}
@@ -280,37 +291,46 @@ public virtual void PutMany(object[] keys, object[] values)
280
291
{
281
292
throw new ArgumentNullException ( nameof ( keys ) ) ;
282
293
}
294
+
283
295
if ( values == null )
284
296
{
285
297
throw new ArgumentNullException ( nameof ( values ) ) ;
286
298
}
299
+
287
300
if ( keys . Length != values . Length )
288
301
{
289
302
throw new ArgumentException ( $ "Length of { nameof ( keys ) } array does not match with { nameof ( values ) } array.") ;
290
303
}
304
+
305
+ Log . Debug ( "Putting {0} objects..." , keys . Length ) ;
291
306
if ( string . IsNullOrEmpty ( PutManyScript ) )
292
307
{
293
308
if ( ExpirationEnabled )
294
309
{
295
- throw new NotSupportedException ( $ "{ nameof ( PutMany ) } operation is not supported.") ;
310
+ throw new NotSupportedException ( $ "{ nameof ( PutMany ) } operation with expiration is not supported.") ;
296
311
}
297
- var pairs = new KeyValuePair < RedisKey , RedisValue > [ keys . Length ] ;
312
+
313
+ var pairs = new KeyValuePair < RedisKey , RedisValue > [ keys . Length ] ;
298
314
for ( var i = 0 ; i < keys . Length ; i ++ )
299
315
{
300
316
pairs [ i ] = new KeyValuePair < RedisKey , RedisValue > ( GetCacheKey ( keys [ i ] ) , Serializer . Serialize ( values [ i ] ) ) ;
317
+ Log . Debug ( "Putting object with key: '{0}'." , pairs [ i ] . Key ) ;
301
318
}
319
+
302
320
Database . StringSet ( pairs ) ;
303
321
return ;
304
322
}
305
-
323
+
306
324
307
325
var cacheKeys = new RedisKey [ keys . Length ] ;
308
326
var cacheValues = new RedisValue [ keys . Length + 2 ] ;
309
327
for ( var i = 0 ; i < keys . Length ; i ++ )
310
328
{
311
329
cacheKeys [ i ] = GetCacheKey ( keys [ i ] ) ;
312
330
cacheValues [ i ] = Serializer . Serialize ( values [ i ] ) ;
331
+ Log . Debug ( "Putting object with key: '{0}'." , cacheKeys [ i ] ) ;
313
332
}
333
+
314
334
cacheKeys = AppendAdditionalKeys ( cacheKeys ) ;
315
335
cacheValues [ keys . Length ] = ExpirationEnabled ;
316
336
cacheValues [ keys . Length + 1 ] = ( long ) Expiration . TotalMilliseconds ;
@@ -330,11 +350,13 @@ public virtual bool Remove(object key)
330
350
}
331
351
332
352
var cacheKey = GetCacheKey ( key ) ;
353
+ Log . Debug ( "Removing object with key: '{0}'." , cacheKey ) ;
333
354
if ( string . IsNullOrEmpty ( RemoveScript ) )
334
355
{
335
356
return Database . KeyDelete ( cacheKey ) ;
336
357
}
337
- var keys = AppendAdditionalKeys ( new RedisKey [ ] { cacheKey } ) ;
358
+
359
+ var keys = AppendAdditionalKeys ( new RedisKey [ ] { cacheKey } ) ;
338
360
var values = GetAdditionalValues ( ) ;
339
361
var results = ( RedisValue [ ] ) Database . ScriptEvaluate ( RemoveScript , keys , values ) ;
340
362
return ( bool ) results [ 0 ] ;
@@ -350,15 +372,20 @@ public virtual long RemoveMany(object[] keys)
350
372
{
351
373
throw new ArgumentNullException ( nameof ( keys ) ) ;
352
374
}
375
+
376
+ Log . Debug ( "Removing {0} objects..." , keys . Length ) ;
353
377
var cacheKeys = new RedisKey [ keys . Length ] ;
354
378
for ( var i = 0 ; i < keys . Length ; i ++ )
355
379
{
356
380
cacheKeys [ i ] = GetCacheKey ( keys [ i ] ) ;
381
+ Log . Debug ( "Removing object with key: '{0}'." , cacheKeys [ i ] ) ;
357
382
}
383
+
358
384
if ( string . IsNullOrEmpty ( RemoveManyScript ) )
359
385
{
360
386
return Database . KeyDelete ( cacheKeys ) ;
361
387
}
388
+
362
389
cacheKeys = AppendAdditionalKeys ( cacheKeys ) ;
363
390
var results = ( RedisValue [ ] ) Database . ScriptEvaluate ( RemoveManyScript , cacheKeys , GetAdditionalValues ( ) ) ;
364
391
return ( long ) results [ 0 ] ;
@@ -375,7 +402,9 @@ public virtual string Lock(object key)
375
402
{
376
403
throw new ArgumentNullException ( nameof ( key ) ) ;
377
404
}
405
+
378
406
var cacheKey = GetCacheKey ( key ) ;
407
+ Log . Debug ( "Locking object with key: '{0}'." , cacheKey ) ;
379
408
var lockValue = _keyLocker . Lock ( cacheKey , LockScript , GetAdditionalKeys ( ) , GetAdditionalValues ( ) ) ;
380
409
381
410
_acquiredKeyLocks . AddOrUpdate ( cacheKey , _ => lockValue , ( _ , currValue ) =>
@@ -400,15 +429,20 @@ public virtual string LockMany(object[] keys)
400
429
{
401
430
throw new ArgumentNullException ( nameof ( keys ) ) ;
402
431
}
432
+
403
433
if ( string . IsNullOrEmpty ( LockManyScript ) )
404
434
{
405
435
throw new NotSupportedException ( $ "{ nameof ( LockMany ) } operation is not supported.") ;
406
436
}
437
+
438
+ Log . Debug ( "Locking {0} objects..." , keys . Length ) ;
407
439
var cacheKeys = new string [ keys . Length ] ;
408
440
for ( var i = 0 ; i < keys . Length ; i ++ )
409
441
{
410
442
cacheKeys [ i ] = GetCacheKey ( keys [ i ] ) ;
443
+ Log . Debug ( "Locking object with key: '{0}'." , cacheKeys [ i ] ) ;
411
444
}
445
+
412
446
return _keyLocker . LockMany ( cacheKeys , LockManyScript , GetAdditionalKeys ( ) , GetAdditionalValues ( ) ) ;
413
447
}
414
448
@@ -423,15 +457,20 @@ public virtual bool Unlock(object key)
423
457
{
424
458
throw new ArgumentNullException ( nameof ( key ) ) ;
425
459
}
460
+
426
461
var cacheKey = GetCacheKey ( key ) ;
462
+ Log . Debug ( "Unlocking object with key: '{0}'." , cacheKey ) ;
427
463
428
464
if ( ! _acquiredKeyLocks . TryRemove ( cacheKey , out var lockValue ) )
429
465
{
430
466
Log . Warn (
431
467
$ "Calling { nameof ( Unlock ) } method for key:'{ cacheKey } ' that was not locked with { nameof ( Lock ) } method before.") ;
432
468
return false ;
433
469
}
434
- return _keyLocker . Unlock ( cacheKey , lockValue , UnlockScript , GetAdditionalKeys ( ) , GetAdditionalValues ( ) ) ;
470
+
471
+ var unlocked = _keyLocker . Unlock ( cacheKey , lockValue , UnlockScript , GetAdditionalKeys ( ) , GetAdditionalValues ( ) ) ;
472
+ Log . Debug ( "Unlock key '{0}' result: {1}" , cacheKey , unlocked ) ;
473
+ return unlocked ;
435
474
}
436
475
437
476
/// <summary>
@@ -446,17 +485,29 @@ public virtual int UnlockMany(object[] keys, string lockValue)
446
485
{
447
486
throw new ArgumentNullException ( nameof ( keys ) ) ;
448
487
}
488
+
449
489
if ( string . IsNullOrEmpty ( UnlockManyScript ) )
450
490
{
451
491
throw new NotSupportedException ( $ "{ nameof ( UnlockMany ) } operation is not supported.") ;
452
492
}
493
+
494
+ Log . Debug ( "Unlocking {0} objects..." , keys . Length ) ;
453
495
var cacheKeys = new string [ keys . Length ] ;
454
496
for ( var i = 0 ; i < keys . Length ; i ++ )
455
497
{
456
- var cacheKey = GetCacheKey ( keys [ i ] ) ;
457
- cacheKeys [ i ] = cacheKey ;
498
+ cacheKeys [ i ] = GetCacheKey ( keys [ i ] ) ;
499
+ Log . Debug ( "Unlocking object with key: '{0}'." , cacheKeys [ i ] ) ;
458
500
}
459
- return _keyLocker . UnlockMany ( cacheKeys , lockValue , UnlockManyScript , GetAdditionalKeys ( ) , GetAdditionalValues ( ) ) ;
501
+
502
+ var unlockedKeys =
503
+ _keyLocker . UnlockMany ( cacheKeys , lockValue , UnlockManyScript , GetAdditionalKeys ( ) , GetAdditionalValues ( ) ) ;
504
+ if ( Log . IsDebugEnabled ( ) )
505
+ {
506
+ Log . Debug ( "Number of unlocked objects with keys ({0}): {1}" , string . Join ( "," , cacheKeys . Select ( o => $ "'{ o } '") ) ,
507
+ unlockedKeys ) ;
508
+ }
509
+
510
+ return unlockedKeys ;
460
511
}
461
512
462
513
/// <summary>
@@ -513,11 +564,13 @@ protected RedisValue[] AppendAdditionalValues(RedisValue[] values)
513
564
{
514
565
return GetAdditionalValues ( ) ;
515
566
}
567
+
516
568
var additionalValues = GetAdditionalValues ( ) ;
517
569
if ( additionalValues == null )
518
570
{
519
571
return values ;
520
572
}
573
+
521
574
var combinedValues = new RedisValue [ values . Length + additionalValues . Length ] ;
522
575
values . CopyTo ( combinedValues , 0 ) ;
523
576
additionalValues . CopyTo ( combinedValues , values . Length ) ;
@@ -535,11 +588,13 @@ protected RedisKey[] AppendAdditionalKeys(RedisKey[] keys)
535
588
{
536
589
return GetAdditionalKeys ( ) ;
537
590
}
591
+
538
592
var additionalKeys = GetAdditionalKeys ( ) ;
539
593
if ( additionalKeys == null )
540
594
{
541
595
return keys ;
542
596
}
597
+
543
598
var combinedKeys = new RedisKey [ keys . Length + additionalKeys . Length ] ;
544
599
keys . CopyTo ( combinedKeys , 0 ) ;
545
600
additionalKeys . CopyTo ( combinedKeys , keys . Length ) ;
0 commit comments