4

当我使用 redis 池时,如何更改 b(redis 命令选择)。

我想从不同的数据库写入主机和读取主机。

例如:

刚才我只有一个redis服务器,所以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)
    {

//这里我要使用的DB号是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)
    {

//这里我要使用的DB号是(day-1)%15

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

编辑:

我找到了一种设置不同数据库的方法,但我觉得这个解决方案不是最好的方法。

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

是否需要锁定线程?当我读或写redis时。恐怕,我在 mutil-thread 有相同的 redis 客户端。那么redis数据库是?

编辑结束

                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();
                };

            };                         
    }

我从池中得到了redisClient,但是redisClient没有changeDB的功能。

所以有人知道如何设置吗?

4

1 回答 1

1

例如:

//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; 
        }
于 2013-11-28T08:03:54.323 回答