Kunal Chiddarwar
Kunal Chiddarwar

Reputation: 41

How to connect to other databases (1 to 15) than default database (0) in stackexchange redis?

I have 3 applications pointing to single redis endpoint. I want to use separate redis database per application. But when I am trying to set defaultDatabase to 1 or 2 in ConfigurationOption it is still using database 0. I am using stackexchange redis. Code is as given below :

  var configurationOptions = new ConfigurationOptions
        {
            EndPoints = { "myredis.redis.cache.windows.net" },
            Password = "xxxxxxxxxxxxxxxxxxx",
            Ssl = true,
            ConfigurationChannel = "MyRedis",
            ChannelPrefix = "MR",
            DefaultDatabase = 1,
        };

        var connectionMultiplexer = ConnectionMultiplexer.Connect(configurationOptions);

        var cacheClient = new StackExchangeRedisCacheClient(connectionMultiplexer, new JilSerializer(), database: 1);

Now adding new key to cache using ICacheClient

 var user = new User()
 {
     Id = 100,
     Name = "John Doe"
 };

var response = cacheClient.Add("mykey", user);

Then retrieving added key from cache

var addedUser= cacheClient.Get<string>("mykey");

Is there anything I am doing wrong? Ideally as per stated in configuration options it should store keys to db1 but it is storing to db0 instead.

Upvotes: 0

Views: 4334

Answers (1)

Adam
Adam

Reputation: 2570

When you retrieve the database object, you should specify the database number. https://stackexchange.github.io/StackExchange.Redis/Basics.html#using-a-redis-database

Upvotes: 0

Related Questions