美文网首页
yii2-redis

yii2-redis

作者: wuxuan94 | 来源:发表于2017-09-26 19:20 被阅读0次

    1.是否强制使用主redis连接

    private static $_isMaster = false;
    

    2.设置分库值

    public function setSharded($shardedValue, $configKey = 'user') {
            //随机选择公共库
    
            if($configKey == 'public' && $shardedValue === null) {
                empty($this->_rand) && $this->_rand = rand(1, 2560);
                $shardedValue = $this->_rand;
            }
            
            if( null !== $shardedValue && null !== $configKey ){
                $this->sharded = new CRedisSharded($shardedValue, $configKey);
                }
            $this->setIndex($shardedValue);
            return $this;
        }
    

    3.访问CRedisSharded类设置分库值并选择连接配置

    public function __construct($value, $configKey) {
            $this->set($value, $configKey);
        }
    

    4.根据分库值获取redis连接

    public function getRedisConnection($isMaster = true) {
            if(empty($this->_sharded)) {
                if(null === self::$_connection) {
                    $config = $this->config->default;
                    self::$_connection = $this->createRedisConnection($config);
                }
                return self::$_connection;
            }
            
            return $isMaster ? $this->getMaster() : $this->getSlave();
        }
    

    配置分库值->获取分库连接or未配置分库值->获取主库连接
    5.连接到redis

    public function connect() {
            if($this->_isConnect) return;
            
            self::trace('Opened redis connect: '.$this->_config['host'].':'.$this->_config['port'], __METHOD__.':'.__LINE__);
            
            try{
                $this->_handler = new Redis();
                $this->_handler->connect($this->_config['host'], $this->_config['port'], $this->_config['timeOut']);
                $this->_config['passwd'] && $this->_handler->auth($this->_config['passwd']);
                $this->_config['serializer'] && $this->_handler->setOption(Redis::OPT_SERIALIZER, $this->_config['serializer']);
                $this->_config['dbIndex'] && $this->_handler->select($this->_config['dbIndex']);
                $this->_isConnect = true;
            } catch(Exception $e) {
                throw new CRedisException("Connect Redis failed: ".$this->_config['host'].':'.$this->_config['port']);
            }
        }
    

    6.执行redis命令

    public function exec($method, $args) {
            $this->_redis->connect();
            return call_user_func_array(array($this->_redis->handler, $method), $args);
        }
    

    7.判断主从进行读写分离

    public function call($method, $args) {
            self::trace('call: '.$method.', arguments: '.var_export($args, true), __METHOD__.':'.__LINE__);
            $isMaster = CRedisManager::isMaster() || !$this->config->enableSlave || !$this->isReadOperation($method);
            $this->_redis = CRedisManager::instance()->setSharded($this->_sharded)->getRedisConnection($isMaster);
            return $this->exec($method, $args);
        }
    

    相关文章

      网友评论

          本文标题:yii2-redis

          本文链接:https://www.haomeiwen.com/subject/awosextx.html