diff --git a/NewLife.Redis/Services/RedisCacheProvider.cs b/NewLife.Redis/Services/RedisCacheProvider.cs index 89b6ed9..6af7b4b 100644 --- a/NewLife.Redis/Services/RedisCacheProvider.cs +++ b/NewLife.Redis/Services/RedisCacheProvider.cs @@ -16,11 +16,11 @@ namespace NewLife.Caching.Services; public class RedisCacheProvider : CacheProvider { #region 属性 - private FullRedis _redis; - private FullRedis _redisQueue; + private FullRedis? _redis; + private FullRedis? _redisQueue; /// 队列 - public FullRedis RedisQueue { get => _redisQueue; set => _redisQueue = value; } + public FullRedis? RedisQueue { get => _redisQueue; set => _redisQueue = value; } #endregion #region 构造 @@ -37,7 +37,7 @@ public RedisCacheProvider(IServiceProvider serviceProvider) /// 初始化 /// /// - public void Init(IConfigProvider config, IServiceProvider serviceProvider = null) + public void Init(IConfigProvider config, IServiceProvider? serviceProvider = null) { var cacheConn = config["RedisCache"]; var queueConn = config["RedisQueue"]; @@ -49,8 +49,8 @@ public void Init(IConfigProvider config, IServiceProvider serviceProvider = null { _redis = new FullRedis(serviceProvider, "RedisCache") { - Log = serviceProvider.GetService(), - Tracer = serviceProvider.GetService(), + Log = serviceProvider.GetRequiredService(), + Tracer = serviceProvider.GetRequiredService(), }; } else @@ -68,8 +68,8 @@ public void Init(IConfigProvider config, IServiceProvider serviceProvider = null { _redisQueue = new FullRedis(serviceProvider, "RedisQueue") { - Log = serviceProvider.GetService(), - Tracer = serviceProvider.GetService(), + Log = serviceProvider.GetRequiredService(), + Tracer = serviceProvider.GetRequiredService(), }; } else @@ -90,7 +90,7 @@ public void Init(IConfigProvider config, IServiceProvider serviceProvider = null /// 主题 /// 消费组。未指定消费组时使用简单队列(如RedisQueue),指定消费组时使用完整队列(如RedisStream) /// - public override IProducerConsumer GetQueue(String topic, String group = null) + public override IProducerConsumer GetQueue(String topic, String? group = null) { if (_redisQueue != null) { diff --git a/XUnitTest/Services/RedisCacheProviderTests.cs b/XUnitTest/Services/RedisCacheProviderTests.cs new file mode 100644 index 0000000..1d335a2 --- /dev/null +++ b/XUnitTest/Services/RedisCacheProviderTests.cs @@ -0,0 +1,23 @@ +using System; +using NewLife.Caching.Services; +using NewLife.Model; +using Xunit; + +namespace XUnitTest.Services; + +public class RedisCacheProviderTests +{ + [Fact] + public void Ctor() + { + var sp = ObjectContainer.Provider; + + var provider = new RedisCacheProvider(sp); + Assert.NotNull(provider.Cache); + Assert.NotNull(provider.InnerCache); + Assert.Null(provider.RedisQueue); + + var q = provider.GetQueue("test"); + Assert.NotNull(q); + } +}