|
|
|
@ -15,10 +15,36 @@ namespace Volo.Abp.Caching |
|
|
|
/// Represents a distributed cache of <typeparamref name="TCacheItem" /> type.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TCacheItem">The type of cache item being cached.</typeparam>
|
|
|
|
public class DistributedCache<TCacheItem> : IDistributedCache<TCacheItem> |
|
|
|
public class DistributedCache<TCacheItem> : DistributedCache<TCacheItem, string>, IDistributedCache<TCacheItem> |
|
|
|
where TCacheItem : class |
|
|
|
{ |
|
|
|
public ILogger<DistributedCache<TCacheItem>> Logger { get; set; } |
|
|
|
public DistributedCache( |
|
|
|
IOptions<CacheOptions> cacheOption, |
|
|
|
IOptions<DistributedCacheOptions> distributedCacheOption, |
|
|
|
IDistributedCache cache, |
|
|
|
ICancellationTokenProvider cancellationTokenProvider, |
|
|
|
IDistributedCacheSerializer serializer, |
|
|
|
ICurrentTenant currentTenant) : base( |
|
|
|
cacheOption: cacheOption, |
|
|
|
distributedCacheOption: distributedCacheOption, |
|
|
|
cache: cache, |
|
|
|
cancellationTokenProvider: cancellationTokenProvider, |
|
|
|
serializer: serializer, |
|
|
|
currentTenant: currentTenant) |
|
|
|
{ |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
/// <summary>
|
|
|
|
/// Represents a distributed cache of <typeparamref name="TCacheItem" /> type.
|
|
|
|
/// Uses a generic cache key type of <typeparamref name="TCacheKey" /> type.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TCacheItem">The type of cache item being cached.</typeparam>
|
|
|
|
/// <typeparam name="TCacheKey">The type of cache key being used.</typeparam>
|
|
|
|
public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheItem, TCacheKey> |
|
|
|
where TCacheItem : class |
|
|
|
{ |
|
|
|
public ILogger<DistributedCache<TCacheItem, TCacheKey>> Logger { get; set; } |
|
|
|
|
|
|
|
protected string CacheName { get; set; } |
|
|
|
|
|
|
|
@ -52,7 +78,7 @@ namespace Volo.Abp.Caching |
|
|
|
_cacheOption = cacheOption.Value; |
|
|
|
Cache = cache; |
|
|
|
CancellationTokenProvider = cancellationTokenProvider; |
|
|
|
Logger = NullLogger<DistributedCache<TCacheItem>>.Instance; |
|
|
|
Logger = NullLogger<DistributedCache<TCacheItem, TCacheKey>>.Instance; |
|
|
|
Serializer = serializer; |
|
|
|
CurrentTenant = currentTenant; |
|
|
|
|
|
|
|
@ -60,7 +86,41 @@ namespace Volo.Abp.Caching |
|
|
|
|
|
|
|
SetDefaultOptions(); |
|
|
|
} |
|
|
|
protected virtual string NormalizeKey(TCacheKey key) |
|
|
|
{ |
|
|
|
var normalizedKey = "c:" + CacheName + ",k:" + _cacheOption.KeyPrefix + key.ToString(); |
|
|
|
|
|
|
|
if (!IgnoreMultiTenancy && CurrentTenant.Id.HasValue) |
|
|
|
{ |
|
|
|
normalizedKey = "t:" + CurrentTenant.Id.Value + "," + normalizedKey; |
|
|
|
} |
|
|
|
|
|
|
|
return normalizedKey; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual DistributedCacheEntryOptions GetDefaultCacheEntryOptions() |
|
|
|
{ |
|
|
|
foreach (var configure in _cacheOption.CacheConfigurators) |
|
|
|
{ |
|
|
|
var options = configure.Invoke(CacheName); |
|
|
|
if (options != null) |
|
|
|
{ |
|
|
|
return options; |
|
|
|
} |
|
|
|
} |
|
|
|
return _cacheOption.GlobalCacheEntryOptions; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual void SetDefaultOptions() |
|
|
|
{ |
|
|
|
CacheName = CacheNameAttribute.GetCacheName(typeof(TCacheItem)); |
|
|
|
|
|
|
|
//IgnoreMultiTenancy
|
|
|
|
IgnoreMultiTenancy = typeof(TCacheItem).IsDefined(typeof(IgnoreMultiTenancyAttribute), true); |
|
|
|
|
|
|
|
//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>
|
|
|
|
@ -68,7 +128,7 @@ namespace Volo.Abp.Caching |
|
|
|
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
|
|
|
|
/// <returns>The cache item, or null.</returns>
|
|
|
|
public virtual TCacheItem Get( |
|
|
|
string key, |
|
|
|
TCacheKey key, |
|
|
|
bool? hideErrors = null) |
|
|
|
{ |
|
|
|
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; |
|
|
|
@ -106,8 +166,8 @@ namespace Volo.Abp.Caching |
|
|
|
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
|
|
|
|
/// <returns>The cache item, or null.</returns>
|
|
|
|
public virtual async Task<TCacheItem> GetAsync( |
|
|
|
string key, |
|
|
|
bool? hideErrors = null, |
|
|
|
TCacheKey key, |
|
|
|
bool? hideErrors = null, |
|
|
|
CancellationToken token = default) |
|
|
|
{ |
|
|
|
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; |
|
|
|
@ -131,7 +191,7 @@ namespace Volo.Abp.Caching |
|
|
|
|
|
|
|
throw; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (cachedBytes == null) |
|
|
|
{ |
|
|
|
return null; |
|
|
|
@ -139,7 +199,6 @@ 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.
|
|
|
|
@ -149,10 +208,10 @@ namespace Volo.Abp.Caching |
|
|
|
/// <param name="optionsFactory">The cache options for the factory delegate.</param>
|
|
|
|
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
|
|
|
|
/// <returns>The cache item.</returns>
|
|
|
|
public TCacheItem GetOrAdd( |
|
|
|
string key, |
|
|
|
Func<TCacheItem> factory, |
|
|
|
Func<DistributedCacheEntryOptions> optionsFactory = null, |
|
|
|
public virtual TCacheItem GetOrAdd( |
|
|
|
TCacheKey key, |
|
|
|
Func<TCacheItem> factory, |
|
|
|
Func<DistributedCacheEntryOptions> optionsFactory = null, |
|
|
|
bool? hideErrors = null) |
|
|
|
{ |
|
|
|
var value = Get(key, hideErrors); |
|
|
|
@ -175,7 +234,6 @@ 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.
|
|
|
|
@ -186,11 +244,11 @@ namespace Volo.Abp.Caching |
|
|
|
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
|
|
|
|
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
|
|
|
|
/// <returns>The cache item.</returns>
|
|
|
|
public async Task<TCacheItem> GetOrAddAsync( |
|
|
|
string key, |
|
|
|
Func<Task<TCacheItem>> factory, |
|
|
|
Func<DistributedCacheEntryOptions> optionsFactory = null, |
|
|
|
bool? hideErrors = null, |
|
|
|
public virtual async Task<TCacheItem> GetOrAddAsync( |
|
|
|
TCacheKey key, |
|
|
|
Func<Task<TCacheItem>> factory, |
|
|
|
Func<DistributedCacheEntryOptions> optionsFactory = null, |
|
|
|
bool? hideErrors = null, |
|
|
|
CancellationToken token = default) |
|
|
|
{ |
|
|
|
token = CancellationTokenProvider.FallbackToProvider(token); |
|
|
|
@ -214,7 +272,6 @@ namespace Volo.Abp.Caching |
|
|
|
|
|
|
|
return value; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the cache item value for the provided key.
|
|
|
|
/// </summary>
|
|
|
|
@ -223,9 +280,9 @@ namespace Volo.Abp.Caching |
|
|
|
/// <param name="options">The cache options for the value.</param>
|
|
|
|
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
|
|
|
|
public virtual void Set( |
|
|
|
string key, |
|
|
|
TCacheItem value, |
|
|
|
DistributedCacheEntryOptions options = null, |
|
|
|
TCacheKey key, |
|
|
|
TCacheItem value, |
|
|
|
DistributedCacheEntryOptions options = null, |
|
|
|
bool? hideErrors = null) |
|
|
|
{ |
|
|
|
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; |
|
|
|
@ -249,7 +306,6 @@ namespace Volo.Abp.Caching |
|
|
|
throw; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets the cache item value for the provided key.
|
|
|
|
/// </summary>
|
|
|
|
@ -260,10 +316,10 @@ namespace Volo.Abp.Caching |
|
|
|
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
|
|
|
|
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
|
|
|
|
public virtual async Task SetAsync( |
|
|
|
string 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; |
|
|
|
@ -288,15 +344,14 @@ namespace Volo.Abp.Caching |
|
|
|
throw; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Refreshes the cache value of the given key, and resets its sliding expiration timeout.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
|
|
|
|
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
|
|
|
|
public virtual void Refresh( |
|
|
|
string key, |
|
|
|
bool? hideErrors = null) |
|
|
|
TCacheKey key, bool? |
|
|
|
hideErrors = null) |
|
|
|
{ |
|
|
|
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; |
|
|
|
|
|
|
|
@ -315,7 +370,6 @@ namespace Volo.Abp.Caching |
|
|
|
throw; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Refreshes the cache value of the given key, and resets its sliding expiration timeout.
|
|
|
|
/// </summary>
|
|
|
|
@ -324,8 +378,8 @@ namespace Volo.Abp.Caching |
|
|
|
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
|
|
|
|
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
|
|
|
|
public virtual async Task RefreshAsync( |
|
|
|
string key, |
|
|
|
bool? hideErrors = null, |
|
|
|
TCacheKey key, |
|
|
|
bool? hideErrors = null, |
|
|
|
CancellationToken token = default) |
|
|
|
{ |
|
|
|
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; |
|
|
|
@ -345,15 +399,13 @@ namespace Volo.Abp.Caching |
|
|
|
throw; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes the cache item for given key from cache.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
|
|
|
|
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
|
|
|
|
public virtual void Remove( |
|
|
|
string key, |
|
|
|
TCacheKey key, |
|
|
|
bool? hideErrors = null) |
|
|
|
{ |
|
|
|
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; |
|
|
|
@ -372,7 +424,6 @@ namespace Volo.Abp.Caching |
|
|
|
throw; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes the cache item for given key from cache.
|
|
|
|
/// </summary>
|
|
|
|
@ -381,8 +432,8 @@ namespace Volo.Abp.Caching |
|
|
|
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
|
|
|
|
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
|
|
|
|
public virtual async Task RemoveAsync( |
|
|
|
string key, |
|
|
|
bool? hideErrors = null, |
|
|
|
TCacheKey key, |
|
|
|
bool? hideErrors = null, |
|
|
|
CancellationToken token = default) |
|
|
|
{ |
|
|
|
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; |
|
|
|
@ -401,42 +452,8 @@ namespace Volo.Abp.Caching |
|
|
|
|
|
|
|
throw; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual string NormalizeKey(string key) |
|
|
|
{ |
|
|
|
var normalizedKey = "c:" + CacheName + ",k:" + _cacheOption.KeyPrefix + key; |
|
|
|
|
|
|
|
if (!IgnoreMultiTenancy && CurrentTenant.Id.HasValue) |
|
|
|
{ |
|
|
|
normalizedKey = "t:" + CurrentTenant.Id.Value + "," + normalizedKey; |
|
|
|
} |
|
|
|
|
|
|
|
return normalizedKey; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual DistributedCacheEntryOptions GetDefaultCacheEntryOptions() |
|
|
|
{ |
|
|
|
foreach (var configure in _cacheOption.CacheConfigurators) |
|
|
|
{ |
|
|
|
var options = configure.Invoke(CacheName); |
|
|
|
if (options != null) |
|
|
|
{ |
|
|
|
return options; |
|
|
|
} |
|
|
|
} |
|
|
|
return _cacheOption.GlobalCacheEntryOptions; |
|
|
|
} |
|
|
|
|
|
|
|
protected virtual void SetDefaultOptions() |
|
|
|
{ |
|
|
|
CacheName = CacheNameAttribute.GetCacheName(typeof(TCacheItem)); |
|
|
|
|
|
|
|
//IgnoreMultiTenancy
|
|
|
|
IgnoreMultiTenancy = typeof(TCacheItem).IsDefined(typeof(IgnoreMultiTenancyAttribute), true); |
|
|
|
|
|
|
|
//Configure default cache entry options
|
|
|
|
DefaultCacheOptions = GetDefaultCacheEntryOptions(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |