From 412bfd5a9883f71fd3e3e30ec0a6064b61c7191e Mon Sep 17 00:00:00 2001 From: Galip Tolga Erdem Date: Sat, 19 Oct 2019 16:52:35 +0300 Subject: [PATCH] Complex object key calculation reverted. Overriding ToString method of complex object to be used as key as suggested. --- .../Volo/Abp/Caching/DistributedCache.cs | 82 +++++++------------ .../Abp/Caching/DistributedCache_Tests.cs | 10 +-- .../Volo/Abp/Caching/PersonCacheItem.cs | 28 ++++++- 3 files changed, 60 insertions(+), 60 deletions(-) diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs index be8fde19f2..feef4f889b 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; @@ -44,7 +43,7 @@ namespace Volo.Abp.Caching /// The type of cache key being used. public class DistributedCache : IDistributedCache where TCacheItem : class - { + { public ILogger> Logger { get; set; } protected string CacheName { get; set; } @@ -88,27 +87,8 @@ namespace Volo.Abp.Caching SetDefaultOptions(); } protected virtual string NormalizeKey(TCacheKey key) - { - Type type = key.GetType(); - string keyValue = key.ToString(); - - // If complex type of object, override the key value with property concatenation - if (!typeof(IComparable).IsAssignableFrom(type) || type.IsPrimitive || type.IsValueType) - { - var sb = new System.Text.StringBuilder(); - var properties = type.GetProperties().Where(prop => prop.CanRead && prop.CanWrite); - - foreach (var prop in properties) - { - var value = prop.GetValue(key, null); - if (value != null) - { - sb.Append(value.ToString()); - } - } - keyValue = sb.ToString(); - } - var normalizedKey = "c:" + CacheName + ",k:" + _cacheOption.KeyPrefix + keyValue; + { + var normalizedKey = "c:" + CacheName + ",k:" + _cacheOption.KeyPrefix + key.ToString(); if (!IgnoreMultiTenancy && CurrentTenant.Id.HasValue) { @@ -116,7 +96,7 @@ namespace Volo.Abp.Caching } return normalizedKey; - } + } protected virtual DistributedCacheEntryOptions GetDefaultCacheEntryOptions() { @@ -148,7 +128,7 @@ namespace Volo.Abp.Caching /// Indicates to throw or hide the exceptions for the distributed cache. /// The cache item, or null. public virtual TCacheItem Get( - TCacheKey key, + TCacheKey key, bool? hideErrors = null) { hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; @@ -186,8 +166,8 @@ namespace Volo.Abp.Caching /// The for the task. /// The cache item, or null. public virtual async Task GetAsync( - TCacheKey key, - bool? hideErrors = null, + TCacheKey key, + bool? hideErrors = null, CancellationToken token = default) { hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; @@ -229,9 +209,9 @@ namespace Volo.Abp.Caching /// Indicates to throw or hide the exceptions for the distributed cache. /// The cache item. public virtual TCacheItem GetOrAdd( - TCacheKey key, - Func factory, - Func optionsFactory = null, + TCacheKey key, + Func factory, + Func optionsFactory = null, bool? hideErrors = null) { var value = Get(key, hideErrors); @@ -265,10 +245,10 @@ namespace Volo.Abp.Caching /// The for the task. /// The cache item. public virtual async Task GetOrAddAsync( - TCacheKey key, - Func> factory, - Func optionsFactory = null, - bool? hideErrors = null, + TCacheKey key, + Func> factory, + Func optionsFactory = null, + bool? hideErrors = null, CancellationToken token = default) { token = CancellationTokenProvider.FallbackToProvider(token); @@ -300,9 +280,9 @@ namespace Volo.Abp.Caching /// The cache options for the value. /// Indicates to throw or hide the exceptions for the distributed cache. public virtual void Set( - TCacheKey key, - TCacheItem value, - DistributedCacheEntryOptions options = null, + TCacheKey key, + TCacheItem value, + DistributedCacheEntryOptions options = null, bool? hideErrors = null) { hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; @@ -336,10 +316,10 @@ namespace Volo.Abp.Caching /// The for the task. /// The indicating that the operation is asynchronous. public virtual async Task SetAsync( - TCacheKey key, - TCacheItem value, - DistributedCacheEntryOptions options = null, - bool? hideErrors = null, + TCacheKey key, + TCacheItem value, + DistributedCacheEntryOptions options = null, + bool? hideErrors = null, CancellationToken token = default) { hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; @@ -370,7 +350,7 @@ namespace Volo.Abp.Caching /// The key of cached item to be retrieved from the cache. /// Indicates to throw or hide the exceptions for the distributed cache. public virtual void Refresh( - TCacheKey key, bool? + TCacheKey key, bool? hideErrors = null) { hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; @@ -398,8 +378,8 @@ namespace Volo.Abp.Caching /// The for the task. /// The indicating that the operation is asynchronous. public virtual async Task RefreshAsync( - TCacheKey key, - bool? hideErrors = null, + TCacheKey key, + bool? hideErrors = null, CancellationToken token = default) { hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; @@ -425,7 +405,7 @@ namespace Volo.Abp.Caching /// The key of cached item to be retrieved from the cache. /// Indicates to throw or hide the exceptions for the distributed cache. public virtual void Remove( - TCacheKey key, + TCacheKey key, bool? hideErrors = null) { hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; @@ -452,8 +432,8 @@ namespace Volo.Abp.Caching /// The for the task. /// The indicating that the operation is asynchronous. public virtual async Task RemoveAsync( - TCacheKey key, - bool? hideErrors = null, + TCacheKey key, + bool? hideErrors = null, CancellationToken token = default) { hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; @@ -472,8 +452,8 @@ namespace Volo.Abp.Caching throw; } - } - - } - + } + + } + } \ No newline at end of file diff --git a/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs b/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs index 93788e0995..927448463a 100644 --- a/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs +++ b/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs @@ -242,9 +242,9 @@ namespace Volo.Abp.Caching [Fact] public async Task Should_Set_Get_And_Remove_Cache_Items_With_Object_Type_CacheKey() { - var personCache = GetRequiredService>(); + var personCache = GetRequiredService>(); - var cacheKey = new DummyObjectAsCacheKey { DummyData = "DummyData", DummyInt = 42 }; + var cacheKey = new ComplexObjectAsCacheKey { Name = "DummyData", Age = 42 }; const string personName = "john nash"; //Get (not exists yet) @@ -271,10 +271,10 @@ namespace Volo.Abp.Caching [Fact] public async Task Should_Set_Get_And_Remove_Cache_Items_For_Same_Object_Type_With_Different_CacheKeys() { - var personCache = GetRequiredService>(); + var personCache = GetRequiredService>(); - var cache1Key = new DummyObjectAsCacheKey { DummyData = "DummyData", DummyInt = 42 }; - var cache2Key = new DummyObjectAsCacheKey { DummyData = "DummyData2", DummyInt = 24 }; + var cache1Key = new ComplexObjectAsCacheKey { Name = "John", Age = 42 }; + var cache2Key = new ComplexObjectAsCacheKey { Name = "Jenny", Age = 24 }; const string personName = "john nash"; //Get (not exists yet) diff --git a/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/PersonCacheItem.cs b/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/PersonCacheItem.cs index 6b3d9c36df..7ea37ffd26 100644 --- a/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/PersonCacheItem.cs +++ b/framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/PersonCacheItem.cs @@ -1,5 +1,6 @@ using System; - +using System.Linq; + namespace Volo.Abp.Caching { [Serializable] @@ -18,10 +19,29 @@ namespace Volo.Abp.Caching } } - public class DummyObjectAsCacheKey + public class ComplexObjectAsCacheKey { - public string DummyData { get; set; } - public int DummyInt { get; set; } + public string Name { get; set; } + public int Age { get; set; } + + public override string ToString() + { + // Return selective fields + //return $"{Name}_{Age}"; + // Return all the fields concatenated + var sb = new System.Text.StringBuilder(); + var properties = this.GetType().GetProperties() + .Where(prop => prop.CanRead && prop.CanWrite); + foreach (var prop in properties) + { + var value = prop.GetValue(this, null); + if (value != null) + { + sb.Append(value.ToString()); + } + } + return sb.ToString(); + } } }