Browse Source

Add RemoveMany method

pull/7523/head
liangshiwei 6 years ago
parent
commit
eb4321880f
  1. 19
      framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs
  2. 126
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs
  3. 12
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/ICacheSupportsMultipleItems.cs
  4. 27
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCache.cs
  5. 16
      framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/TestMemoryDistributedCache.cs

19
framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs

@ -148,6 +148,25 @@ namespace Volo.Abp.Caching.StackExchangeRedis
await GetAndRefreshManyAsync(keys, false, token);
}
public void RemoveMany(IEnumerable<string> keys)
{
keys = Check.NotNull(keys, nameof(keys));
Connect();
RedisDatabase.KeyDelete(keys.Select(key => (RedisKey)(Instance + key)).ToArray());
}
public async Task RemoveManyAsync(IEnumerable<string> keys, CancellationToken token = default)
{
keys = Check.NotNull(keys, nameof(keys));
token.ThrowIfCancellationRequested();
await ConnectAsync(token);
await RedisDatabase.KeyDeleteAsync(keys.Select(key => (RedisKey)(Instance + key)).ToArray());
}
protected virtual byte[][] GetAndRefreshMany(
IEnumerable<string> keys,
bool getData)

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

@ -1284,28 +1284,128 @@ namespace Volo.Abp.Caching
}
}
protected virtual KeyValuePair<TCacheKey, TCacheItem>[] GetOrAddMany(
TCacheKey[] keys,
KeyValuePair<TCacheKey, TCacheItem>[] values,
KeyValuePair<TCacheKey, TCacheItem>[] missingValues)
public void RemoveMany(
IEnumerable<TCacheKey> keys,
bool? hideErrors = null,
bool considerUow = false)
{
var missingValuesIndex = new List<int>();
for (var i = 0; i < keys.Length; i++)
var keyArray = keys.ToArray();
if (Cache is ICacheSupportsMultipleItems cacheSupportsMultipleItems)
{
if (values[i].Value == null)
void RemoveRealCache()
{
missingValuesIndex.Add(i);
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
try
{
cacheSupportsMultipleItems.RemoveMany(
keyArray.Select(NormalizeKey)
);
}
catch (Exception ex)
{
if (hideErrors == true)
{
HandleException(ex);
return;
}
throw;
}
}
}
var valueQueue = new Queue<KeyValuePair<TCacheKey, TCacheItem>>(missingValues);
if (ShouldConsiderUow(considerUow))
{
var uowCache = GetUnitOfWorkCache();
foreach (var index in missingValuesIndex)
foreach (var key in keyArray)
{
if (uowCache.TryGetValue(key, out _))
{
uowCache[key].RemoveValue();
}
}
// ReSharper disable once PossibleNullReferenceException
UnitOfWorkManager.Current.OnCompleted(() =>
{
RemoveRealCache();
return Task.CompletedTask;
});
}
else
{
RemoveRealCache();
}
}
else
{
values[index] = valueQueue.Dequeue();
foreach (var key in keyArray)
{
Remove(key, hideErrors, considerUow);
}
}
}
return values;
public async Task RemoveManyAsync(
IEnumerable<TCacheKey> keys,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default)
{
var keyArray = keys.ToArray();
if (Cache is ICacheSupportsMultipleItems cacheSupportsMultipleItems)
{
async Task RemoveRealCache()
{
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
try
{
await cacheSupportsMultipleItems.RemoveManyAsync(
keyArray.Select(NormalizeKey), token);
}
catch (Exception ex)
{
if (hideErrors == true)
{
await HandleExceptionAsync(ex);
return;
}
throw;
}
}
if (ShouldConsiderUow(considerUow))
{
var uowCache = GetUnitOfWorkCache();
foreach (var key in keyArray)
{
if (uowCache.TryGetValue(key, out _))
{
uowCache[key].RemoveValue();
}
}
// ReSharper disable once PossibleNullReferenceException
UnitOfWorkManager.Current.OnCompleted(RemoveRealCache);
}
else
{
await RemoveRealCache();
}
}
else
{
foreach (var key in keyArray)
{
await RemoveAsync(key, hideErrors, considerUow, token);
}
}
}
protected virtual void HandleException(Exception ex)

12
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/ICacheSupportsMultipleItems.cs

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Distributed;
@ -36,5 +35,14 @@ namespace Volo.Abp.Caching
IEnumerable<string> keys,
CancellationToken token = default
);
void RemoveMany(
IEnumerable<string> keys
);
Task RemoveManyAsync(
IEnumerable<string> keys,
CancellationToken token = default
);
}
}

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

@ -306,5 +306,32 @@ namespace Volo.Abp.Caching
bool considerUow = false,
CancellationToken token = default
);
/// <summary>
/// Removes the cache items for given keys from cache.
/// </summary>
/// <param name="keys">The keys of cached items to be retrieved from the 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 RemoveMany(
IEnumerable<TCacheKey> keys,
bool? hideErrors = null,
bool considerUow = false
);
/// <summary>
/// Removes the cache items for given keys from cache.
/// </summary>
/// <param name="keys">The keys of cached items to be retrieved from the 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>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
Task RemoveManyAsync(
IEnumerable<TCacheKey> keys,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default
);
}
}

16
framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/TestMemoryDistributedCache.cs

@ -73,5 +73,21 @@ namespace Volo.Abp.Caching
await RefreshAsync(key, token);
}
}
public void RemoveMany(IEnumerable<string> keys)
{
foreach (var key in keys)
{
Remove(key);
}
}
public async Task RemoveManyAsync(IEnumerable<string> keys, CancellationToken token = default)
{
foreach (var key in keys)
{
await RemoveAsync(key, token);
}
}
}
}

Loading…
Cancel
Save