Browse Source

Resolved #13322: DistributedCache<TCacheItem> should be an adapter instead of inheriting from DistributedCache<TCacheItem, string>

pull/13845/head
Halil İbrahim Kalkan 4 years ago
parent
commit
f9892c00b4
  1. 121
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs
  2. 1
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCache.cs
  3. 14
      framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_ConfigureOptions_Test.cs

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

@ -21,26 +21,115 @@ namespace Volo.Abp.Caching;
/// </summary>
/// <typeparam name="TCacheItem">The type of cache item being cached.</typeparam>
public class DistributedCache<TCacheItem> :
DistributedCache<TCacheItem, string>,
IDistributedCache<TCacheItem>
where TCacheItem : class
{
public DistributedCache(
IOptions<AbpDistributedCacheOptions> distributedCacheOption,
IDistributedCache cache,
ICancellationTokenProvider cancellationTokenProvider,
IDistributedCacheSerializer serializer,
IDistributedCacheKeyNormalizer keyNormalizer,
IServiceScopeFactory serviceScopeFactory,
IUnitOfWorkManager unitOfWorkManager) : base(
distributedCacheOption: distributedCacheOption,
cache: cache,
cancellationTokenProvider: cancellationTokenProvider,
serializer: serializer,
keyNormalizer: keyNormalizer,
serviceScopeFactory: serviceScopeFactory,
unitOfWorkManager: unitOfWorkManager)
public IDistributedCache<TCacheItem, string> InternalCache { get; }
public DistributedCache(IDistributedCache<TCacheItem, string> internalCache)
{
InternalCache = internalCache;
}
public TCacheItem Get(string key, bool? hideErrors = null, bool considerUow = false)
{
return InternalCache.Get(key, hideErrors, considerUow);
}
public KeyValuePair<string, TCacheItem>[] GetMany(IEnumerable<string> keys, bool? hideErrors = null, bool considerUow = false)
{
return InternalCache.GetMany(keys, hideErrors, considerUow);
}
public Task<KeyValuePair<string, TCacheItem>[]> GetManyAsync(IEnumerable<string> keys, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default)
{
return InternalCache.GetManyAsync(keys, hideErrors, considerUow, token);
}
public Task<TCacheItem> GetAsync(string key, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default)
{
return InternalCache.GetAsync(key, hideErrors, considerUow, token);
}
public TCacheItem GetOrAdd(string key, Func<TCacheItem> factory, Func<DistributedCacheEntryOptions> optionsFactory = null, bool? hideErrors = null, bool considerUow = false)
{
return InternalCache.GetOrAdd(key, factory, optionsFactory, hideErrors, considerUow);
}
public Task<TCacheItem> GetOrAddAsync(string key, Func<Task<TCacheItem>> factory, Func<DistributedCacheEntryOptions> optionsFactory = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default)
{
return InternalCache.GetOrAddAsync(key, factory, optionsFactory, hideErrors, considerUow, token);
}
public KeyValuePair<string, TCacheItem>[] GetOrAddMany(IEnumerable<string> keys, Func<IEnumerable<string>, List<KeyValuePair<string, TCacheItem>>> factory, Func<DistributedCacheEntryOptions> optionsFactory = null, bool? hideErrors = null, bool considerUow = false)
{
return InternalCache.GetOrAddMany(keys, factory, optionsFactory, hideErrors, considerUow);
}
public Task<KeyValuePair<string, TCacheItem>[]> GetOrAddManyAsync(IEnumerable<string> keys, Func<IEnumerable<string>, Task<List<KeyValuePair<string, TCacheItem>>>> factory, Func<DistributedCacheEntryOptions> optionsFactory = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default)
{
return InternalCache.GetOrAddManyAsync(keys, factory, optionsFactory, hideErrors, considerUow, token);
}
public void Set(string key, TCacheItem value, DistributedCacheEntryOptions options = null, bool? hideErrors = null, bool considerUow = false)
{
InternalCache.Set(key, value, options, hideErrors, considerUow);
}
public Task SetAsync(string key, TCacheItem value, DistributedCacheEntryOptions options = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default)
{
return InternalCache.SetAsync(key, value, options, hideErrors, considerUow, token);
}
public void SetMany(IEnumerable<KeyValuePair<string, TCacheItem>> items, DistributedCacheEntryOptions options = null, bool? hideErrors = null, bool considerUow = false)
{
InternalCache.SetMany(items, options, hideErrors, considerUow);
}
public Task SetManyAsync(IEnumerable<KeyValuePair<string, TCacheItem>> items, DistributedCacheEntryOptions options = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default)
{
return InternalCache.SetManyAsync(items, options, hideErrors, considerUow, token);
}
public void Refresh(string key, bool? hideErrors = null)
{
InternalCache.Refresh(key, hideErrors);
}
public Task RefreshAsync(string key, bool? hideErrors = null, CancellationToken token = default)
{
return InternalCache.RefreshAsync(key, hideErrors, token);
}
public void RefreshMany(IEnumerable<string> keys, bool? hideErrors = null)
{
InternalCache.RefreshMany(keys, hideErrors);
}
public Task RefreshManyAsync(IEnumerable<string> keys, bool? hideErrors = null, CancellationToken token = default)
{
return InternalCache.RefreshManyAsync(keys, hideErrors, token);
}
public void Remove(string key, bool? hideErrors = null, bool considerUow = false)
{
InternalCache.Remove(key, hideErrors, considerUow);
}
public Task RemoveAsync(string key, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default)
{
return InternalCache.RemoveAsync(key, hideErrors, considerUow, token);
}
public void RemoveMany(IEnumerable<string> keys, bool? hideErrors = null, bool considerUow = false)
{
InternalCache.RemoveMany(keys, hideErrors, considerUow);
}
public Task RemoveManyAsync(IEnumerable<string> keys, bool? hideErrors = null, bool considerUow = false,
CancellationToken token = default)
{
return InternalCache.RemoveManyAsync(keys, hideErrors, considerUow, token);
}
}

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

@ -14,6 +14,7 @@ namespace Volo.Abp.Caching;
public interface IDistributedCache<TCacheItem> : IDistributedCache<TCacheItem, string>
where TCacheItem : class
{
IDistributedCache<TCacheItem, string> InternalCache { get; }
}
/// <summary>

14
framework/test/Volo.Abp.Caching.Tests/Volo/Abp/Caching/DistributedCache_ConfigureOptions_Test.cs

@ -34,7 +34,19 @@ public class DistributedCache_ConfigureOptions_Test : AbpIntegratedTest<AbpCachi
}
private static DistributedCacheEntryOptions GetDefaultCachingOptions(object instance)
{
var defaultOptionsField = instance.GetType().GetTypeInfo().GetField("DefaultCacheOptions", BindingFlags.Instance | BindingFlags.NonPublic);
var internalCacheProperty = instance
.GetType()
.GetProperty("InternalCache", BindingFlags.Instance | BindingFlags.Public);
if (internalCacheProperty != null)
{
instance = internalCacheProperty.GetValue(instance);
}
var defaultOptionsField = instance
.GetType()
.GetField("DefaultCacheOptions", BindingFlags.Instance | BindingFlags.NonPublic);
return (DistributedCacheEntryOptions)defaultOptionsField.GetValue(instance);
}
}

Loading…
Cancel
Save