Thursday, February 22, 2018

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

Leave a Comment

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?

1 Answers

Answers 1

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 Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment