|
|
|
@ -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,7 +166,7 @@ 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,
|
|
|
|
|
TCacheKey key,
|
|
|
|
|
bool? hideErrors = null,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
@ -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,8 +208,8 @@ 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,
|
|
|
|
|
public virtual TCacheItem GetOrAdd(
|
|
|
|
|
TCacheKey key,
|
|
|
|
|
Func<TCacheItem> factory,
|
|
|
|
|
Func<DistributedCacheEntryOptions> optionsFactory = null,
|
|
|
|
|
bool? hideErrors = null)
|
|
|
|
@ -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,8 +244,8 @@ 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,
|
|
|
|
|
public virtual async Task<TCacheItem> GetOrAddAsync(
|
|
|
|
|
TCacheKey key,
|
|
|
|
|
Func<Task<TCacheItem>> factory,
|
|
|
|
|
Func<DistributedCacheEntryOptions> optionsFactory = null,
|
|
|
|
|
bool? hideErrors = null,
|
|
|
|
@ -214,7 +272,6 @@ namespace Volo.Abp.Caching
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the cache item value for the provided key.
|
|
|
|
|
/// </summary>
|
|
|
|
@ -223,7 +280,7 @@ 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,
|
|
|
|
|
TCacheKey key,
|
|
|
|
|
TCacheItem value,
|
|
|
|
|
DistributedCacheEntryOptions options = null,
|
|
|
|
|
bool? hideErrors = null)
|
|
|
|
@ -249,7 +306,6 @@ namespace Volo.Abp.Caching
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the cache item value for the provided key.
|
|
|
|
|
/// </summary>
|
|
|
|
@ -260,7 +316,7 @@ 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,
|
|
|
|
|
TCacheKey key,
|
|
|
|
|
TCacheItem value,
|
|
|
|
|
DistributedCacheEntryOptions options = null,
|
|
|
|
|
bool? hideErrors = null,
|
|
|
|
@ -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,7 +378,7 @@ 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,
|
|
|
|
|
TCacheKey key,
|
|
|
|
|
bool? hideErrors = null,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
@ -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,7 +432,7 @@ 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,
|
|
|
|
|
TCacheKey key,
|
|
|
|
|
bool? hideErrors = null,
|
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
@ -403,40 +454,6 @@ namespace Volo.Abp.Caching
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|