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 4140cceeb3..dbcfb184eb 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs @@ -23,12 +23,12 @@ namespace Volo.Abp.Caching IDistributedCache cache, ICancellationTokenProvider cancellationTokenProvider, IDistributedCacheSerializer serializer, - ICurrentTenant currentTenant) : base( + IDistributedCacheKeyNormalizer keyNormalizer) : base( distributedCacheOption: distributedCacheOption, cache: cache, cancellationTokenProvider: cancellationTokenProvider, serializer: serializer, - currentTenant: currentTenant) + keyNormalizer: keyNormalizer) { } @@ -54,7 +54,7 @@ namespace Volo.Abp.Caching protected IDistributedCacheSerializer Serializer { get; } - protected ICurrentTenant CurrentTenant { get; } + protected IDistributedCacheKeyNormalizer KeyNormalizer { get; } protected SemaphoreSlim SyncSemaphore { get; } @@ -67,29 +67,29 @@ namespace Volo.Abp.Caching IDistributedCache cache, ICancellationTokenProvider cancellationTokenProvider, IDistributedCacheSerializer serializer, - ICurrentTenant currentTenant) + IDistributedCacheKeyNormalizer keyNormalizer) { _distributedCacheOption = distributedCacheOption.Value; Cache = cache; CancellationTokenProvider = cancellationTokenProvider; Logger = NullLogger>.Instance; Serializer = serializer; - CurrentTenant = currentTenant; + KeyNormalizer = keyNormalizer; SyncSemaphore = new SemaphoreSlim(1, 1); SetDefaultOptions(); } + protected virtual string NormalizeKey(TCacheKey key) { - var normalizedKey = "c:" + CacheName + ",k:" + _distributedCacheOption.KeyPrefix + key.ToString(); - - if (!IgnoreMultiTenancy && CurrentTenant.Id.HasValue) - { - normalizedKey = "t:" + CurrentTenant.Id.Value + "," + normalizedKey; - } - - return normalizedKey; + return KeyNormalizer.NormalizeKey( + new DistributedCacheKeyNormalizeArgs( + key.ToString(), + CacheName, + IgnoreMultiTenancy + ) + ); } protected virtual DistributedCacheEntryOptions GetDefaultCacheEntryOptions() @@ -102,6 +102,7 @@ namespace Volo.Abp.Caching return options; } } + return _distributedCacheOption.GlobalCacheEntryOptions; } @@ -115,6 +116,7 @@ namespace Volo.Abp.Caching //Configure default cache entry options DefaultCacheOptions = GetDefaultCacheEntryOptions(); } + /// /// Gets a cache item with the given key. If no cache item is found for the given key then returns null. /// @@ -193,6 +195,7 @@ namespace Volo.Abp.Caching return Serializer.Deserialize(cachedBytes); } + /// /// Gets or Adds a cache item with the given key. If no cache item is found for the given key then adds a cache item /// provided by delegate and returns the provided cache item. @@ -228,6 +231,7 @@ namespace Volo.Abp.Caching return value; } + /// /// Gets or Adds a cache item with the given key. If no cache item is found for the given key then adds a cache item /// provided by delegate and returns the provided cache item. @@ -266,6 +270,7 @@ namespace Volo.Abp.Caching return value; } + /// /// Sets the cache item value for the provided key. /// @@ -300,6 +305,7 @@ namespace Volo.Abp.Caching throw; } } + /// /// Sets the cache item value for the provided key. /// @@ -338,6 +344,7 @@ namespace Volo.Abp.Caching throw; } } + /// /// Refreshes the cache value of the given key, and resets its sliding expiration timeout. /// @@ -393,6 +400,7 @@ namespace Volo.Abp.Caching throw; } } + /// /// Removes the cache item for given key from cache. /// @@ -418,6 +426,7 @@ namespace Volo.Abp.Caching throw; } } + /// /// Removes the cache item for given key from cache. /// @@ -447,7 +456,5 @@ namespace Volo.Abp.Caching throw; } } - } - } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCacheKeyNormalizeArgs.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCacheKeyNormalizeArgs.cs new file mode 100644 index 0000000000..791a1630eb --- /dev/null +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCacheKeyNormalizeArgs.cs @@ -0,0 +1,21 @@ +namespace Volo.Abp.Caching +{ + public class DistributedCacheKeyNormalizeArgs + { + public string Key { get; } + + public string CacheName { get; } + + public bool IgnoreMultiTenancy { get; } + + public DistributedCacheKeyNormalizeArgs( + string key, + string cacheName, + bool ignoreMultiTenancy) + { + Key = key; + CacheName = cacheName; + IgnoreMultiTenancy = ignoreMultiTenancy; + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCacheKeyNormalizer.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCacheKeyNormalizer.cs new file mode 100644 index 0000000000..aca22705dd --- /dev/null +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCacheKeyNormalizer.cs @@ -0,0 +1,33 @@ +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; +using Volo.Abp.MultiTenancy; + +namespace Volo.Abp.Caching +{ + public class DistributedCacheKeyNormalizer : IDistributedCacheKeyNormalizer, ITransientDependency + { + protected ICurrentTenant CurrentTenant { get; } + + protected AbpDistributedCacheOptions DistributedCacheOptions { get; } + + public DistributedCacheKeyNormalizer( + ICurrentTenant currentTenant, + IOptions distributedCacheOptions) + { + CurrentTenant = currentTenant; + DistributedCacheOptions = distributedCacheOptions.Value; + } + + public virtual string NormalizeKey(DistributedCacheKeyNormalizeArgs args) + { + var normalizedKey = $"c:{args.CacheName},k:{DistributedCacheOptions.KeyPrefix}{args.Key}"; + + if (!args.IgnoreMultiTenancy && CurrentTenant.Id.HasValue) + { + normalizedKey = $"t:{CurrentTenant.Id.Value},{normalizedKey}"; + } + + return normalizedKey; + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCacheKeyNormalizer.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCacheKeyNormalizer.cs new file mode 100644 index 0000000000..5d4499a325 --- /dev/null +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCacheKeyNormalizer.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.Caching +{ + public interface IDistributedCacheKeyNormalizer + { + string NormalizeKey(DistributedCacheKeyNormalizeArgs args); + } +}