thinkphp5.1redis主从配置

项目要求的大概意思是,redis主从读写分离,写要在主机上执行,读要在从机上执行,主机宕机了之后,哨兵模式自动选择一台从机,这一切操作都不能影响前台php业务逻辑的操作

一环境配置

首先需要再服务器上配置开启三个redis服务模拟三台redis服务器,端口分别是6379,6380,6381(主:6379,从:6380,6381),并开启哨兵模式(关于如何开启,参考b站狂神说的redis精讲-哨兵模式详解)

主要修改部分

port:6380
pidfile:"/var/run/redis_6380.pid"
logfile “/var/log/redis/redis80.log”
dbfilename “dump80.rdb”

相关命令

启动
/usr/bin/redis-server /etc/redisconf/redis.conf
/usr/bin/redis-server /etc/redisconf/redis80.conf
/usr/bin/redis-server /etc/redisconf/redis81.conf

连接
/usr/bin/redis-cli -h 127.0.0.1 -p 6379
/usr/bin/redis-cli -h 127.0.0.1 -p 6380
/usr/bin/redis-cli -h 127.0.0.1 -p 6381

查看主从信息
info replication

配置主从
slaveof 127.0.0.1 6379

主从切换(手动将从节点设置成主节点)
redis-cli -h 127.0.0.1 -p 6379 slaveof no one

二 项目相关文件

1 项目config目录下新建redis.php、

return [
    'host' => '127.0.0.1,127.0.0.1,127.0.0.1',
    'port' => '6379,6380,6381'
];

2 extend文件夹下新建文件夹和文件redis/redis.php

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/23
 * Time: 18:56
 */

namespace Redis;

use think\Exception;
use think\exception\ClassNotFoundException;
use think\Log;

class Redis
{

    static protected $instance;
    protected $index = 0;
    protected $port = 6379;
    protected $host = "127.0.0.1";
    protected $options;
    protected $handler;


    public function __construct($options=[]){
        if ( !extension_loaded('redis') ) {
            E(L('_NOT_SUPPERT_').':redis');
        }
        if(empty($options)) {
            $options = array (
                'host'          => config('redis.host') ? config('redis.host') : '127.0.0.1',
                'port'          => config('redis.port') ? config('redis.port') : 6379,
                'timeout'       => config('redis.timeout') ? config('redis.timeout') : false,
                'persistent'    => config('redis.persistent') ? config('redis.persistent') : false,
                'auth'          => config('redis.password') ? config('redis.password') : false,
            );
        }
        $options['host'] = explode(',', $options['host']);

        $options['port'] = explode(',', $options['port']);
        $options['auth'] = explode(',', $options['auth']);
        foreach ($options['host'] as $key=>$value) {
            if (!isset($options['port'][$key])) {
                $options['port'][$key] = $options['port'][0];
            }
            if (!isset($options['auth'][$key])) {
                $options['auth'][$key] = $options['auth'][0];
            }
        }
        $this->options =  $options;
        $expire = config('redisd.expire');
        $this->options['expire'] =  isset($expire) ? (int)$expire : (int)ini_get('session.gc_maxlifetime');;
        $this->options['prefix'] =  isset($options['prefix']) ?  $options['prefix']  :   config('SESSION_PREFIX');
        $this->handler  = new \Redis;
    }

    /**
     * 连接Redis服务端
     * @access public
     * @param bool $is_master : 是否连接主服务器
     */
    public function connect($is_master = true) {

       //主要是这个方法 判断主从就是在这里执行并连接的
        $func = $this->options['persistent'] ? 'pconnect' : 'connect';
        for($i=0;$i<=2;$i++){
            echo $i;
            try {
                if ($this->options['timeout'] === false) {

                    $result = $this->handler->$func($this->options['host'][$i], $this->options['port'][$i]);
                    //if (!$result) throw new ClassNotFoundException('Redis Error', 100);
                } else {
                    $result = $this->handler->$func($this->options['host'][$i], $this->options['port'][$i], $this->options['timeout']);
                   // if (!$result) throw new ClassNotFoundException('Redis Error', 101);
                }
                if ($this->options['auth'][$i]) {
                    $result = $this->handler->auth($this->options['auth'][$i]);
                }
                if (!$result) throw new ClassNotFoundException('Redis Error', 102);
                if($is_master&&$this->handler->info('replication')['role']=='master'){

                   break;
                }
                if(!$is_master&&($this->handler->info('replication')['role']=='slave')){
                    break;
                }
                $this->handler->close();
            } catch ( \Exception $e ) {
                exit('Error Message:'.$e->getMessage().'<br>Error Code:'.$e->getCode().'');
            }

        }

    }
    /**
     * Power: Mikkle
     * Email:776329498@qq.com
     * @param array $options
     * @return Redis
     */
    public static function instance($options = [])
    {
        return new Redis($options);
    }

    public function getKeys($key = '*')
    {
        $this->connect(false);
        return $this->handler->getKeys($key);
    }


    public function setExpire($key, $time = 0)
    {

        if (!$key) {
            return false;
        }
        $this->connect(true);

        switch (true) {
            case ($time == 0):
                return $this->handler->expire($key, 0);
                break;
            case ($time > time()):
                $this->handler->expireAt($key, $time);
                break;
            default:
                return $this->handler->expire($key, $time);
        }
    }


    /*------------------------------------start 1.string结构----------------------------------------------------*/
    /**
     * 增,设置值  构建一个字符串
     * @param string $key KEY名称
     * @param string $value 设置值
     * @param int $timeOut 时间  0表示无过期时间
     * @return true【总是返回true】
     */
    public function set($key, $value, $timeOut = 0)
    {
        $this->connect(true);
        $setRes = $this->handler->set($key, $value);
        if ($timeOut > 0) $this->handler->expire($key, $timeOut);
        return $setRes;
    }

    /**
     * 查,获取 某键对应的值,不存在返回false
     * @param $key ,键值
     * @return bool|string ,查询成功返回信息,失败返回false
     */
    public function get($key)
    {
        $this->connect(false);
        $setRes = $this->handler->get($key);//不存在返回false
        if ($setRes === 'false') {
            return false;
        }
        return $setRes;
    }
    /*------------------------------------1.end string结构----------------------------------------------------*/


    /*------------------------------------2.start list结构----------------------------------------------------*/
    /**
     * 增,构建一个列表(先进后去,类似栈)
     * @param String $key KEY名称
     * @param string $value 值
     * @param $timeOut |num  过期时间
     */
    public function lPush($key, $value, $timeOut = 0)
    {
//          echo "$key - $value \n";
        $this->connect(true);

        $re = $this->handler->LPUSH($key, $value);

        if ($timeOut > 0) $this->handler->expire($key, $timeOut);
        return $re;
    }

    /**
     * 增,构建一个列表(先进先去,类似队列)
     * @param string $key KEY名称
     * @param string $value 值
     * @param $timeOut |num  过期时间
     */
    public function rPush($key, $value, $timeOut = 0)
    {
        $this->connect(true);

        $re = $this->handler->RPUSH($key, $value);
        if ($timeOut > 0) $this->handler->expire($key, $timeOut);
        return $re;
    }

    /**
     * 查,获取所有列表数据(从头到尾取)
     * @param string $key KEY名称
     * @param int $head 开始
     * @param int $tail 结束
     */
    public function lRanges($key, $head, $tail)
    {    $this->connect(false);
        return $this->handler->lrange($key, $head, $tail);
    }

    /**
     * Power by Mikkle
     * QQ:776329498
     * @param $key
     * @return mixed
     */

    public function rPop($key)
    {  $this->connect(true);
        $this->handler->rPop($key);

    }

    public function lPop($key)
    {   $this->connect(true);
        return $this->handler->lpop($key);
    }

    /*------------------------------------2.end list结构----------------------------------------------------*/


    /*------------------------------------3.start set结构----------------------------------------------------*/

    /**
     * 增,构建一个集合(无序集合)
     * @param string $key 集合Y名称
     * @param string|array $value 值
     * @param int $timeOut 时间  0表示无过期时间
     * @return
     */
    public function sAdd($key, $value, $timeOut = 0)
    {
        $this->connect(true);
        $re = $this->handler->sadd($key, $value);
        if ($timeOut > 0) $this->handler->expire($key, $timeOut);
        return $re;
    }

    /**
     * 查,取集合对应元素
     * @param string $key 集合名字
     */
    public function sMembers($key)
    {
        $this->connect(false);
        $re = $this->handler->exists($key);//存在返回1,不存在返回0

        if (!$re) return false;
        return $this->handler->smembers($key);
    }

    /*------------------------------------3.end  set结构----------------------------------------------------*/


    /*------------------------------------4.start sort set结构----------------------------------------------------*/
    /*
     * 增,改,构建一个集合(有序集合),支持批量写入,更新
     * @param string $key 集合名称
     * @param array $score_value key为scoll, value为该权的值
     * @return int 插入操作成功返回插入数量【,更新操作返回0】
     */
    public function zadd($key, $score_value, $timeOut = 0)
    {
        $this->connect(true);

        if (!is_array($score_value)) return false;
        $a = 0;//存放插入的数量
        foreach ($score_value as $score => $value) {
            $re = $this->handler->zadd($key, $score, $value);//当修改时,可以修改,但不返回更新数量
            $re && $a += 1;
            if ($timeOut > 0) $this->handler->expire($key, $timeOut);
        }

        return $a;
    }

    /**
     * 查,有序集合查询,可升序降序,默认从第一条开始,查询一条数据
     * @param $key ,查询的键值
     * @param $min ,从第$min条开始
     * @param $max,查询的条数
     * @param $order ,asc表示升序排序,desc表示降序排序
     * @return array|bool 如果成功,返回查询信息,如果失败返回false
     */
    public function zRange($key, $min = 0, $num = 1, $order = 'desc')
    { $this->connect(false);
        $re = $this->handler->exists($key);//存在返回1,不存在返回0

        if (!$re) return false;//不存在键值
        if ('desc' == strtolower($order)) {
            $re = $this->handler->zrevrange($key, $min, $min + $num - 1);
        } else {
            $re = $this->handler->zrange($key, $min, $min + $num - 1);
        }
        if (!$re) return false;//查询的范围值为空
        return $re;
    }

    /**
     * 返回集合key中,成员member的排名
     * @param $key,键值
     * @param $member,scroll值
     * @param $type ,是顺序查找还是逆序
     * @return bool,键值不存在返回false,存在返回其排名下标
     */
    public function zrank($key, $member, $type = 'desc')
    {
        $this->connect(false);
        $type = strtolower(trim($type));
        if ($type == 'desc') {
            $re = $this->handler->zrevrank($key, $member);//其中有序集成员按score值递减(从大到小)顺序排列,返回其排位
        } else {
            $re = $this->handler->zrank($key, $member);//其中有序集成员按score值递增(从小到大)顺序排列,返回其排位
        }

        if (!is_numeric($re)) return false;//不存在键值

        return $re;
    }

    /**
     * 返回名称为key的zset中score >= star且score <= end的所有元素
     * @param $key
     * @param $member
     * @param $star,
     * @param $end ,
     * @return array
     */
    public function zrangbyscore($key, $star, $end)
    {   $this->connect(false);
        return $this->handler->ZRANGEBYSCORE($key, $star, $end);
    }

    /**
     * 返回名称为key的zset中元素member的score
     * @param $key
     * @param $member
     * @return string ,返回查询的member值
     */
    function zScore($key, $member)
    {
        $this->connect(false);
        $this->handler->ZSCORE($key, $member);
    }
    /*------------------------------------4.end sort set结构----------------------------------------------------*/


    /*------------------------------------5.hash结构----------------------------------------------------*/

    public function hSetJson($redis_key, $field, $data, $timeOut = 0)
    {
        $this->connect(true);
        $redis_info = json_encode($data);                           //field的数据value,以json的形式存储
        $re = $this->handler->hSet($redis_key, $field, $redis_info);//存入缓存
        if ($timeOut > 0) $this->handler->expire($redis_key, $timeOut);//设置过期时间
        return $re;
    }

    public function hGetJson($redis_key, $field)
    {
        $this->connect(false);
        $info = $this->handler->hget($redis_key, $field);
        if ($info) {
            $info = json_decode($info, true);
        } else {
            $info = false;
        }
        return $info;
    }

    public function hSet($redis_key, $name, $data, $timeOut = 0)
    {
        $this->connect(true);
        $re = $this->handler->hset($redis_key, $name, $data);
        if ($timeOut > 0) $this->handler->expire($redis_key, $timeOut);
        return $re;
    }

    public function hSetNx($redis_key, $name, $data, $timeOut = 0)
    {
        $this->connect(true);
        $re = $this->handler->hsetNx($redis_key, $name, $data);
        if ($timeOut > 0) $this->handler->expire($redis_key, $timeOut);
        return $re;
    }


    /**
     * 增,普通逻辑的插入hash数据类型的值
     * @param $key ,键名
     * @param $data |array 一维数组,要存储的数据
     * @param $timeOut |num  过期时间
     * @return $number 返回OK【更新和插入操作都返回ok】
     */
    public function hMset($key, $data, $timeOut = 0)
    {
        $this->connect(true);
        $re = $this->handler->hmset($key, $data);
        if ($timeOut > 0) $this->handler->expire($key, $timeOut);
        return $re;
    }

    /**
     * 查,普通的获取值
     * @param $key ,表示该hash的下标值
     * @return array 。成功返回查询的数组信息,不存在信息返回false
     */
    public function hVals($key)
    {
        $this->connect(true);
        $re = $this->handler->exists($key);//存在返回1,不存在返回0
        if (!$re) return false;
        $vals = $this->handler->hvals($key);
        $keys = $this->handler->hkeys($key);
        $re = array_combine($keys, $vals);
        foreach ($re as $k => $v) {
            if (!is_null(json_decode($v))) {
                $re[$k] = json_decode($v, true);//true表示把json返回成数组
            }
        }
        return $re;
    }

    /**
     *
     * @param $key
     * @param $filed
     * @return bool|string
     */
    public function hGet($key, $filed = [])
    {
        $this->connect(false);
        if (empty($filed)) {
            $re = $this->handler->hgetAll($key);
        } elseif (is_string($filed)) {
            $re = $this->handler->hget($key, $filed);
        } elseif (is_array($filed)) {
            $re = $this->handler->hMget($key, $filed);
        }
        if (!$re) {
            return false;
        }
        return $re;
    }

    public function hDel($redis_key, $name)
    {
        $this->connect(true);
        $re = $this->handler->hdel($redis_key, $name);
        return $re;
    }

    public function hLan($redis_key)
    {
        $this->connect(false);
        $re = $this->handler->hLen($redis_key);
        return $re;
    }

    public function hIncre($redis_key, $filed, $value = 1)
    {
        $this->connect(true);
        return $this->handler->hIncrBy($redis_key, $filed, $value);
    }

    /**
     * 检验某个键值是否存在
     * @param $keys keys
     * @param string $type 类型,默认为常规
     * @param string $field 若为hash类型,输入$field
     * @return bool
     */
    public function hExists($keys, $field = '')
    {
        $this->connect(false);
        $re = $this->handler->hexists($keys, $field);//有返回1,无返回0
        return $re;
    }



    /*------------------------------------end hash结构----------------------------------------------------*/


    /*------------------------------------其他结构----------------------------------------------------*/
    /**
     * 设置自增,自减功能
     * @param $key ,要改变的键值
     * @param int $num ,改变的幅度,默认为1
     * @param string $member ,类型是zset或hash,需要在输入member或filed字段
     * @param string $type,类型,default为普通增减 ,还有:zset,hash
     * @return bool|int 成功返回自增后的scroll整数,失败返回false
     */
    public function incre($key, $num = 1, $member = '', $type = '')
    {
        $this->connect(true);
        $num = intval($num);
        switch (strtolower(trim($type))) {
            case "zset":
                $re = $this->handler->zIncrBy($key, $num, $member);//增长权值
                break;
            case "hash":
                $re = $this->handler->hincrby($key, $member, $num);//增长hashmap里的值
                break;
            default:
                if ($num > 0) {
                    $re = $this->handler->incrby($key, $num);//默认增长
                } else {
                    $re = $this->handler->decrBy($key, -$num);//默认增长
                }
                break;
        }
        if ($re) return $re;
        return false;
    }


    /**
     * 清除缓存
     * @param int $type 默认为0,清除当前数据库;1表示清除所有缓存
     */
    function flush($type = 0)
    {
        $this->connect(true);
        if ($type) {
            $this->handler->flushAll();//清除所有数据库
        } else {
            $this->handler->flushdb();//清除当前数据库
        }
    }

    /**
     * 检验某个键值是否存在
     * @param $keys keys
     * @param string $type 类型,默认为常规
     * @param string $field 若为hash类型,输入$field
     * @return bool
     */
    public function exists($keys, $type = '', $field = '')
    {
        $this->connect(false);
        switch (strtolower(trim($type))) {
            case 'hash':
                $re = $this->handler->hexists($keys, $field);//有返回1,无返回0
                break;
            default:
                $re = $this->handler->exists($keys);
                break;
        }
        return $re;
    }

    /**
     * 删除缓存
     * @param string|array $key 键值
     * @param $type 类型 默认为常规,还有hash,zset
     * @param string $field ,hash=>表示$field值,set=>表示value,zset=>表示value值,list类型特殊暂时不加
     * @return int |  返回删除的个数
     */
    public function delete($key, $type = "default", $field = '')
    {
        $this->connect(true);
        switch (strtolower(trim($type))) {
            case 'hash':
                $re = $this->handler->hDel($key, $field);//返回删除个数
                break;
            case 'set':
                $re = $this->handler->sRem($key, $field);//返回删除个数
                break;
            case 'zset':
                $re = $this->handler->zDelete($key, $field);//返回删除个数
                break;
            default:
                $re = $this->handler->del($key);//返回删除个数
                break;
        }
        return $re;
    }

    //日志记录
    public function logger($log_content, $position = 'user')
    {
        $this->connect(true);
        $max_size = 1000000;   //声明日志的最大尺寸1000K

        $log_dir = './log';//日志存放根目录

        if (!file_exists($log_dir)) mkdir($log_dir, 0777);//如果不存在该文件夹,创建

        if ($position == 'user') {
            $log_filename = "{$log_dir}/User_redis_log.txt";  //日志名称
        } else {
            $log_filename = "{$log_dir}/Wap_redis_log.txt";  //日志名称
        }

        //如果文件存在并且大于了规定的最大尺寸就删除了
        if (file_exists($log_filename) && (abs(filesize($log_filename)) > $max_size)) {
            unlink($log_filename);
        }

        //写入日志,内容前加上时间, 后面加上换行, 以追加的方式写入
        file_put_contents($log_filename, date('Y-m-d_H:i:s') . " " . $log_content . "\n", FILE_APPEND);
    }


    function flushDB()
    {
        $this->connect(true);
        $this->handler->flushDB();
    }

    function __destruct()
    {
        $this->handler->close();
    }

    /**
     * 魔术方法 有不存在的操作的时候执行
     * @access public
     * @param string $method 方法名
     * @param array $args 参数
     * @return mixed
     */
    public function __call($method, $args)
    {
        call_user_func_array([$this->handler, $method], $args);
    }


}

项目的控制器中就可以直接 new redis()–>方法名()使用了,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值