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

php redis断线重连

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

在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断线重连” 的相关文章

cookie中文乱码,GBK下

cookie中文乱码,GBK下

上家公司开发医院挂号系统,系统采用GBK编码。ajax发送的中文用户名让PHP保存为cookie出现乱码的解决方案。1.Javascript变量var user=document.getElementById('user').innerText; user=escape(u...

PHP模拟并发请求

PHP模拟并发请求

原理:使用curl_init()创建多个请求实例,再使用curl_multi_init()批量执行创建的多个请求实例。文件1:curl.php<?php  $threads=500;//并发请求次数 $url='http://blog.cn/index.php?';...

php 经纬度获取城市,php 经纬度转城市

php 经纬度获取城市,php 经纬度转城市

通过经纬度转换为城市名称,并获得城市的编号,通常在全国类型的商城中比较通用。//经纬度转城市名称(返回城市id,城市名称) //$ak开发密钥,$lat纬度,$lng经度,$type返回数据类型 public function getCityName($ak,$lat,$lng)...

php 将数组键值转为变量

php 将数组键值转为变量

<?php $data=array('a'=>1,'b'=>2,'c'=>3,'d'=>4); extract($data); var_dump($a,$b,$c,$d); ?>在人人商城中捡到的...

php上传大文件,php大文件上传

php上传大文件,php大文件上传

(1).前端文件:<form action="upload.php" method="post" enctype="multipart/form-data">    &...

PHP获取站点根目录,PHP获取应用根目录,cgi和cli都支持

PHP获取站点根目录,PHP获取应用根目录,cgi和cli都支持

重构框架的时候想要考虑支持下cli模式,于是参考了thinkphp的底层。/**  * 获取应用根目录  * @return string  */ public static function getRootP...