How to change redis DB that redis client from redis pool

How can I change b (redis command select) when I used the redis pool.

I want to record the host and read the host from different databases.

eg:

only now i have only one redis server so readWriteHosts = readOnlyHosts

pool = RedisDao.CreateManager(hostIp, hostIp); public static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts) { return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig { MaxWritePoolSize = 50,// MaxReadPoolSize = 5,// AutoStart = true, }); } public RedisDB ReadRedisForModel(String ID) { 

// here I want to use the DB number day% 15

  using (var redis = pool.GetClient()) { RedisDB model = new RedisDB(); Dictionary<string, string> dic = redis.GetAllEntriesFromHash(keyPrefix + ID); model.ID = ID;//Int32.Parse(ids[i]); return model; } } public void WriteRedis(RedisDB model) { 

// here I want to use the database number (day-1)% 15

  using (var redis = pool.GetClient()) { 

EDIT:

I find a way to install different databases, but I think this solution is not the best way.

  if(redis is RedisClient) { long db = redis.DB;//db always = 0; ((RedisClient)redis).ChangeDB((day-1)%15); } 

Do I need to block a thread? when I read or write redis. I'm afraid I have the same redis client in mutil-thread. then redis db is?

Change end

  int time = DateTimeUtil.ConvertDateTimeInt(DateTime.Now); model.ID = time + redis.Increment(incrementKey, 1) + "";//.Incr("ID"); using (var pip = redis.CreatePipeline()) { pip.QueueCommand(r => r.AddItemToList(primaryKey, model.ID + "")); pip.Flush(); }; }; } 

I got redisClient from the pool, but redisClient does not have changeDB function.

So will anyone tell me how to install it?

+4
source share
1 answer

eg:

 //write bool IsNeedChangeDB=true; int WriteDBNumber=3 public static PooledRedisClientManager pool = RedisDao.CreateManager(hostIp, hostIp); using (var redis = pool.GetClient()) { if (redis is RedisClient && IsNeedChangeDB) { if (redis.Db != this.WriteDBNumber) { ((RedisClient)redis).ChangeDb(this.WriteDBNumber); } else { Trace.WriteLine("it is a test" + redis.Host); } } redis.Set<string>("key","value"); } int ReadDBNumber=3; //read protected IRedisClient GetRedisClient() { var redis = pool.GetClient(); if (redis is RedisClient && IsNeedChangeDB) { if (redis.Db != this.ReadDBNumber) ((RedisClient)redis).ChangeDb(this.ReadDBNumber); } return redis; } 
+1
source

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


All Articles