Browse Source

Merge pull request #16661 from abpframework/liangshiwei/nullable

Group10: Enable nullable annotations for Volo.Abp.Caching.*
pull/16722/head
Halil İbrahim Kalkan 3 years ago
committed by GitHub
parent
commit
657e95affe
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj
  2. 2
      framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpCachingStackExchangeRedisModule.cs
  3. 60
      framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs
  4. 1
      framework/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj
  5. 10
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs
  6. 2
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs
  7. 131
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs
  8. 4
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/ICacheSupportsMultipleItems.cs
  9. 32
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCache.cs
  10. 2
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItem.cs
  11. 2
      framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItemExtensions.cs

2
framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj

@ -5,6 +5,8 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
<AssemblyName>Volo.Abp.Caching.StackExchangeRedis</AssemblyName>
<PackageId>Volo.Abp.Caching.StackExchangeRedis</PackageId>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>

2
framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpCachingStackExchangeRedisModule.cs

@ -16,7 +16,7 @@ public class AbpCachingStackExchangeRedisModule : AbpModule
var configuration = context.Services.GetConfiguration();
var redisEnabled = configuration["Redis:IsEnabled"];
if (redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled))
if (string.IsNullOrEmpty(redisEnabled) || bool.Parse(redisEnabled))
{
context.Services.AddStackExchangeRedisCache(options =>
{

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

@ -28,8 +28,8 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
private readonly static MethodInfo GetAbsoluteExpirationMethod;
private readonly static MethodInfo GetExpirationInSecondsMethod;
protected IDatabase RedisDatabase => GetRedisDatabase();
private IDatabase _redisDatabase;
protected IDatabase RedisDatabase => GetRedisDatabase()!;
private IDatabase? _redisDatabase;
protected string Instance { get; }
@ -37,28 +37,27 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
{
var type = typeof(RedisCache);
RedisDatabaseField = Check.NotNull(type.GetField("_cache", BindingFlags.Instance | BindingFlags.NonPublic), nameof(RedisDatabaseField));
RedisDatabaseField = Check.NotNull(type.GetField("_cache", BindingFlags.Instance | BindingFlags.NonPublic), nameof(RedisDatabaseField))!;
SetScriptField = Check.NotNull(type.GetField("_setScript", BindingFlags.Instance | BindingFlags.NonPublic), nameof(SetScriptField));
SetScriptField = Check.NotNull(type.GetField("_setScript", BindingFlags.Instance | BindingFlags.NonPublic), nameof(SetScriptField))!;
ConnectMethod = Check.NotNull(type.GetMethod("Connect", BindingFlags.Instance | BindingFlags.NonPublic), nameof(ConnectMethod))!;
ConnectMethod = Check.NotNull(type.GetMethod("Connect", BindingFlags.Instance | BindingFlags.NonPublic), nameof(ConnectMethod));
ConnectAsyncMethod = Check.NotNull(type.GetMethod("ConnectAsync", BindingFlags.Instance | BindingFlags.NonPublic), nameof(ConnectAsyncMethod))!;
ConnectAsyncMethod = Check.NotNull(type.GetMethod("ConnectAsync", BindingFlags.Instance | BindingFlags.NonPublic), nameof(ConnectAsyncMethod));
MapMetadataMethod = Check.NotNull(type.GetMethod("MapMetadata", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static), nameof(MapMetadataMethod))!;
MapMetadataMethod = Check.NotNull(type.GetMethod("MapMetadata", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static), nameof(MapMetadataMethod));
GetAbsoluteExpirationMethod = Check.NotNull(type.GetMethod("GetAbsoluteExpiration", BindingFlags.Static | BindingFlags.NonPublic), nameof(GetAbsoluteExpirationMethod))!;
GetAbsoluteExpirationMethod = Check.NotNull(type.GetMethod("GetAbsoluteExpiration", BindingFlags.Static | BindingFlags.NonPublic), nameof(GetAbsoluteExpirationMethod));
GetExpirationInSecondsMethod = Check.NotNull(type.GetMethod("GetExpirationInSeconds", BindingFlags.Static | BindingFlags.NonPublic), nameof(GetExpirationInSecondsMethod))!;
GetExpirationInSecondsMethod = Check.NotNull(type.GetMethod("GetExpirationInSeconds", BindingFlags.Static | BindingFlags.NonPublic), nameof(GetExpirationInSecondsMethod));
AbsoluteExpirationKey = type.GetField("AbsoluteExpirationKey", BindingFlags.Static | BindingFlags.NonPublic)!.GetValue(null)!.ToString()!;
AbsoluteExpirationKey = type.GetField("AbsoluteExpirationKey", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null).ToString();
SlidingExpirationKey = type.GetField("SlidingExpirationKey", BindingFlags.Static | BindingFlags.NonPublic)!.GetValue(null)!.ToString()!;
SlidingExpirationKey = type.GetField("SlidingExpirationKey", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null).ToString();
DataKey = type.GetField("DataKey", BindingFlags.Static | BindingFlags.NonPublic)!.GetValue(null)!.ToString()!;
DataKey = type.GetField("DataKey", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null).ToString();
// ReSharper disable once PossibleNullReferenceException
NotPresent = Check.NotNull(type.GetField("NotPresent", BindingFlags.Static | BindingFlags.NonPublic), nameof(NotPresent)).GetValue(null).To<int>();
NotPresent = type.GetField("NotPresent", BindingFlags.Static | BindingFlags.NonPublic)!.GetValue(null).To<int>();
}
public AbpRedisCache(IOptions<RedisCacheOptions> optionsAccessor)
@ -84,10 +83,10 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
return;
}
await (Task)ConnectAsyncMethod.Invoke(this, new object[] { token });
await (Task)ConnectAsyncMethod.Invoke(this, new object[] { token })!;
}
public byte[][] GetMany(
public byte[]?[] GetMany(
IEnumerable<string> keys)
{
keys = Check.NotNull(keys, nameof(keys));
@ -95,7 +94,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
return GetAndRefreshMany(keys, true);
}
public async Task<byte[][]> GetManyAsync(
public async Task<byte[]?[]> GetManyAsync(
IEnumerable<string> keys,
CancellationToken token = default)
{
@ -161,7 +160,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
await RedisDatabase.KeyDeleteAsync(keys.Select(key => (RedisKey)(Instance + key)).ToArray());
}
protected virtual byte[][] GetAndRefreshMany(
protected virtual byte[]?[] GetAndRefreshMany(
IEnumerable<string> keys,
bool getData)
{
@ -186,7 +185,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
return bytes;
}
protected virtual async Task<byte[][]> GetAndRefreshManyAsync(
protected virtual async Task<byte[]?[]> GetAndRefreshManyAsync(
IEnumerable<string> keys,
bool getData,
CancellationToken token = default)
@ -217,7 +216,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
protected virtual Task[] PipelineRefreshManyAndOutData(
string[] keys,
RedisValue[][] results,
out byte[][] bytes)
out byte[]?[] bytes)
{
bytes = new byte[keys.Length][];
var tasks = new Task[keys.Length];
@ -226,7 +225,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
{
if (results[i].Length >= 2)
{
MapMetadata(results[i], out DateTimeOffset? absExpr, out TimeSpan? sldExpr);
MapMetadata(results[i], out var absExpr, out var sldExpr);
if (sldExpr.HasValue)
{
@ -295,7 +294,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
out DateTimeOffset? absoluteExpiration,
out TimeSpan? slidingExpiration)
{
var parameters = new object[] { results, null, null };
var parameters = new object?[] { results, null, null };
MapMetadataMethod.Invoke(this, parameters);
absoluteExpiration = (DateTimeOffset?)parameters[1];
@ -308,7 +307,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
DistributedCacheEntryOptions options)
{
return (long?)GetExpirationInSecondsMethod.Invoke(null,
new object[] { creationTime, absoluteExpiration, options });
new object?[] { creationTime, absoluteExpiration, options });
}
protected virtual DateTimeOffset? GetAbsoluteExpiration(
@ -318,18 +317,13 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems
return (DateTimeOffset?)GetAbsoluteExpirationMethod.Invoke(null, new object[] { creationTime, options });
}
private IDatabase GetRedisDatabase()
private IDatabase? GetRedisDatabase()
{
if (_redisDatabase == null)
{
_redisDatabase = RedisDatabaseField.GetValue(this) as IDatabase;
}
return _redisDatabase;
return _redisDatabase ??= RedisDatabaseField.GetValue(this) as IDatabase;
}
private string GetSetScript()
{
return SetScriptField?.GetValue(this).ToString();
return SetScriptField.GetValue(this)!.ToString()!;
}
}

1
framework/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj

@ -5,6 +5,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<AssemblyName>Volo.Abp.Caching</AssemblyName>
<PackageId>Volo.Abp.Caching</PackageId>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>

10
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs

@ -25,26 +25,26 @@ public class AbpDistributedCacheOptions
/// List of all cache configurators.
/// (func argument:Name of cache)
/// </summary>
public List<Func<string, DistributedCacheEntryOptions>> CacheConfigurators { get; set; } //TODO: use a configurator interface instead?
public List<Func<string, DistributedCacheEntryOptions?>> CacheConfigurators { get; set; } //TODO: use a configurator interface instead?
public AbpDistributedCacheOptions()
{
CacheConfigurators = new List<Func<string, DistributedCacheEntryOptions>>();
CacheConfigurators = new List<Func<string, DistributedCacheEntryOptions?>>();
GlobalCacheEntryOptions = new DistributedCacheEntryOptions();
KeyPrefix = "";
}
public void ConfigureCache<TCacheItem>(DistributedCacheEntryOptions options)
public void ConfigureCache<TCacheItem>(DistributedCacheEntryOptions? options)
{
ConfigureCache(typeof(TCacheItem), options);
}
public void ConfigureCache(Type cacheItemType, DistributedCacheEntryOptions options)
public void ConfigureCache(Type cacheItemType, DistributedCacheEntryOptions? options)
{
ConfigureCache(CacheNameAttribute.GetCacheName(cacheItemType), options);
}
public void ConfigureCache(string cacheName, DistributedCacheEntryOptions options)
public void ConfigureCache(string cacheName, DistributedCacheEntryOptions? options)
{
CacheConfigurators.Add(name => cacheName != name ? null : options);
}

2
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs

@ -33,6 +33,6 @@ public class CacheNameAttribute : Attribute
return cacheNameAttribute.Name;
}
return cacheItemType.FullName.RemovePostFix("CacheItem");
return cacheItemType.FullName.RemovePostFix("CacheItem")!;
}
}

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

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@ -31,62 +30,62 @@ public class DistributedCache<TCacheItem> :
InternalCache = internalCache;
}
public TCacheItem Get(string key, bool? hideErrors = null, bool considerUow = false)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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);
}
@ -141,12 +140,13 @@ public class DistributedCache<TCacheItem> :
/// <typeparam name="TCacheKey">The type of cache key being used.</typeparam>
public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheItem, TCacheKey>
where TCacheItem : class
where TCacheKey : notnull
{
public const string UowCacheName = "AbpDistributedCache";
public ILogger<DistributedCache<TCacheItem, TCacheKey>> Logger { get; set; }
protected string CacheName { get; set; }
protected string CacheName { get; set; } = default!;
protected bool IgnoreMultiTenancy { get; set; }
@ -164,7 +164,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
protected SemaphoreSlim SyncSemaphore { get; }
protected DistributedCacheEntryOptions DefaultCacheOptions;
protected DistributedCacheEntryOptions DefaultCacheOptions = default!;
private readonly AbpDistributedCacheOptions _distributedCacheOption;
@ -195,7 +195,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
{
return KeyNormalizer.NormalizeKey(
new DistributedCacheKeyNormalizeArgs(
key.ToString(),
key.ToString()!,
CacheName,
IgnoreMultiTenancy
)
@ -234,7 +234,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
/// <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>
public virtual TCacheItem Get(
public virtual TCacheItem? Get(
TCacheKey key,
bool? hideErrors = null,
bool considerUow = false)
@ -250,7 +250,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
}
byte[] cachedBytes;
byte[]? cachedBytes;
try
{
@ -270,7 +270,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
return ToCacheItem(cachedBytes);
}
public virtual KeyValuePair<TCacheKey, TCacheItem>[] GetMany(
public virtual KeyValuePair<TCacheKey, TCacheItem?>[] GetMany(
IEnumerable<TCacheKey> keys,
bool? hideErrors = null,
bool considerUow = false)
@ -288,7 +288,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
var notCachedKeys = new List<TCacheKey>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem>>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem?>>();
if (ShouldConsiderUow(considerUow))
{
var uowCache = GetUnitOfWorkCache();
@ -297,7 +297,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
var value = uowCache.GetOrDefault(key)?.GetUnRemovedValueOrNull();
if (value != null)
{
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem>(key, value));
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem?>(key, value));
}
}
@ -309,7 +309,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
byte[][] cachedBytes;
byte[]?[] cachedBytes;
var readKeys = notCachedKeys.Any() ? notCachedKeys.ToArray() : keyArray;
try
@ -330,7 +330,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
return cachedValues.Concat(ToCacheItems(cachedBytes, readKeys)).ToArray();
}
protected virtual KeyValuePair<TCacheKey, TCacheItem>[] GetManyFallback(
protected virtual KeyValuePair<TCacheKey, TCacheItem?>[] GetManyFallback(
TCacheKey[] keys,
bool? hideErrors = null,
bool considerUow = false)
@ -340,7 +340,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
try
{
return keys
.Select(key => new KeyValuePair<TCacheKey, TCacheItem>(
.Select(key => new KeyValuePair<TCacheKey, TCacheItem?>(
key,
Get(key, false, considerUow)
)
@ -358,7 +358,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
}
public virtual async Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyAsync(
public virtual async Task<KeyValuePair<TCacheKey, TCacheItem?>[]> GetManyAsync(
IEnumerable<TCacheKey> keys,
bool? hideErrors = null,
bool considerUow = false,
@ -378,7 +378,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
var notCachedKeys = new List<TCacheKey>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem>>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem?>>();
if (ShouldConsiderUow(considerUow))
{
var uowCache = GetUnitOfWorkCache();
@ -387,7 +387,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
var value = uowCache.GetOrDefault(key)?.GetUnRemovedValueOrNull();
if (value != null)
{
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem>(key, value));
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem?>(key, value));
}
}
@ -399,7 +399,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
byte[][] cachedBytes;
byte[]?[] cachedBytes;
var readKeys = notCachedKeys.Any() ? notCachedKeys.ToArray() : keyArray;
@ -424,7 +424,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
return cachedValues.Concat(ToCacheItems(cachedBytes, readKeys)).ToArray();
}
protected virtual async Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyFallbackAsync(
protected virtual async Task<KeyValuePair<TCacheKey, TCacheItem?>[]> GetManyFallbackAsync(
TCacheKey[] keys,
bool? hideErrors = null,
bool considerUow = false,
@ -434,11 +434,11 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
try
{
var result = new List<KeyValuePair<TCacheKey, TCacheItem>>();
var result = new List<KeyValuePair<TCacheKey, TCacheItem?>>();
foreach (var key in keys)
{
result.Add(new KeyValuePair<TCacheKey, TCacheItem>(
result.Add(new KeyValuePair<TCacheKey, TCacheItem?>(
key,
await GetAsync(key, false, considerUow, token: token))
);
@ -466,7 +466,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
/// <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 cache item, or null.</returns>
public virtual async Task<TCacheItem> GetAsync(
public virtual async Task<TCacheItem?> GetAsync(
TCacheKey key,
bool? hideErrors = null,
bool considerUow = false,
@ -483,7 +483,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
}
byte[] cachedBytes;
byte[]? cachedBytes;
try
{
@ -521,10 +521,10 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
/// <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>
public virtual TCacheItem GetOrAdd(
public virtual TCacheItem? GetOrAdd(
TCacheKey key,
Func<TCacheItem> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
Func<DistributedCacheEntryOptions>? optionsFactory = null,
bool? hideErrors = null,
bool considerUow = false)
{
@ -574,10 +574,10 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
/// <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 cache item.</returns>
public virtual async Task<TCacheItem> GetOrAddAsync(
public virtual async Task<TCacheItem?> GetOrAddAsync(
TCacheKey key,
Func<Task<TCacheItem>> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
Func<DistributedCacheEntryOptions>? optionsFactory = null,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default)
@ -618,15 +618,15 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
return value;
}
public KeyValuePair<TCacheKey, TCacheItem>[] GetOrAddMany(
public KeyValuePair<TCacheKey, TCacheItem?>[] GetOrAddMany(
IEnumerable<TCacheKey> keys,
Func<IEnumerable<TCacheKey>, List<KeyValuePair<TCacheKey, TCacheItem>>> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
Func<DistributedCacheEntryOptions>? optionsFactory = null,
bool? hideErrors = null,
bool considerUow = false)
{
KeyValuePair<TCacheKey, TCacheItem>[] result;
KeyValuePair<TCacheKey, TCacheItem?>[] result;
var keyArray = keys.ToArray();
var cacheSupportsMultipleItems = Cache as ICacheSupportsMultipleItems;
@ -641,7 +641,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
else
{
var notCachedKeys = new List<TCacheKey>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem>>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem?>>();
if (ShouldConsiderUow(considerUow))
{
var uowCache = GetUnitOfWorkCache();
@ -650,7 +650,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
var value = uowCache.GetOrDefault(key)?.GetUnRemovedValueOrNull();
if (value != null)
{
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem>(key, value));
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem?>(key, value));
}
}
@ -662,7 +662,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
byte[][] cachedBytes;
byte[]?[] cachedBytes;
var readKeys = notCachedKeys.Any() ? notCachedKeys.ToArray() : keyArray;
try
@ -685,7 +685,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
if (result.All(x => x.Value != null))
{
return result;
return result!;
}
var missingKeys = new List<TCacheKey>();
@ -708,22 +708,22 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
foreach (var index in missingValuesIndex)
{
result[index] = valueQueue.Dequeue();
result[index] = valueQueue.Dequeue()!;
}
return result;
}
public async Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetOrAddManyAsync(
public async Task<KeyValuePair<TCacheKey, TCacheItem?>[]> GetOrAddManyAsync(
IEnumerable<TCacheKey> keys,
Func<IEnumerable<TCacheKey>, Task<List<KeyValuePair<TCacheKey, TCacheItem>>>> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
Func<DistributedCacheEntryOptions>? optionsFactory = null,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default)
{
KeyValuePair<TCacheKey, TCacheItem>[] result;
KeyValuePair<TCacheKey, TCacheItem?>[] result;
var keyArray = keys.ToArray();
var cacheSupportsMultipleItems = Cache as ICacheSupportsMultipleItems;
@ -737,7 +737,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
else
{
var notCachedKeys = new List<TCacheKey>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem>>();
var cachedValues = new List<KeyValuePair<TCacheKey, TCacheItem?>>();
if (ShouldConsiderUow(considerUow))
{
var uowCache = GetUnitOfWorkCache();
@ -746,7 +746,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
var value = uowCache.GetOrDefault(key)?.GetUnRemovedValueOrNull();
if (value != null)
{
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem>(key, value));
cachedValues.Add(new KeyValuePair<TCacheKey, TCacheItem?>(key, value));
}
}
@ -758,7 +758,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
hideErrors = hideErrors ?? _distributedCacheOption.HideErrors;
byte[][] cachedBytes;
byte[]?[] cachedBytes;
var readKeys = notCachedKeys.Any() ? notCachedKeys.ToArray() : keyArray;
try
@ -804,7 +804,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
foreach (var index in missingValuesIndex)
{
result[index] = valueQueue.Dequeue();
result[index] = valueQueue.Dequeue()!;
}
return result;
@ -821,7 +821,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
public virtual void Set(
TCacheKey key,
TCacheItem value,
DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false)
{
@ -886,7 +886,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
public virtual async Task SetAsync(
TCacheKey key,
TCacheItem value,
DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default)
@ -939,7 +939,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
public void SetMany(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false)
{
@ -1012,7 +1012,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
protected virtual void SetManyFallback(
KeyValuePair<TCacheKey, TCacheItem>[] items,
DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false)
{
@ -1045,7 +1045,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
public virtual async Task SetManyAsync(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default)
@ -1117,7 +1117,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
protected virtual async Task SetManyFallbackAsync(
KeyValuePair<TCacheKey, TCacheItem>[] items,
DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default)
@ -1516,19 +1516,19 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
}
}
protected virtual KeyValuePair<TCacheKey, TCacheItem>[] ToCacheItems(byte[][] itemBytes, TCacheKey[] itemKeys)
protected virtual KeyValuePair<TCacheKey, TCacheItem?>[] ToCacheItems(byte[]?[] itemBytes, TCacheKey[] itemKeys)
{
if (itemBytes.Length != itemKeys.Length)
{
throw new AbpException("count of the item bytes should be same with the count of the given keys");
}
var result = new List<KeyValuePair<TCacheKey, TCacheItem>>();
var result = new List<KeyValuePair<TCacheKey, TCacheItem?>>();
for (int i = 0; i < itemKeys.Length; i++)
for (var i = 0; i < itemKeys.Length; i++)
{
result.Add(
new KeyValuePair<TCacheKey, TCacheItem>(
new KeyValuePair<TCacheKey, TCacheItem?>(
itemKeys[i],
ToCacheItem(itemBytes[i])
)
@ -1538,8 +1538,7 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
return result.ToArray();
}
[CanBeNull]
protected virtual TCacheItem ToCacheItem([CanBeNull] byte[] bytes)
protected virtual TCacheItem? ToCacheItem(byte[]? bytes)
{
if (bytes == null)
{
@ -1560,10 +1559,10 @@ public class DistributedCache<TCacheItem, TCacheKey> : IDistributedCache<TCacheI
).ToArray();
}
private static KeyValuePair<TCacheKey, TCacheItem>[] ToCacheItemsWithDefaultValues(TCacheKey[] keys)
private static KeyValuePair<TCacheKey, TCacheItem?>[] ToCacheItemsWithDefaultValues(TCacheKey[] keys)
{
return keys
.Select(key => new KeyValuePair<TCacheKey, TCacheItem>(key, default))
.Select(key => new KeyValuePair<TCacheKey, TCacheItem?>(key, default))
.ToArray();
}

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

@ -7,11 +7,11 @@ namespace Volo.Abp.Caching;
public interface ICacheSupportsMultipleItems
{
byte[][] GetMany(
byte[]?[] GetMany(
IEnumerable<string> keys
);
Task<byte[][]> GetManyAsync(
Task<byte[]?[]> GetManyAsync(
IEnumerable<string> keys,
CancellationToken token = default
);

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

@ -33,7 +33,7 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <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>
TCacheItem Get(
TCacheItem? Get(
TCacheKey key,
bool? hideErrors = null,
bool considerUow = false
@ -50,7 +50,7 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <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>
KeyValuePair<TCacheKey, TCacheItem>[] GetMany(
KeyValuePair<TCacheKey, TCacheItem?>[] GetMany(
IEnumerable<TCacheKey> keys,
bool? hideErrors = null,
bool considerUow = false
@ -69,7 +69,7 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <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>List of cache items.</returns>
Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyAsync(
Task<KeyValuePair<TCacheKey, TCacheItem?>[]> GetManyAsync(
IEnumerable<TCacheKey> keys,
bool? hideErrors = null,
bool considerUow = false,
@ -84,7 +84,7 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <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 cache item, or null.</returns>
Task<TCacheItem> GetAsync(
Task<TCacheItem?> GetAsync(
[NotNull] TCacheKey key,
bool? hideErrors = null,
bool considerUow = false,
@ -101,10 +101,10 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <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>
TCacheItem GetOrAdd(
TCacheItem? GetOrAdd(
TCacheKey key,
Func<TCacheItem> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
Func<DistributedCacheEntryOptions>? optionsFactory = null,
bool? hideErrors = null,
bool considerUow = false
);
@ -120,10 +120,10 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <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 cache item.</returns>
Task<TCacheItem> GetOrAddAsync(
Task<TCacheItem?> GetOrAddAsync(
[NotNull] TCacheKey key,
Func<Task<TCacheItem>> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
Func<DistributedCacheEntryOptions>? optionsFactory = null,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default
@ -139,10 +139,10 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <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 items.</returns>
KeyValuePair<TCacheKey, TCacheItem>[] GetOrAddMany(
KeyValuePair<TCacheKey, TCacheItem?>[] GetOrAddMany(
IEnumerable<TCacheKey> keys,
Func<IEnumerable<TCacheKey>, List<KeyValuePair<TCacheKey, TCacheItem>>> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
Func<DistributedCacheEntryOptions>? optionsFactory = null,
bool? hideErrors = null,
bool considerUow = false
);
@ -158,10 +158,10 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <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 cache items.</returns>
Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetOrAddManyAsync(
Task<KeyValuePair<TCacheKey, TCacheItem?>[]> GetOrAddManyAsync(
IEnumerable<TCacheKey> keys,
Func<IEnumerable<TCacheKey>, Task<List<KeyValuePair<TCacheKey, TCacheItem>>>> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
Func<DistributedCacheEntryOptions>? optionsFactory = null,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default
@ -178,7 +178,7 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
void Set(
TCacheKey key,
TCacheItem value,
DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false
);
@ -196,7 +196,7 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
Task SetAsync(
[NotNull] TCacheKey key,
[NotNull] TCacheItem value,
[CanBeNull] DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default
@ -212,7 +212,7 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <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(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false
);
@ -229,7 +229,7 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
Task SetManyAsync(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null,
DistributedCacheEntryOptions? options = null,
bool? hideErrors = null,
bool considerUow = false,
CancellationToken token = default

2
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItem.cs

@ -8,7 +8,7 @@ public class UnitOfWorkCacheItem<TValue>
{
public bool IsRemoved { get; set; }
public TValue Value { get; set; }
public TValue? Value { get; set; }
public UnitOfWorkCacheItem()
{

2
framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItemExtensions.cs

@ -2,7 +2,7 @@
public static class UnitOfWorkCacheItemExtensions
{
public static TValue GetUnRemovedValueOrNull<TValue>(this UnitOfWorkCacheItem<TValue> item)
public static TValue? GetUnRemovedValueOrNull<TValue>(this UnitOfWorkCacheItem<TValue>? item)
where TValue : class
{
return item != null && !item.IsRemoved ? item.Value : null;

Loading…
Cancel
Save