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

158 lines
3.6 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;
2021-04-23 18:37:18 +08:00
use Exception;
2020-08-31 01:27:08 +08:00
use HttpServer\Http\Context;
2020-09-03 15:23:11 +08:00
use Redis as SRedis;
2021-03-08 18:45:32 +08:00
use Snowflake\Abstracts\Pool;
2021-04-23 18:37:18 +08:00
use Snowflake\Exception\RedisConnectException;
2021-05-07 19:38:37 +08:00
use Swoole\Coroutine;
use Swoole\Runtime;
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-04-23 18:37:18 +08:00
public int $_create = 0;
/**
* @param $value
*/
public function setLength($value)
{
$this->max = $value;
}
/**
* @param string $name
* @return bool
*/
public function canCreate(string $name): bool
{
2021-05-07 19:38:37 +08:00
if (!isset(static::$hasCreate[$name])) {
static::$hasCreate[$name] = 0;
2021-04-23 18:37:18 +08:00
}
2021-05-07 19:38:37 +08:00
return static::$hasCreate[$name] >= $this->max;
2021-04-23 18:37:18 +08:00
}
/**
* @param mixed $config
* @param bool $isMaster
* @return mixed
* @throws Exception
*/
public function get(mixed $config, $isMaster = false): mixed
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
2021-05-04 03:07:11 +08:00
if (!Context::hasContext($coroutineName)) {
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
2021-04-23 18:37:18 +08:00
}
2021-05-04 03:07:11 +08:00
return Context::getContext($coroutineName);
2021-04-23 18:37:18 +08:00
}
/**
* @param string $name
* @param mixed $config
* @return SRedis
* @throws RedisConnectException
* @throws Exception
*/
public function createClient(string $name, mixed $config): SRedis
{
2021-05-07 19:38:37 +08:00
if (Coroutine::getCid() === -1) {
Runtime::enableCoroutine(false);
}
2021-04-23 18:37:18 +08:00
$redis = new SRedis();
if (!$redis->pconnect($config['host'], (int)$config['port'], $config['timeout'])) {
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port']));
}
2021-05-14 03:13:59 +08:00
if (!empty($config['auth']) && !$redis->auth($config['auth'])) {
2021-04-23 18:37:18 +08:00
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->increment($name);
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));
2021-05-04 02:43:58 +08:00
Context::remove($coroutineName);
2021-04-23 18:37:18 +08:00
$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)) {
$this->decrement($coroutineName);
}
2021-05-04 02:43:58 +08:00
Context::remove($coroutineName);
$this->flush(0);
2021-04-23 18:37:18 +08:00
}
/**
* @param string $name
* @param mixed $client
* @return bool
* @throws Exception
*/
public function checkCanUse(string $name, mixed $client): bool
{
try {
if (!($client instanceof SRedis)) {
$result = false;
} else {
$result = true;
}
} catch (\Throwable $exception) {
$this->addError($exception, 'redis');
$result = false;
} finally {
if (!$result) {
$this->decrement($name);
}
return $result;
}
}
2020-08-31 01:27:08 +08:00
}