Files
kiri-core/System/Pool/Redis.php
T

163 lines
4.2 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Pool;
use HttpServer\Http\Context;
2020-09-03 15:23:11 +08:00
use Redis as SRedis;
2020-08-31 01:27:08 +08:00
use RedisException;
2021-02-23 16:56:34 +08:00
use Snowflake\Exception\ComponentException;
2020-09-03 18:05:54 +08:00
use Snowflake\Exception\RedisConnectException;
2020-08-31 01:27:08 +08:00
use Swoole\Coroutine;
use Exception;
2021-02-15 20:31:01 +08:00
use Swoole\Timer;
2020-08-31 01:27:08 +08:00
/**
* Class RedisClient
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Pool
2020-08-31 01:27:08 +08:00
*/
2020-09-03 15:23:11 +08:00
class Redis extends Pool
2020-08-31 01:27:08 +08:00
{
2021-01-04 16:53:29 +08:00
2021-02-22 00:59:05 +08:00
public int $_create = 0;
2021-02-22 00:56:05 +08:00
/**
* @param $value
*/
public function setLength($value)
{
$this->max = $value;
}
/**
2021-02-23 16:56:34 +08:00
* @param mixed $config
2021-02-22 00:56:05 +08:00
* @param bool $isMaster
* @return mixed
* @throws Exception
*/
2021-02-23 16:56:34 +08:00
public function get(mixed $config, $isMaster = false): mixed
2021-02-22 00:56:05 +08:00
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (($redis = Context::getContext($coroutineName)) instanceof \Redis) {
return $redis;
}
2021-02-23 16:56:34 +08:00
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
2021-02-22 00:56:05 +08:00
}
2021-02-22 17:44:24 +08:00
/**
* @param string $name
2021-02-23 16:56:34 +08:00
* @param mixed $config
2021-02-22 17:44:24 +08:00
* @return SRedis
2021-02-23 16:56:34 +08:00
* @throws RedisConnectException|ComponentException
2021-02-22 17:44:24 +08:00
*/
2021-02-23 16:56:34 +08:00
public function createClient(string $name, mixed $config): SRedis
2021-02-22 00:56:05 +08:00
{
$this->printClients($config['host'], $name, true);
$redis = new SRedis();
if (!$redis->connect($config['host'], (int)$config['port'], $config['timeout'])) {
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port']));
}
if (empty($config['auth']) || !$redis->auth($config['auth'])) {
throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $config['host'], $config['auth']));
}
if (!isset($config['read_timeout'])) {
$config['read_timeout'] = 10;
}
$redis->select($config['databases']);
$redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']);
$redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
$this->_create += 1;
return $redis;
}
/**
* @param array $config
* @param bool $isMaster
*/
public function release(array $config, $isMaster = false)
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) {
return;
}
$this->push($coroutineName, Context::getContext($coroutineName));
$this->remove($coroutineName);
$this->lastTime = time();
}
/**
* @param array $config
* @param bool $isMaster
* @throws Exception
*/
public function destroy(array $config, $isMaster = false)
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) {
return;
}
$this->desc($coroutineName);
$this->remove($coroutineName);
$this->clean($coroutineName);
}
/**
* @param $coroutineName
*/
public function remove(string $coroutineName)
{
2021-02-25 13:35:48 +08:00
Context::remove($coroutineName);
2021-02-22 00:56:05 +08:00
}
/**
* @param $name
* @param $time
* @param $client
* @return bool
* @throws Exception
*/
public function checkCanUse(string $name, mixed $client): bool
{
try {
if (!($client instanceof SRedis)) {
$result = false;
} else if (!$client->isConnected() || !$client->ping('connect.')) {
$result = false;
} else {
$result = true;
}
} catch (\Throwable $exception) {
$this->error($exception);
$result = false;
} finally {
if (!$result) {
$this->desc($name);
}
return $result;
}
}
/**
* @param string $name
*/
public function desc(string $name)
{
$this->_create -= 1;
}
2020-08-31 01:27:08 +08:00
}