From 308d06fd0d668e5ce05eb8ae483aa23ace3bc3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 4 Jan 2021 16:03:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/DatabasesProviders.php | 4 ++++ System/Cache/Redis.php | 25 ++++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Database/DatabasesProviders.php b/Database/DatabasesProviders.php index 22977515..a82d315b 100644 --- a/Database/DatabasesProviders.php +++ b/Database/DatabasesProviders.php @@ -7,6 +7,7 @@ namespace Database; use Exception; use Snowflake\Abstracts\Providers; use Snowflake\Application; +use Snowflake\Event; use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; use Snowflake\Abstracts\Config; @@ -26,6 +27,9 @@ class DatabasesProviders extends Providers public function onImport(Application $application) { $application->set('db', $this); + + $event = Snowflake::app()->getEvent(); + $event->on(Event::SERVER_WORKER_START, [$this, 'createPool']); } diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index ec361a64..ef503b7a 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -38,8 +38,25 @@ class Redis extends Component $event = Snowflake::app()->event; $event->on(Event::RELEASE_ALL, [$this, 'destroy']); $event->on(Event::EVENT_AFTER_REQUEST, [$this, 'release']); + $event->on(Event::SERVER_WORKER_START, [$this, 'createPool']); } + + /** + * @throws ConfigException + */ + public function createPool() + { + $connections = Snowflake::app()->pool->redis; + + $config = $this->get_config(); + $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; + + $connections->initConnections('redis:' . $name, true); + $connections->setLength(env('REDIS.POOL_LENGTH', 100)); + } + + /** * @param $name * @param $arguments @@ -117,13 +134,7 @@ SCRIPT; { $connections = Snowflake::app()->pool->redis; - $config = $this->get_config(); - $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; - - $connections->initConnections('redis:' . $name, true); - $connections->setLength(env('REDIS.POOL_LENGTH', 100)); - - $client = $connections->getConnection($config, true); + $client = $connections->getConnection($this->get_config(), true); if (!($client instanceof \Redis)) { throw new Exception('Redis connections more.'); }