So according to StackExchange.Redis docs, I am to re-use IConnectionMultiplexer.
services.AddSingleton<IConnectionMultiplexer>(
ConnectionMultiplexer
.Connect($"{configsOptions.RedisUrl},password={secretsOptions.RedisPassword}"));
But then I found that in my redis service method, I'd have to inject the IConnectionMultiplexer and then connect to the database like this:
public Task GetSomethingFromRedis(string key)
{
IDatabase db = connectionMultiplexer.GetDatabase();
string value = db.StringGet(key);
}
Instead of calling connectionMultiplexer.GetDatabase() every time, is OK for me to have a Singleton of the IDatabase object, injected like this? Or is this a really bad idea?
services.AddSingleton<IDatabase>(cfg =>
{
var redisConnection =
ConnectionMultiplexer
.Connect($"{configsOptions.RedisUrl},password={secretsOptions.RedisPassword}");
return redisConnection.GetDatabase();
});