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

php redis断线重连

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

在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对象转数组

PHP对象转数组

function objtoarr($obj){ $ret = array(); foreach($obj as $key =>$value){ if(gettype($value) == 'arr...

Thinkphp Call Stack,PHP调用栈Call Stack的获取

Thinkphp Call Stack,PHP调用栈Call Stack的获取

ThinkPHP中有一个debug调试功能,能输出报错文件的信息,并能看到这个函数被哪些函数调用,从框架的启动开始记录,特别方便调试。于是研究了下它的底层给予了实现。<?php //--框架核心--Start //框架内置错误处理 function errDealWith($er...

PHP最快方式模拟curl,PHP最快爬虫模拟方法

PHP最快方式模拟curl,PHP最快爬虫模拟方法

有时候我们需要爬一个接口,但是这个接口需要很多参数,包括header和cookie要去编写,使用php curl模拟实在太慢。我们可以通过浏览器的network来复制请求为curl命令。例如我需要模拟请求接口地址:https://www.xkmz.cc/Ajax/Debug/delly,我们只需要在...

php elasticsearch基础使用

php elasticsearch基础使用

elasticsearch的操作都是基于http协议的,已经有现成的php类库,composer安装即可。{     "require": {        &...

php redis队列实现秒杀 , php用redis 实现秒杀功能

php redis队列实现秒杀 , php用redis 实现秒杀功能

为什么使用队列?因为pop取队列具有原子性。假如我们需要秒杀一个商品id,我们先将商品的库存保存到一个队列。例如:<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6...

php位运算符详解

php位运算符详解

文章篇幅较长,如果不喜欢看文章的,此处抛出韩顺丰老师的位运算视频,韩老师应该是全网讲php位运算符最详细的一个老师了。链接:https://pan.baidu.com/s/14xj7er8eVSUcJ-jYXyA0GA  提取码:731m 链接:https://pan.baidu.com...