Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Extensions.Caching.Redis select different database than db0

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)

enter image description here

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:

enter image description here

What do i have to configure to use e.g. db6?

Do i have to use Stackexchange.Redis for this?

like image 733
Daniel Avatar asked Feb 04 '23 02:02

Daniel


1 Answers

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";
});
like image 161
hardkoded Avatar answered Feb 16 '23 04:02

hardkoded