a question on understanding which redis database is used and how it can be configured.
i have a default ASP.NET Core Web Application and a default configured local redis-server (containing 15 databases)
Over Package Management Console i have installed:
Install-Package Microsoft.Extensions.Caching.Redis
Redis is configured in Startup.cs like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDistributedRedisCache(option =>
{
option.Configuration = "127.0.0.1";
option.InstanceName = "master";
});
}
The code to read and write values into the cache is taken from the docs:
var cacheKey = "TheTime";
var existingTime = _distributedCache.GetString(cacheKey);
if (!string.IsNullOrEmpty(existingTime))
{
return "Fetched from cache : " + existingTime;
}
else
{
existingTime = DateTime.UtcNow.ToString();
_distributedCache.SetString(cacheKey, existingTime);
return "Added to cache : " + existingTime;
}
But this code only uses the default database db0 no matter what i configure.
E.g. using this configuration:
services.AddDistributedRedisCache(option =>
{
option.Configuration = "127.0.0.1";
option.InstanceName = "db6";
});
leads to:
What do i have to configure to use e.g. db6?
Do i have to use Stackexchange.Redis for this?
Microsoft.Extensions.Caching.Redis is using Stackexchange.Redis to connect to Redis.
The Configuration
string is documented on StackExchange.Redis. That said, you should be able to do:
services.AddDistributedRedisCache(option =>
{
option.Configuration = "127.0.0.1;defaultDatabase=4";
option.InstanceName = "master";
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With