diff --git a/src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Extensions/DistributedCacheBuilderExtensions.cs b/src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Extensions/DistributedCacheBuilderExtensions.cs index 3d9df7878..f8b54f65c 100644 --- a/src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Extensions/DistributedCacheBuilderExtensions.cs +++ b/src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Extensions/DistributedCacheBuilderExtensions.cs @@ -1,4 +1,4 @@ -// Copyright (c) MASA Stack All rights reserved. +// Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. // ReSharper disable once CheckNamespace @@ -11,6 +11,7 @@ public static void UseCustomDistributedCache( this DistributedCacheBuilder distributedCacheBuilder, Func func) { + distributedCacheBuilder.Services.Configure(options => { if (options.Options.Any(opt => opt.Name == distributedCacheBuilder.Name)) diff --git a/src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Internal/ServiceCollectionExtensions.Core.cs b/src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Internal/ServiceCollectionExtensions.Core.cs index 67ed3f85b..424714ad8 100644 --- a/src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Internal/ServiceCollectionExtensions.Core.cs +++ b/src/BuildingBlocks/Caching/Masa.BuildingBlocks.Caching/Internal/ServiceCollectionExtensions.Core.cs @@ -22,9 +22,7 @@ public static void TryAddDistributedCache( services.TryAddTransient(); services.TryAddTransient(serviceProvider => { - var cacheClient = serviceProvider.EnableIsolation() ? - serviceProvider.GetRequiredService>().Service : - serviceProvider.GetRequiredService>().Service; + var cacheClient = serviceProvider.GetRequiredService(); return new DefaultDistributedCacheClient(cacheClient); }); services.TryAddTransient(serviceProvider @@ -43,9 +41,7 @@ public static void TryAddMultilevelCache( services.TryAddTransient(); services.TryAddTransient(serviceProvider => { - var cacheClient = serviceProvider.EnableIsolation() ? - serviceProvider.GetRequiredService>().Service : - serviceProvider.GetRequiredService>().Service; + var cacheClient = serviceProvider.GetRequiredService(); return new DefaultMultilevelCacheClient(cacheClient); }); services.TryAddTransient(serviceProvider @@ -65,17 +61,7 @@ private static void AddTypeAlias( private static void AddCaching(this IServiceCollection services) { - services.TryAddSingleton>(serviceProvider => - new SingletonService(serviceProvider.GetRequiredService() - .Create())); - services.TryAddScoped>(serviceProvider => - new ScopedService(serviceProvider.GetRequiredService() - .Create())); - - services.TryAddSingleton>(serviceProvider => - new SingletonService(serviceProvider.GetRequiredService() - .Create())); - services.TryAddScoped>(serviceProvider => - new ScopedService(serviceProvider.GetRequiredService().Create())); + services.TryAddScoped(serviceProvider => serviceProvider.GetRequiredService().Create()); + services.TryAddScoped(serviceProvider => serviceProvider.GetRequiredService().Create()); } } diff --git a/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/Extensions/DistributedCacheBuilderExtensions.cs b/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/Extensions/DistributedCacheBuilderExtensions.cs index 4d942c8e9..0a9586b61 100644 --- a/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/Extensions/DistributedCacheBuilderExtensions.cs +++ b/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/Extensions/DistributedCacheBuilderExtensions.cs @@ -20,29 +20,14 @@ public static void UseStackExchangeRedisCache( JsonSerializerOptions? jsonSerializerOptions = null) { distributedCacheBuilder.Services.AddConfigure(redisSectionName, distributedCacheBuilder.Name); - - distributedCacheBuilder.UseCustomDistributedCache(serviceProvider => - { - var redisConfigurationOptions = ComponentConfigUtils.GetComponentConfigByExecute( - serviceProvider, - distributedCacheBuilder.Name, - redisSectionName, - () => - { - if (serviceProvider.EnableIsolation()) - return serviceProvider.GetRequiredService>() - .Get(distributedCacheBuilder.Name); - - var optionsMonitor = serviceProvider.GetRequiredService>(); - return optionsMonitor.Get(distributedCacheBuilder.Name); - }); - - return new RedisCacheClient( - redisConfigurationOptions, + distributedCacheBuilder.Services.AddScoped(serviceProvider => ConnectionMultiplexer.Connect(GetRedisConfig(serviceProvider, distributedCacheBuilder, redisSectionName).GetAvailableRedisOptions())); + distributedCacheBuilder.UseCustomDistributedCache(serviceProvider => new RedisCacheClient( + serviceProvider.GetRequiredService(), + GetRedisConfig(serviceProvider, distributedCacheBuilder, redisSectionName), serviceProvider.GetService(), jsonSerializerOptions, - serviceProvider.GetRequiredService().Create(distributedCacheBuilder.Name)); - }); + serviceProvider.GetRequiredService().Create(distributedCacheBuilder.Name)) + ); } public static void UseStackExchangeRedisCache( @@ -50,18 +35,16 @@ public static void UseStackExchangeRedisCache( Action action, JsonSerializerOptions? jsonSerializerOptions = null) { - distributedCacheBuilder.UseCustomDistributedCache(serviceProvider => - { - var redisConfigurationOptions = new RedisConfigurationOptions(); - action.Invoke(redisConfigurationOptions); - var distributedCacheClient = new RedisCacheClient( + var redisConfigurationOptions = new RedisConfigurationOptions(); + action.Invoke(redisConfigurationOptions); + distributedCacheBuilder.Services.AddScoped(serviceProvider => ConnectionMultiplexer.Connect(redisConfigurationOptions.GetAvailableRedisOptions())); + distributedCacheBuilder.UseCustomDistributedCache(serviceProvider => new RedisCacheClient( + serviceProvider.GetRequiredService(), redisConfigurationOptions, serviceProvider.GetService(), jsonSerializerOptions, serviceProvider.GetRequiredService().Create(distributedCacheBuilder.Name) - ); - return distributedCacheClient; - }); + )); } public static void UseStackExchangeRedisCache( @@ -69,9 +52,11 @@ public static void UseStackExchangeRedisCache( RedisConfigurationOptions redisConfigurationOptions, JsonSerializerOptions? jsonSerializerOptions = null) { + distributedCacheBuilder.Services.AddScoped(serviceProvider => ConnectionMultiplexer.Connect(redisConfigurationOptions.GetAvailableRedisOptions())); distributedCacheBuilder.UseCustomDistributedCache(serviceProvider => { var distributedCacheClient = new RedisCacheClient( + serviceProvider.GetRequiredService(), redisConfigurationOptions, serviceProvider.GetService(), jsonSerializerOptions, @@ -80,4 +65,22 @@ public static void UseStackExchangeRedisCache( return distributedCacheClient; }); } + + private static RedisConfigurationOptions GetRedisConfig(IServiceProvider serviceProvider, DistributedCacheBuilder distributedCacheBuilder, string redisSectionName) + { + var redisConfigurationOptions = ComponentConfigUtils.GetComponentConfigByExecute( + serviceProvider, + distributedCacheBuilder.Name, + redisSectionName, + () => + { + if (serviceProvider.EnableIsolation()) + return serviceProvider.GetRequiredService>() + .Get(distributedCacheBuilder.Name); + + var optionsMonitor = serviceProvider.GetRequiredService>(); + return optionsMonitor.Get(distributedCacheBuilder.Name); + }); + return redisConfigurationOptions; + } } diff --git a/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClient.cs b/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClient.cs index 4beab37e9..96977edfc 100644 --- a/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClient.cs +++ b/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClient.cs @@ -9,11 +9,12 @@ public class RedisCacheClient : RedisCacheClientBase private readonly ITypeAliasProvider? _typeAliasProvider; public RedisCacheClient( + IConnectionMultiplexer connect, RedisConfigurationOptions redisConfigurationOptions, IFormatCacheKeyProvider? formatCacheKeyProvider = null, JsonSerializerOptions? jsonSerializerOptions = null, ITypeAliasProvider? typeAliasProvider = null) - : base(redisConfigurationOptions, jsonSerializerOptions) + : base(connect, redisConfigurationOptions, jsonSerializerOptions) { _formatCacheKeyProvider = formatCacheKeyProvider ?? new DefaultFormatCacheKeyProvider(); _typeAliasProvider = typeAliasProvider; diff --git a/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClientBase.cs b/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClientBase.cs index 20cf755e2..6a80ebf48 100644 --- a/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClientBase.cs +++ b/src/Contrib/Caching/Distributed/Masa.Contrib.Caching.Distributed.StackExchangeRedis/RedisCacheClientBase.cs @@ -26,14 +26,15 @@ protected IDatabase Db private readonly CacheOptions _globalCacheOptions; protected RedisCacheClientBase( + IConnectionMultiplexer connection, RedisConfigurationOptions redisConfigurationOptions, JsonSerializerOptions? jsonSerializerOptions) : this(redisConfigurationOptions.GlobalCacheOptions, redisConfigurationOptions, jsonSerializerOptions) { var redisConfiguration = redisConfigurationOptions.GetAvailableRedisOptions(); - _connection = ConnectionMultiplexer.Connect(redisConfiguration); + _connection = connection; Subscriber = _connection.GetSubscriber(); - InstanceId = redisConfiguration.InstanceId; + InstanceId = redisConfiguration.InstanceId; } private RedisCacheClientBase( diff --git a/src/Contrib/Caching/Distributed/Tests/Masa.Contrib.Caching.Distributed.StackExchangeRedis.Tests/DistributedCacheClientTest.cs b/src/Contrib/Caching/Distributed/Tests/Masa.Contrib.Caching.Distributed.StackExchangeRedis.Tests/DistributedCacheClientTest.cs index c2fe92854..cbd81a18a 100644 --- a/src/Contrib/Caching/Distributed/Tests/Masa.Contrib.Caching.Distributed.StackExchangeRedis.Tests/DistributedCacheClientTest.cs +++ b/src/Contrib/Caching/Distributed/Tests/Masa.Contrib.Caching.Distributed.StackExchangeRedis.Tests/DistributedCacheClientTest.cs @@ -1,4 +1,4 @@ -// Copyright (c) MASA Stack All rights reserved. +// Copyright (c) MASA Stack All rights reserved. // Licensed under the MIT License. See LICENSE.txt in the project root for license information. namespace Masa.Contrib.Caching.Distributed.StackExchangeRedis.Tests; @@ -7,14 +7,15 @@ namespace Masa.Contrib.Caching.Distributed.StackExchangeRedis.Tests; public class DistributedCacheClientTest : TestBase { private RedisCacheClient _distributedCacheClient; + private ConnectionMultiplexer _connection; private IDatabase _database; [TestInitialize] public void Initialize() { - _distributedCacheClient = new RedisCacheClient(GetConfigurationOptions()); - - _database = ConnectionMultiplexer.Connect(GetConfigurationOptions()).GetDatabase(); + _connection = ConnectionMultiplexer.Connect(GetConfigurationOptions()); + _distributedCacheClient = new RedisCacheClient(_connection, GetConfigurationOptions()); + _database = _connection.GetDatabase(); _distributedCacheClient.Set("test_caching", "1"); _distributedCacheClient.Set("test_caching_2", Guid.Empty.ToString()); @@ -96,8 +97,9 @@ public async Task SetAndSpecifyTimeAsyncAndUseGlobalOptions(string key, string v { var globalRedisConfigurationOptions = GetConfigurationOptions(); globalRedisConfigurationOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60); - var distributedCacheClient = new RedisCacheClient(globalRedisConfigurationOptions); - var database = (await ConnectionMultiplexer.ConnectAsync(globalRedisConfigurationOptions)).GetDatabase(); + var connection = await ConnectionMultiplexer.ConnectAsync(globalRedisConfigurationOptions); + var distributedCacheClient = new RedisCacheClient(connection, globalRedisConfigurationOptions); + var database = connection.GetDatabase(); await distributedCacheClient.SetAsync(key, value); CheckLifeCycle(database, key, 55, 60); @@ -110,8 +112,9 @@ public void SetAndSpecifyTimeAndUseGlobalOptions(string key, string value) { var globalRedisConfigurationOptions = GetConfigurationOptions(); globalRedisConfigurationOptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(60); - var distributedCacheClient = new RedisCacheClient(globalRedisConfigurationOptions); - var database = ConnectionMultiplexer.Connect(globalRedisConfigurationOptions).GetDatabase(); + var connection = ConnectionMultiplexer.Connect(globalRedisConfigurationOptions); + var distributedCacheClient = new RedisCacheClient(connection, globalRedisConfigurationOptions); + var database = connection.GetDatabase(); distributedCacheClient.Set(key, value); CheckLifeCycle(database, key, 55, 60); @@ -528,7 +531,7 @@ public void TestGetKeys(string keyPattern, int count) public void TestGetKeys2() { string key = "te" + Guid.NewGuid(); - var distributedCacheClient = new RedisCacheClient(new RedisConfigurationOptions() + var distributedCacheClient = new RedisCacheClient(_connection, new RedisConfigurationOptions() { GlobalCacheOptions = new CacheOptions() { @@ -851,7 +854,7 @@ public void TestExists() { CacheKeyType = CacheKeyType.TypeName }; - var distributedCacheClient = new RedisCacheClient(configurationOptions); + var distributedCacheClient = new RedisCacheClient(_connection, configurationOptions); var key = "redis.exist"; distributedCacheClient.Set(key, "1"); Assert.IsFalse(distributedCacheClient.Exists(key)); @@ -874,7 +877,7 @@ public async Task TestExistsAsync() { CacheKeyType = CacheKeyType.TypeName }; - var distributedCacheClient = new RedisCacheClient(configurationOptions); + var distributedCacheClient = new RedisCacheClient(_connection, configurationOptions); var key = "redis.exist"; await distributedCacheClient.SetAsync(key, "1"); Assert.IsFalse(await distributedCacheClient.ExistsAsync(key)); diff --git a/src/Contrib/Data/IdGenerator/Snowflake/Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis/BaseRedis.cs b/src/Contrib/Data/IdGenerator/Snowflake/Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis/BaseRedis.cs index 3cf317cac..060744f76 100644 --- a/src/Contrib/Data/IdGenerator/Snowflake/Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis/BaseRedis.cs +++ b/src/Contrib/Data/IdGenerator/Snowflake/Masa.Contrib.Data.IdGenerator.Snowflake.Distributed.Redis/BaseRedis.cs @@ -11,9 +11,9 @@ public class BaseRedis public BaseRedis(RedisConfigurationOptions redisOptions) { - DistributedCacheClient = new RedisCacheClient(redisOptions); var options = (ConfigurationOptions)redisOptions; Connection = ConnectionMultiplexer.Connect(options); + DistributedCacheClient = new RedisCacheClient(Connection, redisOptions); Database = Connection.GetDatabase(options.DefaultDatabase ?? 0); } }