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

142 lines
3.4 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-07-05 15:42:44 +08:00
use Snowflake\Abstracts\Component;
2021-07-05 11:23:57 +08:00
use Snowflake\Exception\ConfigException;
2021-04-23 18:37:18 +08:00
use Snowflake\Exception\RedisConnectException;
2021-07-05 15:42:44 +08:00
use Snowflake\Snowflake;
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
*/
2021-07-05 15:42:44 +08:00
class Redis extends Component
2020-08-31 01:27:08 +08:00
{
2021-07-05 15:42:44 +08:00
private ?ClientsPool $clientsPool = null;
2021-04-23 18:37:18 +08:00
/**
* @param mixed $config
* @param bool $isMaster
* @return mixed
* @throws Exception
*/
2021-06-29 14:36:45 +08:00
public function get(mixed $config, bool $isMaster = false): mixed
2021-04-23 18:37:18 +08:00
{
2021-07-05 15:42:44 +08:00
$coroutineName = $this->getPool()->name('Redis:' . $config['host'], $isMaster);
if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName);
}
if (Coroutine::getCid() === -1) {
return Context::setContext($coroutineName, $this->createClient($coroutineName, $config));
}
$clients = $this->getPool()->getFromChannel($coroutineName);
if (empty($clients)) {
return Context::setContext($coroutineName, $this->createClient($coroutineName, $config));
2021-04-23 18:37:18 +08:00
}
2021-07-05 15:42:44 +08:00
return Context::setContext($coroutineName, $clients);
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']);
2021-07-05 15:42:44 +08:00
$this->getPool()->increment($name);
2021-04-23 18:37:18 +08:00
return $redis;
}
/**
* @param array $config
* @param bool $isMaster
2021-07-05 11:23:57 +08:00
* @throws ConfigException
2021-07-05 15:42:44 +08:00
* @throws Exception
2021-04-23 18:37:18 +08:00
*/
2021-06-29 14:36:45 +08:00
public function release(array $config, bool $isMaster = false)
2021-04-23 18:37:18 +08:00
{
2021-07-05 15:42:44 +08:00
$coroutineName = $this->getPool()->name('Redis:' . $config['host'], $isMaster);
2021-04-23 18:37:18 +08:00
if (!Context::hasContext($coroutineName)) {
return;
}
2021-07-05 15:42:44 +08:00
$this->getPool()->push($coroutineName, Context::getContext($coroutineName));
2021-07-05 11:58:57 +08:00
Context::remove($coroutineName);
2021-04-23 18:37:18 +08:00
}
/**
* @param array $config
* @param bool $isMaster
* @throws Exception
*/
2021-06-29 14:36:45 +08:00
public function destroy(array $config, bool $isMaster = false)
2021-04-23 18:37:18 +08:00
{
2021-07-05 15:42:44 +08:00
$coroutineName = $this->getPool()->name('Redis:' . $config['host'], $isMaster);
2021-04-23 18:37:18 +08:00
if (Context::hasContext($coroutineName)) {
2021-07-05 15:42:44 +08:00
$this->getPool()->decrement($coroutineName);
2021-04-23 18:37:18 +08:00
}
2021-07-05 16:15:45 +08:00
$this->getPool()->clean($coroutineName);
2021-07-05 11:23:57 +08:00
Context::remove($coroutineName);
2021-04-23 18:37:18 +08:00
}
2021-07-05 15:42:44 +08:00
2021-04-23 18:37:18 +08:00
/**
2021-07-05 15:42:44 +08:00
* @return ClientsPool
2021-04-23 18:37:18 +08:00
* @throws Exception
*/
2021-07-05 15:42:44 +08:00
public function getPool(): ClientsPool
2021-04-23 18:37:18 +08:00
{
2021-07-05 15:42:44 +08:00
if (!$this->clientsPool) {
$this->clientsPool = Snowflake::app()->getClientsPool();
2021-04-23 18:37:18 +08:00
}
2021-07-05 15:42:44 +08:00
return $this->clientsPool;
2021-04-23 18:37:18 +08:00
}
2020-08-31 01:27:08 +08:00
2021-07-05 15:49:37 +08:00
/**
* @param $name
* @param $isMaster
* @param $max
* @throws Exception
*/
public function initConnections($name, $isMaster, $max)
{
$this->getPool()->initConnections($name, $isMaster, $max);
}
2020-08-31 01:27:08 +08:00
}