From 4e707e952ec8be4e95c46d1b6d91f0eb564d29a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 31 Oct 2023 09:28:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0RedisCacheProvider=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Redis/Services/RedisCacheProvider.cs | 18 +++++++-------- XUnitTest/Services/RedisCacheProviderTests.cs | 23 +++++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 XUnitTest/Services/RedisCacheProviderTests.cs 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); + } +}