2020-11-03 21:02:24 +08:00
|
|
|
|
<?php /** @noinspection PhpComposerExtensionStubsInspection */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZM\Store\Redis;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use RedisException;
|
|
|
|
|
|
use Swoole\Database\RedisConfig;
|
|
|
|
|
|
use Swoole\Database\RedisPool;
|
|
|
|
|
|
use ZM\Console\Console;
|
|
|
|
|
|
|
|
|
|
|
|
class ZMRedisPool
|
|
|
|
|
|
{
|
|
|
|
|
|
/** @var null|RedisPool */
|
|
|
|
|
|
public static $pool = null;
|
|
|
|
|
|
|
|
|
|
|
|
public static function init($config) {
|
|
|
|
|
|
self::$pool = new RedisPool((new RedisConfig())
|
|
|
|
|
|
->withHost($config['host'])
|
|
|
|
|
|
->withPort($config['port'])
|
|
|
|
|
|
->withAuth($config['auth'])
|
|
|
|
|
|
->withDbIndex($config['db_index'])
|
|
|
|
|
|
->withTimeout($config['timeout'] ?? 1)
|
|
|
|
|
|
);
|
|
|
|
|
|
try {
|
|
|
|
|
|
$r = self::$pool->get()->ping('123');
|
2021-02-09 17:09:09 +08:00
|
|
|
|
if (strpos(strtolower($r), "123") !== false) {
|
2020-11-03 21:02:24 +08:00
|
|
|
|
Console::debug("成功连接redis连接池!");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
var_dump($r);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (RedisException $e) {
|
2021-02-09 17:09:09 +08:00
|
|
|
|
Console::error("Redis init failed! " . $e->getMessage());
|
2020-11-03 21:02:24 +08:00
|
|
|
|
self::$pool = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|