From 13cc75765bcd5f77d26113a8ab9b21a07e7b2cc5 Mon Sep 17 00:00:00 2001 From: xl Date: Fri, 26 May 2023 10:30:23 +0800 Subject: [PATCH] qqq --- kiri-engine/Redis/Redis.php | 321 +++++++++++++++++++----------------- 1 file changed, 172 insertions(+), 149 deletions(-) diff --git a/kiri-engine/Redis/Redis.php b/kiri-engine/Redis/Redis.php index 59a236dd..902eb03e 100644 --- a/kiri-engine/Redis/Redis.php +++ b/kiri-engine/Redis/Redis.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Kiri\Redis; use Exception; +use JetBrains\PhpStorm\ArrayShape; use Kiri; use Kiri\Abstracts\Component; use Kiri\Events\EventProvider; @@ -32,107 +33,85 @@ use ReflectionException; class Redis extends Component { - public string $host = ''; + public string $host = ''; - public int $port = 6379; + public int $port = 6379; - public string $prefix = 'api:'; + public string $prefix = 'api:'; - public string $auth = ''; + public string $auth = ''; - public int $databases = 0; + public int $databases = 0; - public int $timeout = 30; + public int $timeout = 30; - /** - * @var int - */ - public int $read_timeout = -1; + /** + * @var int + */ + public int $read_timeout = -1; - /** - * @var array|int[] - */ - public array $pool = ['min' => 1, 'max' => 100]; + /** + * @var array|int[] + */ + public array $pool = ['min' => 1, 'max' => 100]; - /** - * @return void + /** + * @return void * @throws Exception - */ - public function init(): void - { - $config = $this->get_config(); - - $length = \config('cache.redis.pool.max', 10); - on(OnWorkerExit::class, [$this, 'destroy']); - Kiri::getPool()->created($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; - }); - } + */ + public function init(): void + { + on(OnWorkerExit::class, [$this, 'destroy']); + } - /** - * @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 $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 + */ + 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 = <<