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

php redis断线重连

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

在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 curl 获取cookie

php curl 获取cookie

    为了实现注册机才写的教程,为了批量注册一个网站,注册带有验证码,幸好是文本验证码,但是有session验证,于是POST必须携带cookie。代码如下。<?php class  AutoCurl{    ...

php 开启错误提示,php 关闭错误提示

php 开启错误提示,php 关闭错误提示

开启错误提示代码:ini_set("display_errors", "On"); error_reporting(E_ALL | E_STRICT);关闭错误提示代码:error_reporting(E_ALL ^&n...

php迭代器学习

php迭代器学习

php官方已经提供了Iterator(迭代器)接口,通过网上资料的学习,目前看适合超大集合或者数组提取使用。学习一个函数的实现对比内存占用差距.使用迭代器和普通循环实现range()函数。(1).普通循环实现range()函数。function newrange($low, $h...

PHP yield  PHP协程,PHP协程用法学习

PHP yield PHP协程,PHP协程用法学习

【一】.迭代器迭代是指反复执行一个过程,每执行一次叫做一次迭代。比如下面的代码就叫做迭代:1.  <?php   2.  $data = ['1', '2', &...

composer自动加载类库(非psr4规范的文件)

composer自动加载类库(非psr4规范的文件)

在项目下的composer配置文件修改(PaySdk是我这里一个支付sdk的目录,包含各种各样的支付sdk,这样写的意思让composer自动把PaySdk下的所有文件自动加载):"autoload": {      &...

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

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

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