kefu/application/service/controller/Redis.php

49 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\service\controller;
use think\Controller;
class Redis extends Controller
{
public static $_instance;
private static $_reconnectNum = 3;
public static function instance(){
/**
* 配置数据.
*/
try{
if (empty(self::$_instance)) {
// echo date('Y-m-d H:i:s')."重新实例化redis.......".PHP_EOL;
global $configFile;
self::$_instance = new \Redis();
self::$_instance->connect(config('cache.host'), config('cache.port'));
self::$_instance->auth(config('cache.password'));
} else {
if(!self::$_instance->isConnected()){
throw new \Exception('redis连接断开' . " \n");
}
}
}catch (\Exception $e){
if(self::$_reconnectNum > 0){
self::$_reconnectNum --;
usleep(500000);
if(!empty(self::$_instance)){
self::$_instance = '';
}
return self::instance();
}else{
throw new \Exception("REDIS连接超时" . $e->getMessage() . "\n");
// echo "REDIS连接超时" . $e->getMessage() . "\n";
}
}
return self::$_instance;
}
}