35 lines
720 B
PHP
35 lines
720 B
PHP
|
<?php
|
||
|
|
||
|
namespace app\service;
|
||
|
|
||
|
use app\exception\ServiceException;
|
||
|
use think\facade\Log;
|
||
|
|
||
|
class Redis
|
||
|
{
|
||
|
private static $redis = null;
|
||
|
|
||
|
private function __construct()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
private function __clone()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
// Redis 单例模式
|
||
|
public static function instance(): ?\Redis
|
||
|
{
|
||
|
if (self::$redis == null) {
|
||
|
try {
|
||
|
$redis = new \Redis();
|
||
|
$redis->connect(env('redis.host'), env('redis.port'));
|
||
|
self::$redis = $redis;
|
||
|
} catch (ServiceException $e) {
|
||
|
Log::error('实例化Redis失败: ' . $e->getMessage());
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
return self::$redis;
|
||
|
}
|
||
|
}
|