This commit is contained in:
2023-04-17 01:39:32 +08:00
parent a0cd1182c1
commit 00b6d28196
+8 -17
View File
@@ -44,13 +44,6 @@ class Redis extends Component
private int $timeout = 30; private int $timeout = 30;
/**
* @var ContainerInterface
*/
#[Container(ContainerInterface::class)]
readonly public ContainerInterface $container;
/** /**
* @var int * @var int
*/ */
@@ -74,10 +67,10 @@ class Redis extends Component
$length = Config::get('cache.redis.pool.max', 10); $length = Config::get('cache.redis.pool.max', 10);
$eventProvider = $this->container->get(EventProvider::class); $eventProvider = Kiri::getDi()->get(EventProvider::class);
$eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0); $eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0);
$pool = $this->container->get(Pool::class); $pool = Kiri::getDi()->get(Pool::class);
$pool->initConnections($config['host'], $length, static function () use ($config) { $pool->initConnections($config['host'], $length, static function () use ($config) {
$redis = new \Redis(); $redis = new \Redis();
if (!$redis->connect($config['host'], $config['port'], $config['timeout'])) { if (!$redis->connect($config['host'], $config['port'], $config['timeout'])) {
@@ -169,13 +162,11 @@ SCRIPT;
/** /**
* @return void * @return void
* @throws ContainerExceptionInterface * @throws \ReflectionException
* @throws NotFoundExceptionInterface
* @throws Exception
*/ */
public function destroy(): void public function destroy(): void
{ {
$pool = $this->container->get(Pool::class); $pool = Kiri::getDi()->get(Pool::class);
$pool->clean($this->host); $pool->clean($this->host);
} }
@@ -187,6 +178,7 @@ SCRIPT;
* @throws ConfigException * @throws ConfigException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws \ReflectionException
*/ */
public function proxy($name, $arguments): mixed public function proxy($name, $arguments): mixed
{ {
@@ -196,7 +188,7 @@ SCRIPT;
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$response = \Kiri::getLogger()->addError($throwable->getMessage()); $response = \Kiri::getLogger()->addError($throwable->getMessage());
} finally { } finally {
$pool = $this->container->get(Pool::class); $pool = Kiri::getDi()->get(Pool::class);
$pool->push($this->host, $client); $pool->push($this->host, $client);
} }
return $response; return $response;
@@ -206,12 +198,11 @@ SCRIPT;
/** /**
* @return \Redis * @return \Redis
* @throws ConfigException * @throws ConfigException
* @throws ContainerExceptionInterface * @throws \ReflectionException
* @throws NotFoundExceptionInterface
*/ */
private function getClient(): \Redis private function getClient(): \Redis
{ {
$pool = $this->container->get(Pool::class); $pool = Kiri::getDi()->get(Pool::class);
return $pool->get($this->host); return $pool->get($this->host);
} }