This commit is contained in:
xl
2023-05-26 10:30:23 +08:00
parent fff3211e61
commit 13cc75765b
+48 -25
View File
@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Kiri\Redis;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Kiri;
use Kiri\Abstracts\Component;
use Kiri\Events\EventProvider;
@@ -62,28 +63,7 @@ class Redis extends Component
*/
public function init(): void
{
$config = $this->get_config();
$length = \config('cache.redis.pool.max', 10);
on(OnWorkerExit::class, [$this, 'destroy']);
Kiri::getPool()->created($config['host'], $length, static function () use ($config) {
$redis = new \Redis();
if (!$redis->connect($config['host'], $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 ($config['read_timeout'] < 0) {
$config['read_timeout'] = 0;
}
$redis->select($config['databases']);
if ($config['read_timeout'] > 0) {
$redis->setOption(\Redis::OPT_READ_TIMEOUT, $config['read_timeout']);
}
$redis->setOption(\Redis::OPT_PREFIX, $config['prefix']);
return $redis;
});
}
@@ -108,7 +88,6 @@ class Redis extends Component
* @param $key
* @param int $timeout
* @return bool
* @throws RedisException
*/
public function waite($key, int $timeout = 5): bool
{
@@ -161,7 +140,7 @@ SCRIPT;
*/
public function destroy(): void
{
Kiri::getPool()->clean($this->host);
$this->pool()->clean($this->host);
}
@@ -182,7 +161,7 @@ SCRIPT;
} catch (\Throwable $throwable) {
$response = addError($throwable, 'redis');
} finally {
Kiri::getPool()->push($this->host, $client);
$this->pool()->push($this->host, $client);
}
return $response;
}
@@ -195,13 +174,57 @@ SCRIPT;
*/
private function getClient(): \Redis
{
return Kiri::getPool()->get($this->host);
return $this->pool()->get($this->host);
}
/**
* @return Pool
* @throws ReflectionException
*/
protected function pool(): Pool
{
$pool = Kiri::getPool();
if (!$pool->hasChannel($this->host)) {
$config = $this->get_config();
$length = \config('cache.redis.pool.max', 10);
$pool->created($config['host'], $length, $this->connect($config));
}
return $pool;
}
/**
* @param $config
* @return \Closure
*/
protected function connect($config): \Closure
{
return static function () use ($config) {
$redis = new \Redis();
if (!$redis->connect($config['host'], $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 ($config['read_timeout'] < 0) {
$config['read_timeout'] = 0;
}
$redis->select($config['databases']);
if ($config['read_timeout'] > 0) {
$redis->setOption(\Redis::OPT_READ_TIMEOUT, $config['read_timeout']);
}
$redis->setOption(\Redis::OPT_PREFIX, $config['prefix']);
return $redis;
};
}
/**
* @return array
*/
#[ArrayShape(['host' => "string", 'port' => "int", 'prefix' => "string", 'auth' => "string", 'databases' => "int", 'timeout' => "int", 'read_timeout' => "int", 'pool' => "array|int[]"])]
public function get_config(): array
{
return [