redis 实例化以及ping 的操作

本文介绍了一个简单的PHP Redis类封装,包括连接初始化、心跳检测及单例模式实现。通过该类可以方便地进行Redis操作,同时确保长连接的有效性和稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一旦开启一个不关闭的脚本  就需要一个ping函数去不断的校验的链接

 

下面的redis类经过简单的封装  ; ping 函数由于历史原因并没有封装到 Redis类里面 

 

class Redis {
    protected $handler;
    protected $options = [
        'persistent' => false,
        'host'       => '127.0.0.1',
        'port'       => 6379,
        'password'   => '',
        'select'     => 0,
        'timeout'    => 0,
    ];

    /**
     * 架构函数
     * @param array $options 配置redis初始化参数
     */
    public function __construct($options = [])
    {
        if (!extension_loaded('redis')) {
            throw new \BadFunctionCallException('not support: redis');
        }
        if (!empty($options)) {
            $this->options = array_merge($this->options, $options);
        }
        $this->handler = new \Redis;
        $func          = $this->options['persistent'] ? 'pconnect' : 'connect';
        $this->handler->$func($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']);
        }
    }

    public function __call($method, $args)
    {
        return call_user_func_array([$this->handler,$method], $args);
    }

    //禁止clone
    private function __clone() {}

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

 

 

 

 

 

//获取redis实例,单例
function getRedis()
{
    static $redis;

    try {

        // init redis
        if (!$redis) {
            $redis = redisInit();
        }
        // ping redis
        $redis = redisPing($redis);

        if (!is_object($redis)) {
            throw new \RedisException('reconnect redis failed');
        }
    } catch (\RedisException $e) {
        error_log($e->getMessage(), 3, ERROR_LOG);
    }
    return $redis;
}


/**
 * @param $redis
 * @return Redis|null
 */
function redisPing($redis)
{
    try {

        if (!is_object($redis) || !method_exists($redis, 'ping')) {
            throw new \RedisException('This connection is down');
        }

        $response = $redis->ping();

        if ($response != '+PONG') {
            throw new \RedisException('This connection is down');
        }

    } catch (\RedisException $e) {
        $redis = redisInit();
    }

    return $redis;
}

function redisInit()
{

    // config
    $redisConf = array(
        'type' => 'redis',
        'host' => 'crs-redis.test.cn',
        'port' => '6379',
    );

    // persistent redis
    $redisConf['persistent'] = true;
    
    // begin
    $redis = null;
    $i = 0;
    while ($i < 3 && !$redis) {
        try {
            $redis = new Redis($redisConf);

        } catch (\RedisException $e) {
            $i++;
        }
    }

    return $redis;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值