当前位置:首页 > PHP > 正文内容

php redis断线重连

高老师5年前 (2021-03-16)PHP1914

在cli下运行消费数据,需要保持redis断线重连,虽然redis可以直接传递参数长连接,但是如果redis服务器异常关闭后再重启则需要重启php脚本,比较麻烦,所以写了一个redis断线重连简单版。

<?php

class PRedis
{
    /**
     * options
     * @var array
     */
    protected $options = ['host' => '127.0.0.1', 'port' => 6379, 'password' => '', 'select' => 0, 'timeout' => 0, 'expire' => 0, 'persistent' => false, 'prefix' => ''];

    /**
     * handler
     * @var null
     */
    protected $handler = null;

    /**
     * Redis constructor.
     * @param array $options
     * @throws Exception
     */
    public function __construct($options = [])
    {
        $this->options = array_merge($this->options, $options);
        $this->connect(false);
    }

    /**
     * connect
     * @param $throw
     * @throws RedisException
     */
    protected function connect($throw = true)
    {
        try {
            $this->handler = new \Redis();
            if ($this->options['persistent']) {
                $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
            } else {
                $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
            }
            if ('' != $this->options['password']) {
                $this->handler->auth($this->options['password']);
            }
            if (0 != $this->options['select']) {
                $this->handler->select($this->options['select']);
            }
        } catch (\RedisException$exception) {
            if ($throw) {
                throw $exception;
            }

        }
    }

    /**
     * __call
     * @param $name
     * @param $args
     * @return mixed
     * @throws
     */
    public function __call($name, $args)
    {
        try {
            set_error_handler(function ($errno, $errStr) {
                restore_error_handler();
                throw new RedisException($errStr, $errno);
            });

            return $this->handler->{$name}(...$args);
        } catch (\RedisException$exception) {
            echo "断线重连:" . $exception->getMessage() . PHP_EOL;
            $this->connect(false);
            return false;
        }
    }
}

$redis = new PRedis();
$redisKey = 'key';
while (true) {
    //set
    $redis->set($redisKey, rand(1, 99));

    //get
    $value = $redis->get($redisKey);
    if ($value) {
        echo $value . PHP_EOL;
    }

    //sleep
    sleep(1);
}

可以在断线重连异常中适当ping()来决定是否真正去连接,并且建议增加间隔多久连接一次。tp中mysql的断线重连是依赖于错误信息的关键字,或许在redis中断线重连也可以参考,毕竟异常不一定都是redis断开连接,可以从异常信息中提取关键字来精确分析,当然ping一下更简单。也没有来得及去看redis官方文档说明,凑合能用。

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/196.html

分享给朋友:

“php redis断线重连” 的相关文章

全方位认识PHP的SESSION

全方位认识PHP的SESSION

【一】.介绍session由于HTTP是无状态的请求,创建一个会话需要保持必须需要身份标识。当用户第一次访问,PHP会为用户创建一个唯一的sessionid,并将sessionid通过cookie发送给浏览器,并在服务器的临时文件创建一个以sessionid为名的文件用来保存这个sessionid保...

php finally使用

php finally使用

<?php /**  * @throws Exception  */ function curl() {     throw  new \Exception('err...

posix_ttyname函数详解

posix_ttyname函数详解

posix_ttyname - 获取当前终端设备名称。<?php     var_dump( posix_ttyname(STDOUT) );我们启动一个终端,执行上面的代码输出:/dev/tty1我们再启动一个终端,执行上面的代码输...

 thinkphp5定时任务,tp5定时任务,thinkphp定时任务,php定时任务,php定时器

thinkphp5定时任务,tp5定时任务,thinkphp定时任务,php定时任务,php定时器

本教程使用的定时任务基于EasyTak,EasyTask官方文档:https://gitee.com/392223903/EasyTask(1).安装tp5.0或者5.1composer create-project topthink/think=5.0.* tp5&n...

php关闭浏览器继续运行

php关闭浏览器继续运行

//设置客户端断开依然运行 ignore_user_abort(true); //设置脚本不超时 set_time_limit(0); //死循环每隔1秒访问一次网址 while (true) {     sleep(1);  &nb...

php curl Received HTTP code 403 from proxy after CONNECT

php curl Received HTTP code 403 from proxy after CONNECT

在调用微信code换openid的接口curl报错curl Received HTTP code 403 from proxy after CONNECT,错误码56。可以看到是curl的代理有问题。然后我自己电脑设置代理去访问curl请求的地址,的确也返回了403,说明代理不允许访问这个地址,联系...