Microsoft.Extensions.Caching.Redis selects a different database than db0

a question about understanding which redis database is used and how it can be configured.

I have a default ASP.NET web application and a local redis server configured by default (containing 15 databases)

enter image description here

In the package management console, I installed:

Install-Package Microsoft.Extensions.Caching.Redis

Redis is configured in Startup.cs as follows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    services.AddDistributedRedisCache(option =>
    {
        option.Configuration = "127.0.0.1";
        option.InstanceName = "master";
    });
}

The code for reading and writing values ​​to the cache is taken from the documents:

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 db0 database by default, regardless of what I'm setting up.

eg. using this configuration:

services.AddDistributedRedisCache(option =>
{
    option.Configuration = "127.0.0.1";
    option.InstanceName = "db6";
});

leads to:

enter image description here

What do I need to configure for use, for example? DB6?

Do I need to use Stackexchange.Redis for this?

+4
source share
1 answer

Microsoft.Extensions.Caching.Redis Stackexchange.Redis Redis.

Configuration StackExchange.Redis. , :

services.AddDistributedRedisCache(option =>
{
    option.Configuration = "127.0.0.1;defaultDatabase=4";
    option.InstanceName = "master";
});
+5

Source: https://habr.com/ru/post/1693013/


All Articles