49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?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;
|
||
}
|
||
|
||
|
||
}
|