From 9d01385e361734ab4b2836167ec8075931871a8a Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Thu, 25 May 2023 16:00:08 +0800 Subject: [PATCH 1/4] Annotate Volo.Abp.Caching for nullability --- .../Volo.Abp.Caching/Volo.Abp.Caching.csproj | 1 + .../Abp/Caching/AbpDistributedCacheOptions.cs | 10 +- .../Volo/Abp/Caching/CacheNameAttribute.cs | 2 +- .../Volo/Abp/Caching/DistributedCache.cs | 131 +++++++++--------- .../Caching/ICacheSupportsMultipleItems.cs | 4 +- .../Volo/Abp/Caching/IDistributedCache.cs | 32 ++--- .../Volo/Abp/Caching/UnitOfWorkCacheItem.cs | 2 +- .../Caching/UnitOfWorkCacheItemExtensions.cs | 2 +- 8 files changed, 92 insertions(+), 92 deletions(-) diff --git a/framework/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj b/framework/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj index 52d6a3381d..adc2ef921d 100644 --- a/framework/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj +++ b/framework/src/Volo.Abp.Caching/Volo.Abp.Caching.csproj @@ -5,6 +5,7 @@ netstandard2.0;netstandard2.1;net7.0 + enable Volo.Abp.Caching Volo.Abp.Caching $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs index 26b2ef506b..9cb42fdf96 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/AbpDistributedCacheOptions.cs +++ b/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) /// - public List> CacheConfigurators { get; set; } //TODO: use a configurator interface instead? + public List> CacheConfigurators { get; set; } //TODO: use a configurator interface instead? public AbpDistributedCacheOptions() { - CacheConfigurators = new List>(); + CacheConfigurators = new List>(); GlobalCacheEntryOptions = new DistributedCacheEntryOptions(); KeyPrefix = ""; } - public void ConfigureCache(DistributedCacheEntryOptions options) + public void ConfigureCache(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); } diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs index ff162921c9..c49225acbf 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/CacheNameAttribute.cs +++ b/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")!; } } diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs index a72a369655..03e42b5d92 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs +++ b/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 : 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[] GetMany(IEnumerable keys, bool? hideErrors = null, bool considerUow = false) + public KeyValuePair[] GetMany(IEnumerable keys, bool? hideErrors = null, bool considerUow = false) { return InternalCache.GetMany(keys, hideErrors, considerUow); } - public Task[]> GetManyAsync(IEnumerable keys, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) + public Task[]> GetManyAsync(IEnumerable keys, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) { return InternalCache.GetManyAsync(keys, hideErrors, considerUow, token); } - public Task GetAsync(string key, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) + public Task 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 factory, Func optionsFactory = null, bool? hideErrors = null, bool considerUow = false) + public TCacheItem? GetOrAdd(string key, Func factory, Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false) { return InternalCache.GetOrAdd(key, factory, optionsFactory, hideErrors, considerUow); } - public Task GetOrAddAsync(string key, Func> factory, Func optionsFactory = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) + public Task GetOrAddAsync(string key, Func> factory, Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) { return InternalCache.GetOrAddAsync(key, factory, optionsFactory, hideErrors, considerUow, token); } - public KeyValuePair[] GetOrAddMany(IEnumerable keys, Func, List>> factory, Func optionsFactory = null, bool? hideErrors = null, bool considerUow = false) + public KeyValuePair[] GetOrAddMany(IEnumerable keys, Func, List>> factory, Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false) { return InternalCache.GetOrAddMany(keys, factory, optionsFactory, hideErrors, considerUow); } - public Task[]> GetOrAddManyAsync(IEnumerable keys, Func, Task>>> factory, Func optionsFactory = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) + public Task[]> GetOrAddManyAsync(IEnumerable keys, Func, Task>>> factory, Func? 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> items, DistributedCacheEntryOptions options = null, bool? hideErrors = null, bool considerUow = false) + public void SetMany(IEnumerable> items, DistributedCacheEntryOptions? options = null, bool? hideErrors = null, bool considerUow = false) { InternalCache.SetMany(items, options, hideErrors, considerUow); } - public Task SetManyAsync(IEnumerable> items, DistributedCacheEntryOptions options = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) + public Task SetManyAsync(IEnumerable> 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 : /// The type of cache key being used. public class DistributedCache : IDistributedCache where TCacheItem : class + where TCacheKey : notnull { public const string UowCacheName = "AbpDistributedCache"; public ILogger> 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 : IDistributedCache : IDistributedCache : IDistributedCacheIndicates to throw or hide the exceptions for the distributed cache. /// 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. /// The cache item, or null. - public virtual TCacheItem Get( + public virtual TCacheItem? Get( TCacheKey key, bool? hideErrors = null, bool considerUow = false) @@ -250,7 +250,7 @@ public class DistributedCache : IDistributedCache : IDistributedCache[] GetMany( + public virtual KeyValuePair[] GetMany( IEnumerable keys, bool? hideErrors = null, bool considerUow = false) @@ -288,7 +288,7 @@ public class DistributedCache : IDistributedCache(); - var cachedValues = new List>(); + var cachedValues = new List>(); if (ShouldConsiderUow(considerUow)) { var uowCache = GetUnitOfWorkCache(); @@ -297,7 +297,7 @@ public class DistributedCache : IDistributedCache(key, value)); + cachedValues.Add(new KeyValuePair(key, value)); } } @@ -309,7 +309,7 @@ public class DistributedCache : IDistributedCache : IDistributedCache[] GetManyFallback( + protected virtual KeyValuePair[] GetManyFallback( TCacheKey[] keys, bool? hideErrors = null, bool considerUow = false) @@ -340,7 +340,7 @@ public class DistributedCache : IDistributedCache new KeyValuePair( + .Select(key => new KeyValuePair( key, Get(key, false, considerUow) ) @@ -358,7 +358,7 @@ public class DistributedCache : IDistributedCache[]> GetManyAsync( + public virtual async Task[]> GetManyAsync( IEnumerable keys, bool? hideErrors = null, bool considerUow = false, @@ -378,7 +378,7 @@ public class DistributedCache : IDistributedCache(); - var cachedValues = new List>(); + var cachedValues = new List>(); if (ShouldConsiderUow(considerUow)) { var uowCache = GetUnitOfWorkCache(); @@ -387,7 +387,7 @@ public class DistributedCache : IDistributedCache(key, value)); + cachedValues.Add(new KeyValuePair(key, value)); } } @@ -399,7 +399,7 @@ public class DistributedCache : IDistributedCache : IDistributedCache[]> GetManyFallbackAsync( + protected virtual async Task[]> GetManyFallbackAsync( TCacheKey[] keys, bool? hideErrors = null, bool considerUow = false, @@ -434,11 +434,11 @@ public class DistributedCache : IDistributedCache>(); + var result = new List>(); foreach (var key in keys) { - result.Add(new KeyValuePair( + result.Add(new KeyValuePair( key, await GetAsync(key, false, considerUow, token: token)) ); @@ -466,7 +466,7 @@ public class DistributedCache : IDistributedCacheThis 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. /// The for the task. /// The cache item, or null. - public virtual async Task GetAsync( + public virtual async Task GetAsync( TCacheKey key, bool? hideErrors = null, bool considerUow = false, @@ -483,7 +483,7 @@ public class DistributedCache : IDistributedCache : IDistributedCacheIndicates to throw or hide the exceptions for the distributed cache. /// 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. /// The cache item. - public virtual TCacheItem GetOrAdd( + public virtual TCacheItem? GetOrAdd( TCacheKey key, Func factory, - Func optionsFactory = null, + Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false) { @@ -574,10 +574,10 @@ public class DistributedCache : IDistributedCacheThis 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. /// The for the task. /// The cache item. - public virtual async Task GetOrAddAsync( + public virtual async Task GetOrAddAsync( TCacheKey key, Func> factory, - Func optionsFactory = null, + Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) @@ -618,15 +618,15 @@ public class DistributedCache : IDistributedCache[] GetOrAddMany( + public KeyValuePair[] GetOrAddMany( IEnumerable keys, Func, List>> factory, - Func optionsFactory = null, + Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false) { - KeyValuePair[] result; + KeyValuePair[] result; var keyArray = keys.ToArray(); var cacheSupportsMultipleItems = Cache as ICacheSupportsMultipleItems; @@ -641,7 +641,7 @@ public class DistributedCache : IDistributedCache(); - var cachedValues = new List>(); + var cachedValues = new List>(); if (ShouldConsiderUow(considerUow)) { var uowCache = GetUnitOfWorkCache(); @@ -650,7 +650,7 @@ public class DistributedCache : IDistributedCache(key, value)); + cachedValues.Add(new KeyValuePair(key, value)); } } @@ -662,7 +662,7 @@ public class DistributedCache : IDistributedCache : IDistributedCache x.Value != null)) { - return result; + return result!; } var missingKeys = new List(); @@ -708,22 +708,22 @@ public class DistributedCache : IDistributedCache[]> GetOrAddManyAsync( + public async Task[]> GetOrAddManyAsync( IEnumerable keys, Func, Task>>> factory, - Func optionsFactory = null, + Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) { - KeyValuePair[] result; + KeyValuePair[] result; var keyArray = keys.ToArray(); var cacheSupportsMultipleItems = Cache as ICacheSupportsMultipleItems; @@ -737,7 +737,7 @@ public class DistributedCache : IDistributedCache(); - var cachedValues = new List>(); + var cachedValues = new List>(); if (ShouldConsiderUow(considerUow)) { var uowCache = GetUnitOfWorkCache(); @@ -746,7 +746,7 @@ public class DistributedCache : IDistributedCache(key, value)); + cachedValues.Add(new KeyValuePair(key, value)); } } @@ -758,7 +758,7 @@ public class DistributedCache : IDistributedCache : IDistributedCache : IDistributedCache : IDistributedCache : IDistributedCache> items, - DistributedCacheEntryOptions options = null, + DistributedCacheEntryOptions? options = null, bool? hideErrors = null, bool considerUow = false) { @@ -1012,7 +1012,7 @@ public class DistributedCache : IDistributedCache[] items, - DistributedCacheEntryOptions options = null, + DistributedCacheEntryOptions? options = null, bool? hideErrors = null, bool considerUow = false) { @@ -1045,7 +1045,7 @@ public class DistributedCache : IDistributedCache> items, - DistributedCacheEntryOptions options = null, + DistributedCacheEntryOptions? options = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) @@ -1117,7 +1117,7 @@ public class DistributedCache : IDistributedCache[] items, - DistributedCacheEntryOptions options = null, + DistributedCacheEntryOptions? options = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default) @@ -1516,19 +1516,19 @@ public class DistributedCache : IDistributedCache[] ToCacheItems(byte[][] itemBytes, TCacheKey[] itemKeys) + protected virtual KeyValuePair[] 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>(); + var result = new List>(); - for (int i = 0; i < itemKeys.Length; i++) + for (var i = 0; i < itemKeys.Length; i++) { result.Add( - new KeyValuePair( + new KeyValuePair( itemKeys[i], ToCacheItem(itemBytes[i]) ) @@ -1538,8 +1538,7 @@ public class DistributedCache : IDistributedCache : IDistributedCache[] ToCacheItemsWithDefaultValues(TCacheKey[] keys) + private static KeyValuePair[] ToCacheItemsWithDefaultValues(TCacheKey[] keys) { return keys - .Select(key => new KeyValuePair(key, default)) + .Select(key => new KeyValuePair(key, default)) .ToArray(); } diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/ICacheSupportsMultipleItems.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/ICacheSupportsMultipleItems.cs index f11b8b56c6..80857aed17 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/ICacheSupportsMultipleItems.cs +++ b/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 keys ); - Task GetManyAsync( + Task GetManyAsync( IEnumerable keys, CancellationToken token = default ); diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCache.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCache.cs index ab3ca560a8..af654c8d9a 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCache.cs +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/IDistributedCache.cs @@ -33,7 +33,7 @@ public interface IDistributedCache /// Indicates to throw or hide the exceptions for the distributed cache. /// 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. /// The cache item, or null. - TCacheItem Get( + TCacheItem? Get( TCacheKey key, bool? hideErrors = null, bool considerUow = false @@ -50,7 +50,7 @@ public interface IDistributedCache /// Indicates to throw or hide the exceptions for the distributed cache. /// 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. /// List of cache items. - KeyValuePair[] GetMany( + KeyValuePair[] GetMany( IEnumerable keys, bool? hideErrors = null, bool considerUow = false @@ -69,7 +69,7 @@ public interface IDistributedCache /// 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. /// /// The for the task. /// List of cache items. - Task[]> GetManyAsync( + Task[]> GetManyAsync( IEnumerable keys, bool? hideErrors = null, bool considerUow = false, @@ -84,7 +84,7 @@ public interface IDistributedCache /// 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. /// The for the task. /// The cache item, or null. - Task GetAsync( + Task GetAsync( [NotNull] TCacheKey key, bool? hideErrors = null, bool considerUow = false, @@ -101,10 +101,10 @@ public interface IDistributedCache /// Indicates to throw or hide the exceptions for the distributed cache. /// 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. /// The cache item. - TCacheItem GetOrAdd( + TCacheItem? GetOrAdd( TCacheKey key, Func factory, - Func optionsFactory = null, + Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false ); @@ -120,10 +120,10 @@ public interface IDistributedCache /// 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. /// The for the task. /// The cache item. - Task GetOrAddAsync( + Task GetOrAddAsync( [NotNull] TCacheKey key, Func> factory, - Func optionsFactory = null, + Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default @@ -139,10 +139,10 @@ public interface IDistributedCache /// Indicates to throw or hide the exceptions for the distributed cache. /// 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. /// The cache items. - KeyValuePair[] GetOrAddMany( + KeyValuePair[] GetOrAddMany( IEnumerable keys, Func, List>> factory, - Func optionsFactory = null, + Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false ); @@ -158,10 +158,10 @@ public interface IDistributedCache /// 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. /// The for the task. /// The cache items. - Task[]> GetOrAddManyAsync( + Task[]> GetOrAddManyAsync( IEnumerable keys, Func, Task>>> factory, - Func optionsFactory = null, + Func? optionsFactory = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default @@ -178,7 +178,7 @@ public interface IDistributedCache 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 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 /// 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. void SetMany( IEnumerable> items, - DistributedCacheEntryOptions options = null, + DistributedCacheEntryOptions? options = null, bool? hideErrors = null, bool considerUow = false ); @@ -229,7 +229,7 @@ public interface IDistributedCache /// The indicating that the operation is asynchronous. Task SetManyAsync( IEnumerable> items, - DistributedCacheEntryOptions options = null, + DistributedCacheEntryOptions? options = null, bool? hideErrors = null, bool considerUow = false, CancellationToken token = default diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItem.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItem.cs index 2c0622eed2..4dee5d97fc 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItem.cs +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItem.cs @@ -8,7 +8,7 @@ public class UnitOfWorkCacheItem { public bool IsRemoved { get; set; } - public TValue Value { get; set; } + public TValue? Value { get; set; } public UnitOfWorkCacheItem() { diff --git a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItemExtensions.cs b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItemExtensions.cs index 6dc7909fa8..860e9cfb27 100644 --- a/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItemExtensions.cs +++ b/framework/src/Volo.Abp.Caching/Volo/Abp/Caching/UnitOfWorkCacheItemExtensions.cs @@ -2,7 +2,7 @@ public static class UnitOfWorkCacheItemExtensions { - public static TValue GetUnRemovedValueOrNull(this UnitOfWorkCacheItem item) + public static TValue? GetUnRemovedValueOrNull(this UnitOfWorkCacheItem? item) where TValue : class { return item != null && !item.IsRemoved ? item.Value : null; From f894aa19f6faf66520a1deca93a2155d8f6935cc Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Thu, 25 May 2023 16:00:34 +0800 Subject: [PATCH 2/4] Annotate Volo.Abp.Caching.StackExchangeRedis for nullability --- ...Volo.Abp.Caching.StackExchangeRedis.csproj | 1 + .../AbpCachingStackExchangeRedisModule.cs | 2 +- .../StackExchangeRedis/AbpRedisCache.cs | 65 ++++++++----------- 3 files changed, 29 insertions(+), 39 deletions(-) diff --git a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj b/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj index e2bfd7549a..8963eb4b54 100644 --- a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj +++ b/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj @@ -5,6 +5,7 @@ netstandard2.0;netstandard2.1;net7.0 + enable Volo.Abp.Caching.StackExchangeRedis Volo.Abp.Caching.StackExchangeRedis $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; diff --git a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpCachingStackExchangeRedisModule.cs b/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpCachingStackExchangeRedisModule.cs index d6c0d8b916..817c42af18 100644 --- a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpCachingStackExchangeRedisModule.cs +++ b/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 => { diff --git a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs b/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs index ecad8b16d5..a4bb8a57b0 100644 --- a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs +++ b/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs @@ -18,9 +18,9 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems protected static readonly string AbsoluteExpirationKey; protected static readonly string SlidingExpirationKey; protected static readonly string DataKey; + protected static readonly string SetScript; protected static readonly long NotPresent; - private readonly static FieldInfo SetScriptField; private readonly static FieldInfo RedisDatabaseField; private readonly static MethodInfo ConnectMethod; private readonly static MethodInfo ConnectAsyncMethod; @@ -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)); + 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(); + NotPresent = type.GetField("NotPresent", BindingFlags.Static | BindingFlags.NonPublic)!.GetValue(null).To(); + + SetScript = type.GetField("_setScript", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(null)!.ToString()!; } public AbpRedisCache(IOptions 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 keys) { keys = Check.NotNull(keys, nameof(keys)); @@ -95,7 +94,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems return GetAndRefreshMany(keys, true); } - public async Task GetManyAsync( + public async Task GetManyAsync( IEnumerable 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 keys, bool getData) { @@ -186,7 +185,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems return bytes; } - protected virtual async Task GetAndRefreshManyAsync( + protected virtual async Task GetAndRefreshManyAsync( IEnumerable 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) { @@ -277,7 +276,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems for (var i = 0; i < itemArray.Length; i++) { - tasks[i] = RedisDatabase.ScriptEvaluateAsync(GetSetScript(), new RedisKey[] { Instance + itemArray[i].Key }, + tasks[i] = RedisDatabase.ScriptEvaluateAsync(SetScript, new RedisKey[] { Instance + itemArray[i].Key }, new RedisValue[] { absoluteExpiration?.Ticks ?? NotPresent, @@ -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,8 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems return (DateTimeOffset?)GetAbsoluteExpirationMethod.Invoke(null, new object[] { creationTime, options }); } - private IDatabase GetRedisDatabase() - { - if (_redisDatabase == null) - { - _redisDatabase = RedisDatabaseField.GetValue(this) as IDatabase; - } - - return _redisDatabase; - } - - private string GetSetScript() + private IDatabase? GetRedisDatabase() { - return SetScriptField?.GetValue(this).ToString(); + return _redisDatabase ??= RedisDatabaseField.GetValue(this) as IDatabase; } } From fd63bdcc0a88c6a57ed612a759f95d925c12430d Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Thu, 25 May 2023 17:45:15 +0800 Subject: [PATCH 3/4] Update AbpRedisCache.cs --- .../Abp/Caching/StackExchangeRedis/AbpRedisCache.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs b/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs index a4bb8a57b0..650ff1a562 100644 --- a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs +++ b/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo/Abp/Caching/StackExchangeRedis/AbpRedisCache.cs @@ -18,9 +18,9 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems protected static readonly string AbsoluteExpirationKey; protected static readonly string SlidingExpirationKey; protected static readonly string DataKey; - protected static readonly string SetScript; protected static readonly long NotPresent; + private readonly static FieldInfo SetScriptField; private readonly static FieldInfo RedisDatabaseField; private readonly static MethodInfo ConnectMethod; private readonly static MethodInfo ConnectAsyncMethod; @@ -39,6 +39,8 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems RedisDatabaseField = Check.NotNull(type.GetField("_cache", BindingFlags.Instance | BindingFlags.NonPublic), nameof(RedisDatabaseField))!; + SetScriptField = Check.NotNull(type.GetField("_setScript", BindingFlags.Instance | BindingFlags.NonPublic), nameof(SetScriptField))!; + ConnectMethod = Check.NotNull(type.GetMethod("Connect", BindingFlags.Instance | BindingFlags.NonPublic), nameof(ConnectMethod))!; ConnectAsyncMethod = Check.NotNull(type.GetMethod("ConnectAsync", BindingFlags.Instance | BindingFlags.NonPublic), nameof(ConnectAsyncMethod))!; @@ -56,8 +58,6 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems DataKey = type.GetField("DataKey", BindingFlags.Static | BindingFlags.NonPublic)!.GetValue(null)!.ToString()!; NotPresent = type.GetField("NotPresent", BindingFlags.Static | BindingFlags.NonPublic)!.GetValue(null).To(); - - SetScript = type.GetField("_setScript", BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(null)!.ToString()!; } public AbpRedisCache(IOptions optionsAccessor) @@ -276,7 +276,7 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems for (var i = 0; i < itemArray.Length; i++) { - tasks[i] = RedisDatabase.ScriptEvaluateAsync(SetScript, new RedisKey[] { Instance + itemArray[i].Key }, + tasks[i] = RedisDatabase.ScriptEvaluateAsync(GetSetScript(), new RedisKey[] { Instance + itemArray[i].Key }, new RedisValue[] { absoluteExpiration?.Ticks ?? NotPresent, @@ -321,4 +321,9 @@ public class AbpRedisCache : RedisCache, ICacheSupportsMultipleItems { return _redisDatabase ??= RedisDatabaseField.GetValue(this) as IDatabase; } + + private string GetSetScript() + { + return SetScriptField.GetValue(this)!.ToString()!; + } } From f2ac06e31eba5c50035986b862fa111881d32427 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 29 May 2023 19:28:21 +0800 Subject: [PATCH 4/4] Update Volo.Abp.Caching.StackExchangeRedis.csproj --- .../Volo.Abp.Caching.StackExchangeRedis.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj b/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj index 8963eb4b54..33ee65ee04 100644 --- a/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj +++ b/framework/src/Volo.Abp.Caching.StackExchangeRedis/Volo.Abp.Caching.StackExchangeRedis.csproj @@ -6,6 +6,7 @@ netstandard2.0;netstandard2.1;net7.0 enable + Nullable Volo.Abp.Caching.StackExchangeRedis Volo.Abp.Caching.StackExchangeRedis $(AssetTargetFallback);portable-net45+win8+wp8+wpa81;