yii2-redis

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);
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • linux 启动 redis:cd /usr/local/redis-3.2.0src/redis-server ...
    晏子小七阅读 9,879评论 0 11
  • 本文将从Redis的基本特性入手,通过讲述Redis的数据结构和主要命令对Redis的基本能力进行直观介绍。之后概...
    kelgon阅读 61,281评论 23 625
  • 人的幸福,不是吃的好,穿的好,住的好就叫幸福!是拥有一个爱自己懂自己的人,不管他有多大能力,总是把最好最多的留给你...
    憨猪儿阅读 229评论 2 1
  • 《人类简史》讲述了与虫草无异的人类从普通物种演化成如神般顶级物种的过程。全书结构:​认知革命(1~4章),农业革命...
    沧海浩歌阅读 730评论 0 3