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) { if ($exception instanceof RedisConnectException) { throw new Exception($exception->getMessage()); } $this->error(print_r($exception, true)); $data = false; } finally { return $data; } } /** * 释放连接池 * @throws ConfigException */ public function release() { $connections = Snowflake::app()->pool->redis; $connections->release($this->get_config(), true); } /** * 销毁连接池 * @throws ConfigException */ public function destroy() { $connections = Snowflake::app()->pool->redis; $connections->destroy($this->get_config(), true); } /** * @return \Redis * @throws Exception */ public function proxy() { $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); if (!($client instanceof \Redis)) { throw new Exception('Redis connections more.'); } return $client; } /** * @return array * @throws ConfigException */ public function get_config(): array { $config = Config::get('cache.redis', false, [ 'host' => '127.0.0.1', 'port' => '6379', 'prefix' => Config::get('id'), 'auth' => '', 'databases' => '0', 'read_timeout' => -1, 'conn_timeout' => -1, ]); return [ 'host' => $config['host'], 'port' => $config['port'], 'auth' => $config['auth'], 'timeout' => $config['conn_timeout'], 'databases' => $config['databases'], 'read_timeout' => $config['read_timeout'], 'prefix' => $config['prefix'], ]; } }