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
|
|
|
|
|
|
|
|
|
2021-08-11 01:04:57 +08:00
|
|
|
namespace Kiri\Pool;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
2021-08-17 19:01:37 +08:00
|
|
|
use Annotation\Inject;
|
2021-08-16 18:24:32 +08:00
|
|
|
use Closure;
|
2021-04-23 18:37:18 +08:00
|
|
|
use Exception;
|
2021-08-17 16:52:50 +08:00
|
|
|
use Http\Context\Context;
|
2021-08-11 01:04:57 +08:00
|
|
|
use Kiri\Abstracts\Component;
|
2021-08-17 19:01:37 +08:00
|
|
|
use Kiri\Events\EventProvider;
|
2021-08-11 01:04:57 +08:00
|
|
|
use Kiri\Exception\ConfigException;
|
|
|
|
|
use Kiri\Kiri;
|
2021-08-17 19:01:37 +08:00
|
|
|
use Server\Events\OnWorkerExit;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class RedisClient
|
2021-08-11 01:04:57 +08:00
|
|
|
* @package Kiri\Kiri\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-08-16 18:43:37 +08:00
|
|
|
use Alias;
|
2021-08-08 02:42:55 +08:00
|
|
|
|
|
|
|
|
|
2021-08-16 18:43:37 +08:00
|
|
|
/**
|
|
|
|
|
* @param mixed $config
|
|
|
|
|
* @param bool $isMaster
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function get(mixed $config, bool $isMaster = false): mixed
|
|
|
|
|
{
|
|
|
|
|
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
|
|
|
|
if (Context::hasContext($coroutineName)) {
|
|
|
|
|
return Context::getContext($coroutineName);
|
|
|
|
|
}
|
|
|
|
|
$clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config));
|
|
|
|
|
return Context::setContext($coroutineName, $clients);
|
|
|
|
|
}
|
2021-08-08 02:42:55 +08:00
|
|
|
|
|
|
|
|
|
2021-08-16 18:24:32 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param mixed $config
|
|
|
|
|
* @return Closure
|
|
|
|
|
*/
|
2021-08-16 18:43:37 +08:00
|
|
|
public function create(string $name, mixed $config): Closure
|
|
|
|
|
{
|
|
|
|
|
return static function () use ($name, $config) {
|
|
|
|
|
return Kiri::getDi()->newObject(\Kiri\Cache\Base\Redis::class, [
|
|
|
|
|
$config['host'], (int)$config['port'], $config['databases'] ?? 0,
|
|
|
|
|
$config['auth'], $config['prefix'] ?? '', $config['timeout'] ?? 30,
|
|
|
|
|
$config['read_timeout'] ?? 30
|
|
|
|
|
]);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $config
|
|
|
|
|
* @param bool $isMaster
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function release(array $config, bool $isMaster = false)
|
|
|
|
|
{
|
|
|
|
|
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
|
|
|
|
if (!Context::hasContext($coroutineName)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->getPool()->push($coroutineName, Context::getContext($coroutineName));
|
|
|
|
|
Context::remove($coroutineName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $config
|
|
|
|
|
* @param bool $isMaster
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function destroy(array $config, bool $isMaster = false)
|
|
|
|
|
{
|
|
|
|
|
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
|
|
|
|
$this->getPool()->clean($coroutineName);
|
|
|
|
|
Context::remove($coroutineName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-17 19:01:37 +08:00
|
|
|
/**
|
|
|
|
|
* @param array $config
|
|
|
|
|
* @param bool $isMaster
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function connection_clear(array $config, bool $isMaster = false)
|
|
|
|
|
{
|
|
|
|
|
$coroutineName = $this->name('Redis:' . $config['host'], $isMaster);
|
|
|
|
|
$this->getPool()->clean($coroutineName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-08-16 18:43:37 +08:00
|
|
|
/**
|
|
|
|
|
* @return Pool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function getPool(): Pool
|
|
|
|
|
{
|
|
|
|
|
return Kiri::getDi()->get(Pool::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $isMaster
|
|
|
|
|
* @param $max
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function initConnections($name, $isMaster, $max)
|
|
|
|
|
{
|
|
|
|
|
$this->getPool()->initConnections($name, $isMaster, $max);
|
|
|
|
|
}
|
2021-07-05 15:49:37 +08:00
|
|
|
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|