Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(multilevelcache): fix injected by singleton to scope #731

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +11,7 @@ public static void UseCustomDistributedCache(
this DistributedCacheBuilder distributedCacheBuilder,
Func<IServiceProvider, IManualDistributedCacheClient> func)
{

distributedCacheBuilder.Services.Configure<DistributedCacheFactoryOptions>(options =>
{
if (options.Options.Any(opt => opt.Name == distributedCacheBuilder.Name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ public static void TryAddDistributedCache(
services.TryAddTransient<IDistributedCacheClientFactory, DefaultDistributedCacheClientFactory>();
services.TryAddTransient<IManualDistributedCacheClient>(serviceProvider =>
{
var cacheClient = serviceProvider.EnableIsolation() ?
serviceProvider.GetRequiredService<ScopedService<IManualDistributedCacheClient>>().Service :
serviceProvider.GetRequiredService<SingletonService<IManualDistributedCacheClient>>().Service;
var cacheClient = serviceProvider.GetRequiredService<IManualDistributedCacheClient>();
return new DefaultDistributedCacheClient(cacheClient);
});
services.TryAddTransient<IDistributedCacheClient>(serviceProvider
Expand All @@ -43,9 +41,7 @@ public static void TryAddMultilevelCache(
services.TryAddTransient<IMultilevelCacheClientFactory, DefaultMultilevelCacheClientFactory>();
services.TryAddTransient<IManualMultilevelCacheClient>(serviceProvider =>
{
var cacheClient = serviceProvider.EnableIsolation() ?
serviceProvider.GetRequiredService<ScopedService<IManualMultilevelCacheClient>>().Service :
serviceProvider.GetRequiredService<SingletonService<IManualMultilevelCacheClient>>().Service;
var cacheClient = serviceProvider.GetRequiredService<IManualMultilevelCacheClient>();
return new DefaultMultilevelCacheClient(cacheClient);
});
services.TryAddTransient<IMultilevelCacheClient>(serviceProvider
Expand All @@ -65,17 +61,7 @@ private static void AddTypeAlias(

private static void AddCaching(this IServiceCollection services)
{
services.TryAddSingleton<SingletonService<IManualDistributedCacheClient>>(serviceProvider =>
new SingletonService<IManualDistributedCacheClient>(serviceProvider.GetRequiredService<IDistributedCacheClientFactory>()
.Create()));
services.TryAddScoped<ScopedService<IManualDistributedCacheClient>>(serviceProvider =>
new ScopedService<IManualDistributedCacheClient>(serviceProvider.GetRequiredService<IDistributedCacheClientFactory>()
.Create()));

services.TryAddSingleton<SingletonService<IManualMultilevelCacheClient>>(serviceProvider =>
new SingletonService<IManualMultilevelCacheClient>(serviceProvider.GetRequiredService<IMultilevelCacheClientFactory>()
.Create()));
services.TryAddScoped<ScopedService<IManualMultilevelCacheClient>>(serviceProvider =>
new ScopedService<IManualMultilevelCacheClient>(serviceProvider.GetRequiredService<IMultilevelCacheClientFactory>().Create()));
services.TryAddScoped(serviceProvider => serviceProvider.GetRequiredService<IDistributedCacheClientFactory>().Create());
services.TryAddScoped(serviceProvider => serviceProvider.GetRequiredService<IMultilevelCacheClientFactory>().Create());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,27 @@ public static void UseStackExchangeRedisCache(
JsonSerializerOptions? jsonSerializerOptions = null)
{
distributedCacheBuilder.Services.AddConfigure<RedisConfigurationOptions>(redisSectionName, distributedCacheBuilder.Name);

distributedCacheBuilder.UseCustomDistributedCache(serviceProvider =>
{
var redisConfigurationOptions = ComponentConfigUtils.GetComponentConfigByExecute(
serviceProvider,
distributedCacheBuilder.Name,
redisSectionName,
() =>
{
if (serviceProvider.EnableIsolation())
return serviceProvider.GetRequiredService<IOptionsSnapshot<RedisConfigurationOptions>>()
.Get(distributedCacheBuilder.Name);

var optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<RedisConfigurationOptions>>();
return optionsMonitor.Get(distributedCacheBuilder.Name);
});

return new RedisCacheClient(
redisConfigurationOptions,
distributedCacheBuilder.UseCustomDistributedCache(serviceProvider => new RedisCacheClient(
GetRedisConfig(serviceProvider, distributedCacheBuilder, redisSectionName),
serviceProvider.GetService<IFormatCacheKeyProvider>(),
jsonSerializerOptions,
serviceProvider.GetRequiredService<ITypeAliasFactory>().Create(distributedCacheBuilder.Name));
});
serviceProvider.GetRequiredService<ITypeAliasFactory>().Create(distributedCacheBuilder.Name))
);
}

public static void UseStackExchangeRedisCache(
this DistributedCacheBuilder distributedCacheBuilder,
Action<RedisConfigurationOptions> 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.UseCustomDistributedCache(serviceProvider => new RedisCacheClient(
redisConfigurationOptions,
serviceProvider.GetService<IFormatCacheKeyProvider>(),
jsonSerializerOptions,
serviceProvider.GetRequiredService<ITypeAliasFactory>().Create(distributedCacheBuilder.Name)
);
return distributedCacheClient;
});
));
}

public static void UseStackExchangeRedisCache(
Expand All @@ -80,4 +59,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<IOptionsSnapshot<RedisConfigurationOptions>>()
.Get(distributedCacheBuilder.Name);

var optionsMonitor = serviceProvider.GetRequiredService<IOptionsMonitor<RedisConfigurationOptions>>();
return optionsMonitor.Get(distributedCacheBuilder.Name);
});
return redisConfigurationOptions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class RedisCacheClient : RedisCacheClientBase
private readonly IFormatCacheKeyProvider _formatCacheKeyProvider;
private readonly ITypeAliasProvider? _typeAliasProvider;

public RedisCacheClient(
public RedisCacheClient(
RedisConfigurationOptions redisConfigurationOptions,
IFormatCacheKeyProvider? formatCacheKeyProvider = null,
JsonSerializerOptions? jsonSerializerOptions = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +13,6 @@ public class DistributedCacheClientTest : TestBase
public void Initialize()
{
_distributedCacheClient = new RedisCacheClient(GetConfigurationOptions());

_database = ConnectionMultiplexer.Connect(GetConfigurationOptions()).GetDatabase();

_distributedCacheClient.Set("test_caching", "1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(redisOptions);
Database = Connection.GetDatabase(options.DefaultDatabase ?? 0);
}
}
Loading