1, 'max' => 100]; /** * @return void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @throws Exception */ public function init(): void { $config = $this->get_config(); $length = Config::get('cache.redis.pool.max', 10); $eventProvider = $this->container->get(EventProvider::class); $eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0); $pool = $this->container->get(Pool::class); $pool->initConnections($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; }); } /** * @param $name * @param $arguments * @return mixed * @throws */ public function __call($name, $arguments): mixed { if (method_exists($this, $name)) { $data = $this->{$name}(...$arguments); } else { $data = $this->proxy($name, $arguments); } return $data; } /** * @param $key * @param int $timeout * @return bool * @throws \RedisException */ public function waite($key, int $timeout = 5): bool { $time = time(); while (!$this->setNx($key, '1')) { if (time() - $time >= $timeout) { return FALSE; } usleep(1000); } $this->expire($key, $timeout); return TRUE; } /** * @param $key * @param int $timeout * @return bool|int * @throws Exception */ public function lock($key, int $timeout = 5): bool|int { $script = <<