event; $event->on(Event::RELEASE_ALL, [$this, 'destroy']); $event->on(Event::EVENT_AFTER_REQUEST, [$this, 'release']); } /** * @param $name * @param $arguments * @return mixed * @throws */ public function __call($name, $arguments) { try { if (method_exists($this, $name)) { $data = $this->{$name}(...$arguments); } else { $data = $this->proxy()->{$name}(...$arguments); } } catch (\Throwable $exception) { $this->error(print_r($exception, true)); $data = false; } finally { return $data; } } /** * 释放连接池 */ public function release() { $connections = Snowflake::get()->pool->redis; $connections->release($this->get_config(), true); } /** * 销毁连接池 */ public function destroy() { $connections = Snowflake::get()->pool->redis; $connections->destroy($this->get_config(), true); } /** * @return \Redis * @throws Exception */ public function proxy() { $connections = Snowflake::get()->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); if (!($client instanceof \Redis)) { throw new Exception('Redis connections more.'); } return $client; } /** * @return array */ public function get_config(): array { return [ 'host' => env('REDIS.HOST', $this->host), 'port' => env('REDIS.PORT', $this->port), 'auth' => env('REDIS.PASSWORD', $this->auth), 'timeout' => env('REDIS.TIMEOUT', 2), 'databases' => env('REDIS.DATABASE', $this->databases), 'read_timeout' => env('REDIS.READ_TIMEOUT', 10), 'prefix' => env('REDIS.PREFIX', 'system:'), ]; } }