Skip to content

Commit e44438d

Browse files
committed
Added logging to region strategies
1 parent 2cec1db commit e44438d

File tree

4 files changed

+199
-33
lines changed

4 files changed

+199
-33
lines changed

StackExRedis/NHibernate.Caches.StackExRedis/AbstractRegionStrategy.cs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,17 @@ public virtual object Get(object key)
171171
{
172172
return null;
173173
}
174+
174175
var cacheKey = GetCacheKey(key);
176+
Log.Debug("Fetching object with key: '{0}'.", cacheKey);
175177
RedisValue result;
176178
if (string.IsNullOrEmpty(GetScript))
177179
{
178180
result = Database.StringGet(cacheKey);
179181
}
180182
else
181183
{
182-
var keys = AppendAdditionalKeys(new RedisKey[] { cacheKey });
184+
var keys = AppendAdditionalKeys(new RedisKey[] {cacheKey});
183185
var values = AppendAdditionalValues(new RedisValue[]
184186
{
185187
UseSlidingExpiration && ExpirationEnabled,
@@ -188,6 +190,7 @@ public virtual object Get(object key)
188190
var results = (RedisValue[]) Database.ScriptEvaluate(GetScript, keys, values);
189191
result = results[0];
190192
}
193+
191194
return result.IsNullOrEmpty ? null : Serializer.Deserialize(result);
192195
}
193196

@@ -202,11 +205,15 @@ public virtual object[] GetMany(object[] keys)
202205
{
203206
return null;
204207
}
208+
205209
var cacheKeys = new RedisKey[keys.Length];
210+
Log.Debug("Fetching {0} objects...", keys.Length);
206211
for (var i = 0; i < keys.Length; i++)
207212
{
208213
cacheKeys[i] = GetCacheKey(keys[i]);
214+
Log.Debug("Fetching object with key: '{0}'.", cacheKeys[i]);
209215
}
216+
210217
RedisValue[] results;
211218
if (string.IsNullOrEmpty(GetManyScript))
212219
{
@@ -232,6 +239,7 @@ public virtual object[] GetMany(object[] keys)
232239
objects[i] = Serializer.Deserialize(result);
233240
}
234241
}
242+
235243
return objects;
236244
}
237245

@@ -246,11 +254,14 @@ public virtual void Put(object key, object value)
246254
{
247255
throw new ArgumentNullException(nameof(key));
248256
}
257+
249258
if (value == null)
250259
{
251260
throw new ArgumentNullException(nameof(value));
252261
}
262+
253263
var cacheKey = GetCacheKey(key);
264+
Log.Debug("Putting object with key: '{0}'.", cacheKey);
254265
RedisValue serializedValue = Serializer.Serialize(value);
255266

256267
if (string.IsNullOrEmpty(PutScript))
@@ -264,7 +275,7 @@ public virtual void Put(object key, object value)
264275
{
265276
serializedValue,
266277
ExpirationEnabled,
267-
(long)Expiration.TotalMilliseconds
278+
(long) Expiration.TotalMilliseconds
268279
});
269280
Database.ScriptEvaluate(PutScript, keys, values);
270281
}
@@ -280,37 +291,46 @@ public virtual void PutMany(object[] keys, object[] values)
280291
{
281292
throw new ArgumentNullException(nameof(keys));
282293
}
294+
283295
if (values == null)
284296
{
285297
throw new ArgumentNullException(nameof(values));
286298
}
299+
287300
if (keys.Length != values.Length)
288301
{
289302
throw new ArgumentException($"Length of {nameof(keys)} array does not match with {nameof(values)} array.");
290303
}
304+
305+
Log.Debug("Putting {0} objects...", keys.Length);
291306
if (string.IsNullOrEmpty(PutManyScript))
292307
{
293308
if (ExpirationEnabled)
294309
{
295-
throw new NotSupportedException($"{nameof(PutMany)} operation is not supported.");
310+
throw new NotSupportedException($"{nameof(PutMany)} operation with expiration is not supported.");
296311
}
297-
var pairs = new KeyValuePair<RedisKey,RedisValue>[keys.Length];
312+
313+
var pairs = new KeyValuePair<RedisKey, RedisValue>[keys.Length];
298314
for (var i = 0; i < keys.Length; i++)
299315
{
300316
pairs[i] = new KeyValuePair<RedisKey, RedisValue>(GetCacheKey(keys[i]), Serializer.Serialize(values[i]));
317+
Log.Debug("Putting object with key: '{0}'.", pairs[i].Key);
301318
}
319+
302320
Database.StringSet(pairs);
303321
return;
304322
}
305-
323+
306324

307325
var cacheKeys = new RedisKey[keys.Length];
308326
var cacheValues = new RedisValue[keys.Length + 2];
309327
for (var i = 0; i < keys.Length; i++)
310328
{
311329
cacheKeys[i] = GetCacheKey(keys[i]);
312330
cacheValues[i] = Serializer.Serialize(values[i]);
331+
Log.Debug("Putting object with key: '{0}'.", cacheKeys[i]);
313332
}
333+
314334
cacheKeys = AppendAdditionalKeys(cacheKeys);
315335
cacheValues[keys.Length] = ExpirationEnabled;
316336
cacheValues[keys.Length + 1] = (long) Expiration.TotalMilliseconds;
@@ -330,11 +350,13 @@ public virtual bool Remove(object key)
330350
}
331351

332352
var cacheKey = GetCacheKey(key);
353+
Log.Debug("Removing object with key: '{0}'.", cacheKey);
333354
if (string.IsNullOrEmpty(RemoveScript))
334355
{
335356
return Database.KeyDelete(cacheKey);
336357
}
337-
var keys = AppendAdditionalKeys(new RedisKey[] { cacheKey });
358+
359+
var keys = AppendAdditionalKeys(new RedisKey[] {cacheKey});
338360
var values = GetAdditionalValues();
339361
var results = (RedisValue[]) Database.ScriptEvaluate(RemoveScript, keys, values);
340362
return (bool) results[0];
@@ -350,15 +372,20 @@ public virtual long RemoveMany(object[] keys)
350372
{
351373
throw new ArgumentNullException(nameof(keys));
352374
}
375+
376+
Log.Debug("Removing {0} objects...", keys.Length);
353377
var cacheKeys = new RedisKey[keys.Length];
354378
for (var i = 0; i < keys.Length; i++)
355379
{
356380
cacheKeys[i] = GetCacheKey(keys[i]);
381+
Log.Debug("Removing object with key: '{0}'.", cacheKeys[i]);
357382
}
383+
358384
if (string.IsNullOrEmpty(RemoveManyScript))
359385
{
360386
return Database.KeyDelete(cacheKeys);
361387
}
388+
362389
cacheKeys = AppendAdditionalKeys(cacheKeys);
363390
var results = (RedisValue[]) Database.ScriptEvaluate(RemoveManyScript, cacheKeys, GetAdditionalValues());
364391
return (long) results[0];
@@ -375,7 +402,9 @@ public virtual string Lock(object key)
375402
{
376403
throw new ArgumentNullException(nameof(key));
377404
}
405+
378406
var cacheKey = GetCacheKey(key);
407+
Log.Debug("Locking object with key: '{0}'.", cacheKey);
379408
var lockValue = _keyLocker.Lock(cacheKey, LockScript, GetAdditionalKeys(), GetAdditionalValues());
380409

381410
_acquiredKeyLocks.AddOrUpdate(cacheKey, _ => lockValue, (_, currValue) =>
@@ -400,15 +429,20 @@ public virtual string LockMany(object[] keys)
400429
{
401430
throw new ArgumentNullException(nameof(keys));
402431
}
432+
403433
if (string.IsNullOrEmpty(LockManyScript))
404434
{
405435
throw new NotSupportedException($"{nameof(LockMany)} operation is not supported.");
406436
}
437+
438+
Log.Debug("Locking {0} objects...", keys.Length);
407439
var cacheKeys = new string[keys.Length];
408440
for (var i = 0; i < keys.Length; i++)
409441
{
410442
cacheKeys[i] = GetCacheKey(keys[i]);
443+
Log.Debug("Locking object with key: '{0}'.", cacheKeys[i]);
411444
}
445+
412446
return _keyLocker.LockMany(cacheKeys, LockManyScript, GetAdditionalKeys(), GetAdditionalValues());
413447
}
414448

@@ -423,15 +457,20 @@ public virtual bool Unlock(object key)
423457
{
424458
throw new ArgumentNullException(nameof(key));
425459
}
460+
426461
var cacheKey = GetCacheKey(key);
462+
Log.Debug("Unlocking object with key: '{0}'.", cacheKey);
427463

428464
if (!_acquiredKeyLocks.TryRemove(cacheKey, out var lockValue))
429465
{
430466
Log.Warn(
431467
$"Calling {nameof(Unlock)} method for key:'{cacheKey}' that was not locked with {nameof(Lock)} method before.");
432468
return false;
433469
}
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;
435474
}
436475

437476
/// <summary>
@@ -446,17 +485,29 @@ public virtual int UnlockMany(object[] keys, string lockValue)
446485
{
447486
throw new ArgumentNullException(nameof(keys));
448487
}
488+
449489
if (string.IsNullOrEmpty(UnlockManyScript))
450490
{
451491
throw new NotSupportedException($"{nameof(UnlockMany)} operation is not supported.");
452492
}
493+
494+
Log.Debug("Unlocking {0} objects...", keys.Length);
453495
var cacheKeys = new string[keys.Length];
454496
for (var i = 0; i < keys.Length; i++)
455497
{
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]);
458500
}
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;
460511
}
461512

462513
/// <summary>
@@ -513,11 +564,13 @@ protected RedisValue[] AppendAdditionalValues(RedisValue[] values)
513564
{
514565
return GetAdditionalValues();
515566
}
567+
516568
var additionalValues = GetAdditionalValues();
517569
if (additionalValues == null)
518570
{
519571
return values;
520572
}
573+
521574
var combinedValues = new RedisValue[values.Length + additionalValues.Length];
522575
values.CopyTo(combinedValues, 0);
523576
additionalValues.CopyTo(combinedValues, values.Length);
@@ -535,11 +588,13 @@ protected RedisKey[] AppendAdditionalKeys(RedisKey[] keys)
535588
{
536589
return GetAdditionalKeys();
537590
}
591+
538592
var additionalKeys = GetAdditionalKeys();
539593
if (additionalKeys == null)
540594
{
541595
return keys;
542596
}
597+
543598
var combinedKeys = new RedisKey[keys.Length + additionalKeys.Length];
544599
keys.CopyTo(combinedKeys, 0);
545600
additionalKeys.CopyTo(combinedKeys, keys.Length);

0 commit comments

Comments
 (0)