在 .NET Core 依赖注入中,`StackExchange.Redis.ConnectionMultiplexer` 应该是 `AddSingleton` 还是 `AddScope`?
Should `StackExchange.Redis.ConnectionMultiplexer` be `AddSingleton` or `AddScope` in .NET Core dependency injection?
我正在使用 StackExchange.Redis
添加到 .NET Core 的 Redis 连接,它目前看起来像这样:
public static IServiceCollection AddRedisMultiplexer(
this IServiceCollection services,
Func<ConfigurationOptions> getOptions = null)
{
// Get the options or assume localhost, as these will be set in Startup.ConfigureServices assume they won't change
var options = getOptions?.Invoke() ?? ConfigurationOptions.Parse("localhost");
// The Redis is a singleton, shared as much as possible.
return services.AddSingleton<IConnectionMultiplexer>(provider => ConnectionMultiplexer.Connect(options));
}
然后在Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddRedisMultiplexer(() =>
ConfigurationOptions.Parse(Configuration["ConnectionStrings:Redis"]));
...
这意味着我可以在任何地方使用 IConnectionMultiplexer
进行依赖注入。
我的问题是:ConnectionMultiplexer
是 designed to be reused,所以我使用 AddSingleton
为整个应用程序保留一个实例。但是,我也可以使用 AddScoped
在请求期间使用一个。哪个更好?为什么?
应用程序的预期负载是多少?如果你有很多并发,我认为使用 AddScoped
意味着很多不必要的负担来为每个请求启动和关闭连接。
此外,恕我直言,这些观察表明您应该使用 AddSingleton
(...) it is exceptionally rare that you would want to use a
ConnectionMultiplexer briefly, as the idea is to re-use this object.
Another common use of redis is as a pub/sub message distribution tool;
this is also simple, and in the event of connection failure, the
ConnectionMultiplexer will handle all the details of re-subscribing to
the requested channels.
此外,只有一个 ConnectionMultiplexer
实例(恕我直言),您将节省内存。
我正在使用 StackExchange.Redis
添加到 .NET Core 的 Redis 连接,它目前看起来像这样:
public static IServiceCollection AddRedisMultiplexer(
this IServiceCollection services,
Func<ConfigurationOptions> getOptions = null)
{
// Get the options or assume localhost, as these will be set in Startup.ConfigureServices assume they won't change
var options = getOptions?.Invoke() ?? ConfigurationOptions.Parse("localhost");
// The Redis is a singleton, shared as much as possible.
return services.AddSingleton<IConnectionMultiplexer>(provider => ConnectionMultiplexer.Connect(options));
}
然后在Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddRedisMultiplexer(() =>
ConfigurationOptions.Parse(Configuration["ConnectionStrings:Redis"]));
...
这意味着我可以在任何地方使用 IConnectionMultiplexer
进行依赖注入。
我的问题是:ConnectionMultiplexer
是 designed to be reused,所以我使用 AddSingleton
为整个应用程序保留一个实例。但是,我也可以使用 AddScoped
在请求期间使用一个。哪个更好?为什么?
应用程序的预期负载是多少?如果你有很多并发,我认为使用 AddScoped
意味着很多不必要的负担来为每个请求启动和关闭连接。
此外,恕我直言,这些观察表明您应该使用 AddSingleton
(...) it is exceptionally rare that you would want to use a ConnectionMultiplexer briefly, as the idea is to re-use this object.
Another common use of redis is as a pub/sub message distribution tool; this is also simple, and in the event of connection failure, the ConnectionMultiplexer will handle all the details of re-subscribing to the requested channels.
此外,只有一个 ConnectionMultiplexer
实例(恕我直言),您将节省内存。