Browse Source

Resolved #2181: Introduce IDistributedCacheKeyNormalizer.

pull/2196/head
Halil İbrahim Kalkan 7 years ago
parent
commit
cd50b57357
  1. 37
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs
  2. 21
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCacheKeyNormalizeArgs.cs
  3. 33
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCacheKeyNormalizer.cs
  4. 7
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCacheKeyNormalizer.cs

37
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<DistributedCache<TCacheItem, TCacheKey>>.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();
}
/// <summary>
/// Gets a cache item with the given key. If no cache item is found for the given key then returns null.
/// </summary>
@ -193,6 +195,7 @@ namespace Volo.Abp.Caching
return Serializer.Deserialize<TCacheItem>(cachedBytes);
}
/// <summary>
/// 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 <paramref name="factory" /> delegate and returns the provided cache item.
@ -228,6 +231,7 @@ namespace Volo.Abp.Caching
return value;
}
/// <summary>
/// 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 <paramref name="factory" /> delegate and returns the provided cache item.
@ -266,6 +270,7 @@ namespace Volo.Abp.Caching
return value;
}
/// <summary>
/// Sets the cache item value for the provided key.
/// </summary>
@ -300,6 +305,7 @@ namespace Volo.Abp.Caching
throw;
}
}
/// <summary>
/// Sets the cache item value for the provided key.
/// </summary>
@ -338,6 +344,7 @@ namespace Volo.Abp.Caching
throw;
}
}
/// <summary>
/// Refreshes the cache value of the given key, and resets its sliding expiration timeout.
/// </summary>
@ -393,6 +400,7 @@ namespace Volo.Abp.Caching
throw;
}
}
/// <summary>
/// Removes the cache item for given key from cache.
/// </summary>
@ -418,6 +426,7 @@ namespace Volo.Abp.Caching
throw;
}
}
/// <summary>
/// Removes the cache item for given key from cache.
/// </summary>
@ -447,7 +456,5 @@ namespace Volo.Abp.Caching
throw;
}
}
}
}

21
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;
}
}
}

33
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<AbpDistributedCacheOptions> 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;
}
}
}

7
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);
}
}
Loading…
Cancel
Save