Browse Source

Refactor.

pull/4523/head
maliming 6 years ago
parent
commit
a391e7a528
  1. 160
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs
  2. 58
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCache.cs
  3. 12
      framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs

160
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs

@ -53,7 +53,7 @@ namespace Volo.Abp.Caching
public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheItem, TCacheKey> public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheItem, TCacheKey>
where TCacheItem : class where TCacheItem : class
{ {
public const string DistributedCacheName = "AbpDistributedCache"; public const string UowCacheName = "AbpDistributedCache";
public ILogger<DistributedCache<TCacheItem, TCacheKey>> Logger { get; set; } public ILogger<DistributedCache<TCacheItem, TCacheKey>> Logger { get; set; }
@ -142,13 +142,13 @@ namespace Volo.Abp.Caching
/// Gets a cache item with the given key. If no cache item is found for the given key then returns null. /// Gets a cache item with the given key. If no cache item is found for the given key then returns null.
/// </summary> /// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <returns>The cache item, or null.</returns> /// <returns>The cache item, or null.</returns>
public virtual TCacheItem Get( public virtual TCacheItem Get(
TCacheKey key, TCacheKey key,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null) bool considerUow = false)
{ {
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
@ -183,8 +183,8 @@ namespace Volo.Abp.Caching
public virtual KeyValuePair<TCacheKey, TCacheItem>[] GetMany( public virtual KeyValuePair<TCacheKey, TCacheItem>[] GetMany(
IEnumerable<TCacheKey> keys, IEnumerable<TCacheKey> keys,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null) bool considerUow = false)
{ {
var keyArray = keys.ToArray(); var keyArray = keys.ToArray();
@ -193,19 +193,19 @@ namespace Volo.Abp.Caching
{ {
return GetManyFallback( return GetManyFallback(
keyArray, keyArray,
considerUow, hideErrors,
hideErrors considerUow
); );
} }
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem>>();
var notCachedKeys = new List<TCacheKey>(); var notCachedKeys = new List<TCacheKey>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem>>();
if (ShouldConsiderUow(considerUow)) if (ShouldConsiderUow(considerUow))
{ {
var cache = GetUnitOfWorkCache(); var uowCache = GetUnitOfWorkCache();
foreach (var key in keyArray) foreach (var key in keyArray)
{ {
var value = cache.GetOrDefault(key)?.GetUnRemovedValueOrNull(); var value = uowCache.GetOrDefault(key)?.GetUnRemovedValueOrNull();
if (value != null) if (value != null)
{ {
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem>(key, value)); cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem>(key, value));
@ -232,7 +232,7 @@ namespace Volo.Abp.Caching
if (hideErrors == true) if (hideErrors == true)
{ {
HandleException(ex); HandleException(ex);
return ToCacheItemsWithDefaultValues(readKeys); return ToCacheItemsWithDefaultValues(keyArray);
} }
throw; throw;
@ -243,8 +243,8 @@ namespace Volo.Abp.Caching
protected virtual KeyValuePair<TCacheKey, TCacheItem>[] GetManyFallback( protected virtual KeyValuePair<TCacheKey, TCacheItem>[] GetManyFallback(
TCacheKey[] keys, TCacheKey[] keys,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null) bool considerUow = false)
{ {
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
@ -253,7 +253,7 @@ namespace Volo.Abp.Caching
return keys return keys
.Select(key => new KeyValuePair<TCacheKey, TCacheItem>( .Select(key => new KeyValuePair<TCacheKey, TCacheItem>(
key, key,
Get(key, considerUow, hideErrors: false) Get(key, false, considerUow)
) )
).ToArray(); ).ToArray();
} }
@ -271,8 +271,8 @@ namespace Volo.Abp.Caching
public virtual async Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyAsync( public virtual async Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyAsync(
IEnumerable<TCacheKey> keys, IEnumerable<TCacheKey> keys,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default) CancellationToken token = default)
{ {
var keyArray = keys.ToArray(); var keyArray = keys.ToArray();
@ -282,20 +282,20 @@ namespace Volo.Abp.Caching
{ {
return await GetManyFallbackAsync( return await GetManyFallbackAsync(
keyArray, keyArray,
considerUow,
hideErrors, hideErrors,
considerUow,
token token
); );
} }
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem>>();
var notCachedKeys = new List<TCacheKey>(); var notCachedKeys = new List<TCacheKey>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem>>();
if (ShouldConsiderUow(considerUow)) if (ShouldConsiderUow(considerUow))
{ {
var cache = GetUnitOfWorkCache(); var uowCache = GetUnitOfWorkCache();
foreach (var key in keyArray) foreach (var key in keyArray)
{ {
var value = cache.GetOrDefault(key)?.GetUnRemovedValueOrNull(); var value = uowCache.GetOrDefault(key)?.GetUnRemovedValueOrNull();
if (value != null) if (value != null)
{ {
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem>(key, value)); cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem>(key, value));
@ -326,7 +326,7 @@ namespace Volo.Abp.Caching
if (hideErrors == true) if (hideErrors == true)
{ {
await HandleExceptionAsync(ex); await HandleExceptionAsync(ex);
return ToCacheItemsWithDefaultValues(readKeys); return ToCacheItemsWithDefaultValues(keyArray);
} }
throw; throw;
@ -337,8 +337,8 @@ namespace Volo.Abp.Caching
protected virtual async Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyFallbackAsync( protected virtual async Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyFallbackAsync(
TCacheKey[] keys, TCacheKey[] keys,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default) CancellationToken token = default)
{ {
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
@ -351,7 +351,7 @@ namespace Volo.Abp.Caching
{ {
result.Add(new KeyValuePair<TCacheKey, TCacheItem>( result.Add(new KeyValuePair<TCacheKey, TCacheItem>(
key, key,
await GetAsync(key, considerUow, hideErrors: false, token: token)) await GetAsync(key, false, considerUow, token: token))
); );
} }
@ -373,14 +373,14 @@ namespace Volo.Abp.Caching
/// Gets a cache item with the given key. If no cache item is found for the given key then returns null. /// Gets a cache item with the given key. If no cache item is found for the given key then returns null.
/// </summary> /// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param> /// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
/// <returns>The cache item, or null.</returns> /// <returns>The cache item, or null.</returns>
public virtual async Task<TCacheItem> GetAsync( public virtual async Task<TCacheItem> GetAsync(
TCacheKey key, TCacheKey key,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default) CancellationToken token = default)
{ {
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
@ -429,17 +429,17 @@ namespace Volo.Abp.Caching
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="factory">The factory delegate is used to provide the cache item when no cache item is found for the given <paramref name="key" />.</param> /// <param name="factory">The factory delegate is used to provide the cache item when no cache item is found for the given <paramref name="key" />.</param>
/// <param name="optionsFactory">The cache options for the factory delegate.</param> /// <param name="optionsFactory">The cache options for the factory delegate.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <returns>The cache item.</returns> /// <returns>The cache item.</returns>
public virtual TCacheItem GetOrAdd( public virtual TCacheItem GetOrAdd(
TCacheKey key, TCacheKey key,
Func<TCacheItem> factory, Func<TCacheItem> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null, Func<DistributedCacheEntryOptions> optionsFactory = null,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null) bool considerUow = false)
{ {
var value = Get(key, considerUow, hideErrors); var value = Get(key, hideErrors, considerUow);
if (value != null) if (value != null)
{ {
return value; return value;
@ -447,7 +447,7 @@ namespace Volo.Abp.Caching
using (SyncSemaphore.Lock()) using (SyncSemaphore.Lock())
{ {
value = Get(key, considerUow, hideErrors); value = Get(key, hideErrors, considerUow);
if (value != null) if (value != null)
{ {
return value; return value;
@ -468,7 +468,7 @@ namespace Volo.Abp.Caching
} }
} }
Set(key, value, optionsFactory?.Invoke(), considerUow, hideErrors); Set(key, value, optionsFactory?.Invoke(), hideErrors, considerUow);
} }
return value; return value;
@ -489,12 +489,12 @@ namespace Volo.Abp.Caching
TCacheKey key, TCacheKey key,
Func<Task<TCacheItem>> factory, Func<Task<TCacheItem>> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null, Func<DistributedCacheEntryOptions> optionsFactory = null,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default) CancellationToken token = default)
{ {
token = CancellationTokenProvider.FallbackToProvider(token); token = CancellationTokenProvider.FallbackToProvider(token);
var value = await GetAsync(key, considerUow, hideErrors, token); var value = await GetAsync(key, hideErrors, considerUow, token);
if (value != null) if (value != null)
{ {
return value; return value;
@ -502,7 +502,7 @@ namespace Volo.Abp.Caching
using (await SyncSemaphore.LockAsync(token)) using (await SyncSemaphore.LockAsync(token))
{ {
value = await GetAsync(key, considerUow, hideErrors, token); value = await GetAsync(key, hideErrors, considerUow, token);
if (value != null) if (value != null)
{ {
return value; return value;
@ -523,7 +523,7 @@ namespace Volo.Abp.Caching
} }
} }
await SetAsync(key, value, optionsFactory?.Invoke(), considerUow, hideErrors, token); await SetAsync(key, value, optionsFactory?.Invoke(), hideErrors, considerUow, token);
} }
return value; return value;
@ -535,14 +535,14 @@ namespace Volo.Abp.Caching
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="value">The cache item value to set in the cache.</param> /// <param name="value">The cache item value to set in the cache.</param>
/// <param name="options">The cache options for the value.</param> /// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
public virtual void Set( public virtual void Set(
TCacheKey key, TCacheKey key,
TCacheItem value, TCacheItem value,
DistributedCacheEntryOptions options = null, DistributedCacheEntryOptions options = null,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null) bool considerUow = false)
{ {
void SetRealCache() void SetRealCache()
{ {
@ -570,14 +570,14 @@ namespace Volo.Abp.Caching
if (ShouldConsiderUow(considerUow)) if (ShouldConsiderUow(considerUow))
{ {
var cache = GetUnitOfWorkCache(); var uowCache = GetUnitOfWorkCache();
if (cache.TryGetValue(key, out _)) if (uowCache.TryGetValue(key, out _))
{ {
cache[key].SetValue(value); uowCache[key].SetValue(value);
} }
else else
{ {
cache.Add(key, new UnitOfWorkCacheItem<TCacheItem>(value)); uowCache.Add(key, new UnitOfWorkCacheItem<TCacheItem>(value));
} }
// ReSharper disable once PossibleNullReferenceException // ReSharper disable once PossibleNullReferenceException
@ -598,16 +598,16 @@ namespace Volo.Abp.Caching
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="value">The cache item value to set in the cache.</param> /// <param name="value">The cache item value to set in the cache.</param>
/// <param name="options">The cache options for the value.</param> /// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param> /// <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> /// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
public virtual async Task SetAsync( public virtual async Task SetAsync(
TCacheKey key, TCacheKey key,
TCacheItem value, TCacheItem value,
DistributedCacheEntryOptions options = null, DistributedCacheEntryOptions options = null,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default) CancellationToken token = default)
{ {
async Task SetRealCache() async Task SetRealCache()
@ -637,14 +637,14 @@ namespace Volo.Abp.Caching
if (ShouldConsiderUow(considerUow)) if (ShouldConsiderUow(considerUow))
{ {
var cache = GetUnitOfWorkCache(); var uowCache = GetUnitOfWorkCache();
if (cache.TryGetValue(key, out _)) if (uowCache.TryGetValue(key, out _))
{ {
cache[key].SetValue(value); uowCache[key].SetValue(value);
} }
else else
{ {
cache.Add(key, new UnitOfWorkCacheItem<TCacheItem>(value)); uowCache.Add(key, new UnitOfWorkCacheItem<TCacheItem>(value));
} }
// ReSharper disable once PossibleNullReferenceException // ReSharper disable once PossibleNullReferenceException
@ -659,8 +659,8 @@ namespace Volo.Abp.Caching
public void SetMany( public void SetMany(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items, IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null, DistributedCacheEntryOptions options = null,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null) bool considerUow = false)
{ {
var itemsArray = items.ToArray(); var itemsArray = items.ToArray();
@ -670,8 +670,8 @@ namespace Volo.Abp.Caching
SetManyFallback( SetManyFallback(
itemsArray, itemsArray,
options, options,
considerUow, hideErrors,
hideErrors considerUow
); );
return; return;
@ -702,17 +702,17 @@ namespace Volo.Abp.Caching
if (ShouldConsiderUow(considerUow)) if (ShouldConsiderUow(considerUow))
{ {
var cache = GetUnitOfWorkCache(); var uowCache = GetUnitOfWorkCache();
foreach (var pair in itemsArray) foreach (var pair in itemsArray)
{ {
if (cache.TryGetValue(pair.Key, out _)) if (uowCache.TryGetValue(pair.Key, out _))
{ {
cache[pair.Key].SetValue(pair.Value); uowCache[pair.Key].SetValue(pair.Value);
} }
else else
{ {
cache.Add(pair.Key, new UnitOfWorkCacheItem<TCacheItem>(pair.Value)); uowCache.Add(pair.Key, new UnitOfWorkCacheItem<TCacheItem>(pair.Value));
} }
} }
@ -732,8 +732,8 @@ namespace Volo.Abp.Caching
protected virtual void SetManyFallback( protected virtual void SetManyFallback(
KeyValuePair<TCacheKey, TCacheItem>[] items, KeyValuePair<TCacheKey, TCacheItem>[] items,
DistributedCacheEntryOptions options = null, DistributedCacheEntryOptions options = null,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null) bool considerUow = false)
{ {
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
@ -745,8 +745,8 @@ namespace Volo.Abp.Caching
item.Key, item.Key,
item.Value, item.Value,
options, options,
considerUow, false,
hideErrors: false considerUow
); );
} }
} }
@ -765,8 +765,8 @@ namespace Volo.Abp.Caching
public virtual async Task SetManyAsync( public virtual async Task SetManyAsync(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items, IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null, DistributedCacheEntryOptions options = null,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default) CancellationToken token = default)
{ {
var itemsArray = items.ToArray(); var itemsArray = items.ToArray();
@ -777,8 +777,8 @@ namespace Volo.Abp.Caching
await SetManyFallbackAsync( await SetManyFallbackAsync(
itemsArray, itemsArray,
options, options,
considerUow,
hideErrors, hideErrors,
considerUow,
token token
); );
@ -811,17 +811,17 @@ namespace Volo.Abp.Caching
if (ShouldConsiderUow(considerUow)) if (ShouldConsiderUow(considerUow))
{ {
var cache = GetUnitOfWorkCache(); var uowCache = GetUnitOfWorkCache();
foreach (var pair in itemsArray) foreach (var pair in itemsArray)
{ {
if (cache.TryGetValue(pair.Key, out _)) if (uowCache.TryGetValue(pair.Key, out _))
{ {
cache[pair.Key].SetValue(pair.Value); uowCache[pair.Key].SetValue(pair.Value);
} }
else else
{ {
cache.Add(pair.Key, new UnitOfWorkCacheItem<TCacheItem>(pair.Value)); uowCache.Add(pair.Key, new UnitOfWorkCacheItem<TCacheItem>(pair.Value));
} }
} }
@ -837,8 +837,8 @@ namespace Volo.Abp.Caching
protected virtual async Task SetManyFallbackAsync( protected virtual async Task SetManyFallbackAsync(
KeyValuePair<TCacheKey, TCacheItem>[] items, KeyValuePair<TCacheKey, TCacheItem>[] items,
DistributedCacheEntryOptions options = null, DistributedCacheEntryOptions options = null,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default) CancellationToken token = default)
{ {
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors; hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
@ -851,8 +851,8 @@ namespace Volo.Abp.Caching
item.Key, item.Key,
item.Value, item.Value,
options, options,
false,
considerUow, considerUow,
hideErrors: false,
token: token token: token
); );
} }
@ -934,8 +934,8 @@ namespace Volo.Abp.Caching
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
public virtual void Remove( public virtual void Remove(
TCacheKey key, TCacheKey key,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null) bool considerUow = false)
{ {
void RemoveRealCache() void RemoveRealCache()
{ {
@ -959,10 +959,10 @@ namespace Volo.Abp.Caching
if (ShouldConsiderUow(considerUow)) if (ShouldConsiderUow(considerUow))
{ {
var cache = GetUnitOfWorkCache(); var uowCache = GetUnitOfWorkCache();
if (cache.TryGetValue(key, out _)) if (uowCache.TryGetValue(key, out _))
{ {
cache[key].RemoveValue(); uowCache[key].RemoveValue();
} }
// ReSharper disable once PossibleNullReferenceException // ReSharper disable once PossibleNullReferenceException
@ -982,14 +982,14 @@ namespace Volo.Abp.Caching
/// Removes the cache item for given key from cache. /// Removes the cache item for given key from cache.
/// </summary> /// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param> /// <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> /// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
public virtual async Task RemoveAsync( public virtual async Task RemoveAsync(
TCacheKey key, TCacheKey key,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default) CancellationToken token = default)
{ {
async Task RemoveRealCache() async Task RemoveRealCache()
@ -1014,10 +1014,10 @@ namespace Volo.Abp.Caching
if (ShouldConsiderUow(considerUow)) if (ShouldConsiderUow(considerUow))
{ {
var cache = GetUnitOfWorkCache(); var uowCache = GetUnitOfWorkCache();
if (cache.TryGetValue(key, out _)) if (uowCache.TryGetValue(key, out _))
{ {
cache[key].RemoveValue(); uowCache[key].RemoveValue();
} }
// ReSharper disable once PossibleNullReferenceException // ReSharper disable once PossibleNullReferenceException
@ -1104,14 +1104,14 @@ namespace Volo.Abp.Caching
protected virtual string GetUnitOfWorkCacheKey() protected virtual string GetUnitOfWorkCacheKey()
{ {
return DistributedCacheName + CacheName; return UowCacheName + CacheName;
} }
protected virtual Dictionary<TCacheKey, UnitOfWorkCacheItem<TCacheItem>> GetUnitOfWorkCache() protected virtual Dictionary<TCacheKey, UnitOfWorkCacheItem<TCacheItem>> GetUnitOfWorkCache()
{ {
if (UnitOfWorkManager.Current == null) if (UnitOfWorkManager.Current == null)
{ {
throw new AbpException($"There is no unit of work in the current context, The {GetType().Name} can only be used in a unit of work."); throw new AbpException($"There is no active UOW.");
} }
return UnitOfWorkManager.Current.GetOrAddItem(GetUnitOfWorkCacheKey(), return UnitOfWorkManager.Current.GetOrAddItem(GetUnitOfWorkCacheKey(),

58
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCache.cs

@ -29,13 +29,13 @@ namespace Volo.Abp.Caching
/// Gets a cache item with the given key. If no cache item is found for the given key then returns null. /// Gets a cache item with the given key. If no cache item is found for the given key then returns null.
/// </summary> /// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <returns>The cache item, or null.</returns> /// <returns>The cache item, or null.</returns>
TCacheItem Get( TCacheItem Get(
TCacheKey key, TCacheKey key,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null bool considerUow = false
); );
/// <summary> /// <summary>
@ -46,13 +46,13 @@ namespace Volo.Abp.Caching
/// if the related key not found in the cache. /// if the related key not found in the cache.
/// </summary> /// </summary>
/// <param name="keys">The keys of cached items to be retrieved from the cache.</param> /// <param name="keys">The keys of cached items to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <returns>List of cache items.</returns> /// <returns>List of cache items.</returns>
KeyValuePair<TCacheKey, TCacheItem>[] GetMany( KeyValuePair<TCacheKey, TCacheItem>[] GetMany(
IEnumerable<TCacheKey> keys, IEnumerable<TCacheKey> keys,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null bool considerUow = false
); );
/// <summary> /// <summary>
@ -64,14 +64,14 @@ namespace Volo.Abp.Caching
/// ///
/// </summary> /// </summary>
/// <param name="keys">The keys of cached items to be retrieved from the cache.</param> /// <param name="keys">The keys of cached items to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// /// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param> /// /// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
/// <returns>List of cache items.</returns> /// <returns>List of cache items.</returns>
Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyAsync( Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyAsync(
IEnumerable<TCacheKey> keys, IEnumerable<TCacheKey> keys,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default CancellationToken token = default
); );
@ -85,8 +85,8 @@ namespace Volo.Abp.Caching
/// <returns>The cache item, or null.</returns> /// <returns>The cache item, or null.</returns>
Task<TCacheItem> GetAsync( Task<TCacheItem> GetAsync(
[NotNull] TCacheKey key, [NotNull] TCacheKey key,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default CancellationToken token = default
); );
@ -97,15 +97,15 @@ namespace Volo.Abp.Caching
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="factory">The factory delegate is used to provide the cache item when no cache item is found for the given <paramref name="key" />.</param> /// <param name="factory">The factory delegate is used to provide the cache item when no cache item is found for the given <paramref name="key" />.</param>
/// <param name="optionsFactory">The cache options for the factory delegate.</param> /// <param name="optionsFactory">The cache options for the factory delegate.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <returns>The cache item.</returns> /// <returns>The cache item.</returns>
TCacheItem GetOrAdd( TCacheItem GetOrAdd(
TCacheKey key, TCacheKey key,
Func<TCacheItem> factory, Func<TCacheItem> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null, Func<DistributedCacheEntryOptions> optionsFactory = null,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null bool considerUow = false
); );
/// <summary> /// <summary>
@ -115,16 +115,16 @@ namespace Volo.Abp.Caching
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="factory">The factory delegate is used to provide the cache item when no cache item is found for the given <paramref name="key" />.</param> /// <param name="factory">The factory delegate is used to provide the cache item when no cache item is found for the given <paramref name="key" />.</param>
/// <param name="optionsFactory">The cache options for the factory delegate.</param> /// <param name="optionsFactory">The cache options for the factory delegate.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param> /// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
/// <returns>The cache item.</returns> /// <returns>The cache item.</returns>
Task<TCacheItem> GetOrAddAsync( Task<TCacheItem> GetOrAddAsync(
[NotNull] TCacheKey key, [NotNull] TCacheKey key,
Func<Task<TCacheItem>> factory, Func<Task<TCacheItem>> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null, Func<DistributedCacheEntryOptions> optionsFactory = null,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default CancellationToken token = default
); );
@ -134,14 +134,14 @@ namespace Volo.Abp.Caching
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="value">The cache item value to set in the cache.</param> /// <param name="value">The cache item value to set in the cache.</param>
/// <param name="options">The cache options for the value.</param> /// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
void Set( void Set(
TCacheKey key, TCacheKey key,
TCacheItem value, TCacheItem value,
DistributedCacheEntryOptions options = null, DistributedCacheEntryOptions options = null,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null bool considerUow = false
); );
/// <summary> /// <summary>
@ -150,16 +150,16 @@ namespace Volo.Abp.Caching
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="value">The cache item value to set in the cache.</param> /// <param name="value">The cache item value to set in the cache.</param>
/// <param name="options">The cache options for the value.</param> /// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param> /// <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> /// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
Task SetAsync( Task SetAsync(
[NotNull] TCacheKey key, [NotNull] TCacheKey key,
[NotNull] TCacheItem value, [NotNull] TCacheItem value,
[CanBeNull] DistributedCacheEntryOptions options = null, [CanBeNull] DistributedCacheEntryOptions options = null,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default CancellationToken token = default
); );
@ -169,13 +169,13 @@ namespace Volo.Abp.Caching
/// </summary> /// </summary>
/// <param name="items">Items to set on the cache</param> /// <param name="items">Items to set on the cache</param>
/// <param name="options">The cache options for the value.</param> /// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
void SetMany( void SetMany(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items, IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null, DistributedCacheEntryOptions options = null,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null bool considerUow = false
); );
/// <summary> /// <summary>
@ -184,15 +184,15 @@ namespace Volo.Abp.Caching
/// </summary> /// </summary>
/// <param name="items">Items to set on the cache</param> /// <param name="items">Items to set on the cache</param>
/// <param name="options">The cache options for the value.</param> /// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param> /// <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> /// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
Task SetManyAsync( Task SetManyAsync(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items, IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null, DistributedCacheEntryOptions options = null,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default CancellationToken token = default
); );
@ -223,26 +223,26 @@ namespace Volo.Abp.Caching
/// Removes the cache item for given key from cache. /// Removes the cache item for given key from cache.
/// </summary> /// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
void Remove( void Remove(
TCacheKey key, TCacheKey key,
bool considerUow = false, bool? hideErrors = null,
bool? hideErrors = null bool considerUow = false
); );
/// <summary> /// <summary>
/// Removes the cache item for given key from cache. /// Removes the cache item for given key from cache.
/// </summary> /// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param> /// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param> /// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param> /// <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> /// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
Task RemoveAsync( Task RemoveAsync(
TCacheKey key, TCacheKey key,
bool considerUow = false,
bool? hideErrors = null, bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default CancellationToken token = default
); );
} }

12
framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_Tests.cs

@ -181,10 +181,10 @@ namespace Volo.Abp.Caching
factoryExecuted = false; factoryExecuted = false;
cacheItem = await personCache.GetOrAddAsync(cacheKey, cacheItem = await personCache.GetOrAddAsync(cacheKey,
async () => () =>
{ {
factoryExecuted = true; factoryExecuted = true;
return new PersonCacheItem(personName); return Task.FromResult(new PersonCacheItem(personName));
}); });
factoryExecuted.ShouldBeFalse(); factoryExecuted.ShouldBeFalse();
@ -539,7 +539,7 @@ namespace Volo.Abp.Caching
cacheValue = await personCache.GetAsync(key, considerUow: false); cacheValue = await personCache.GetAsync(key, considerUow: false);
cacheValue.ShouldBeNull(); cacheValue.ShouldBeNull();
} }
[Fact] [Fact]
public async Task Should_Set_And_Get_Multiple_Items_Async() public async Task Should_Set_And_Get_Multiple_Items_Async()
{ {
@ -547,7 +547,7 @@ namespace Volo.Abp.Caching
await personCache.SetManyAsync(new[] await personCache.SetManyAsync(new[]
{ {
new KeyValuePair<string, PersonCacheItem>("john", new PersonCacheItem("John Nash")), new KeyValuePair<string, PersonCacheItem>("john", new PersonCacheItem("John Nash")),
new KeyValuePair<string, PersonCacheItem>("thomas", new PersonCacheItem("Thomas Moore")) new KeyValuePair<string, PersonCacheItem>("thomas", new PersonCacheItem("Thomas Moore"))
}); });
@ -557,7 +557,7 @@ namespace Volo.Abp.Caching
"thomas", "thomas",
"baris" //doesn't exist "baris" //doesn't exist
}); });
cacheItems.Length.ShouldBe(3); cacheItems.Length.ShouldBe(3);
cacheItems[0].Key.ShouldBe("john"); cacheItems[0].Key.ShouldBe("john");
cacheItems[0].Value.Name.ShouldBe("John Nash"); cacheItems[0].Value.Name.ShouldBe("John Nash");
@ -565,7 +565,7 @@ namespace Volo.Abp.Caching
cacheItems[1].Value.Name.ShouldBe("Thomas Moore"); cacheItems[1].Value.Name.ShouldBe("Thomas Moore");
cacheItems[2].Key.ShouldBe("baris"); cacheItems[2].Key.ShouldBe("baris");
cacheItems[2].Value.ShouldBeNull(); cacheItems[2].Value.ShouldBeNull();
(await personCache.GetAsync("john")).Name.ShouldBe("John Nash"); (await personCache.GetAsync("john")).Name.ShouldBe("John Nash");
(await personCache.GetAsync("baris")).ShouldBeNull(); (await personCache.GetAsync("baris")).ShouldBeNull();
} }

Loading…
Cancel
Save