From 457ae27528a5468bb3dfae1a5b19aac2ba990fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 24 Feb 2022 15:24:29 +0800 Subject: [PATCH] modify plugin name --- function.php | 6 +- kiri-engine/Cache/File.php | 141 ------- kiri-engine/Pool/Connection.php | 45 +- kiri-engine/Pool/Pool.php | 3 +- .../Pool/{Helper => }/QueueInterface.php | 56 +-- kiri-engine/Pool/Redis.php | 120 ------ kiri-engine/Pool/{Helper => }/SplQueue.php | 202 ++++----- .../ICache.php => Redis/CacheInterface.php} | 130 +++--- .../Base/Redis.php => Redis/Helper.php} | 357 ++++++++-------- kiri-engine/{Cache => Redis}/Redis.php | 392 +++++++++--------- kiri-gii/Gii.php | 2 +- 11 files changed, 596 insertions(+), 858 deletions(-) delete mode 100644 kiri-engine/Cache/File.php rename kiri-engine/Pool/{Helper => }/QueueInterface.php (85%) delete mode 100644 kiri-engine/Pool/Redis.php rename kiri-engine/Pool/{Helper => }/SplQueue.php (91%) rename kiri-engine/{Cache/ICache.php => Redis/CacheInterface.php} (90%) rename kiri-engine/{Cache/Base/Redis.php => Redis/Helper.php} (85%) rename kiri-engine/{Cache => Redis}/Redis.php (67%) diff --git a/function.php b/function.php index 171ae5de..ef7eeb3a 100644 --- a/function.php +++ b/function.php @@ -441,12 +441,12 @@ if (!function_exists('redis')) { /** - * @return \Kiri\Cache\Redis|Redis + * @return \Kiri\Redis\Redis|Redis * @throws Exception */ - function redis(): \Kiri\Cache\Redis|Redis + function redis(): \Kiri\Redis\Redis|Redis { - return Kiri::getDi()->get(\Kiri\Cache\Redis::class); + return Kiri::getDi()->get(\Kiri\Redis\Redis::class); } } diff --git a/kiri-engine/Cache/File.php b/kiri-engine/Cache/File.php deleted file mode 100644 index 410f6e52..00000000 --- a/kiri-engine/Cache/File.php +++ /dev/null @@ -1,141 +0,0 @@ -getCacheKey($key); - if (!$this->exists($tmpFile)) { - touch($tmpFile); - } - return System::writeFile($tmpFile, $val, LOCK_EX); - } - - /** - * @param $key - * @param array $hashKeys - * @return array|bool - */ - public function hMGet($key, array $hashKeys): array|bool - { - $hash = $this->get($key); - if (!is_array($hash)) { - return false; - } - - $nowHash = []; - foreach ($hashKeys as $hashKey) { - $nowHash[$hashKey] = $hash[$hashKey] ?? null; - } - return $nowHash; - } - - /** - * @param $key - * @param array $val - * @return bool|int|string - */ - public function hMSet($key, array $val): bool|int|string - { - $hash = $this->get($key); - if (!is_array($hash)) { - return false; - } - - $merge = array_merge($hash, $val); - return $this->set($key, $merge); - } - - /** - * @param string $key - * @param string $hashKey - * @return string|int|bool - */ - public function hGet(string $key, string $hashKey): string|int|bool - { - $hash = $this->get($key); - if (!is_array($hash)) { - return false; - } - return $hash[$hashKey] ?? false; - } - - /** - * @param $key - * @param $hashKey - * @param $hashValue - * @return bool|int|string - */ - public function hSet($key, $hashKey, $hashValue): bool|int|string - { - $hash = $this->get($key); - if (!is_array($hash)) { - return false; - } - - $hash[$hashKey] = $hashValue; - - return $this->set($key, $hash); - } - - /** - * @param $key - * @return bool - */ - #[Pure] public function exists($key): bool - { - return file_exists($key); - } - - /** - * @param $key - * @return mixed|bool - */ - public function get($key): mixed - { - $tmpFile = $this->getCacheKey($key); - if (!$this->exists($tmpFile)) { - return false; - } - $content = file_get_contents($tmpFile); - return swoole_unserialize($content); - } - - /** - * @param $key - * @return string - * @throws - */ - private function getCacheKey($key): string - { - return storage($key, 'cache'); - } -} diff --git a/kiri-engine/Pool/Connection.php b/kiri-engine/Pool/Connection.php index 7e5f68a8..ca45ae12 100644 --- a/kiri-engine/Pool/Connection.php +++ b/kiri-engine/Pool/Connection.php @@ -10,6 +10,8 @@ use Kiri; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; use Kiri\Context; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use Swoole\Error; use Throwable; @@ -20,7 +22,19 @@ use Throwable; class Connection extends Component { - use Alias; + + private Pool $pool; + + + /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function init() + { + $this->pool = $this->getContainer()->get(Pool::class); + } /** @@ -83,15 +97,15 @@ class Connection extends Component public function get(mixed $config): ?PDO { $coroutineName = $config['cds']; - - if (($connection = Context::getContext($coroutineName)) instanceof PDO) { - return $connection; - } +// +// if (($connection = Context::getContext($coroutineName)) instanceof PDO) { +// return $connection; +// } $minx = Config::get('databases.pool.min', 1); /** @var PDO $connections */ - $connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $minx); + $connections = $this->pool->get($coroutineName, $this->create($coroutineName, $config), $minx); if (Context::hasContext('begin_' . $coroutineName)) { $connections->beginTransaction(); } @@ -121,7 +135,7 @@ class Connection extends Component */ public function addItem(string $name, PDO $PDO) { - $this->getPool()->push($name, $PDO); + $this->pool->push($name, $PDO); } @@ -132,7 +146,7 @@ class Connection extends Component */ public function initConnections($name, $max) { - $this->getPool()->initConnections($name, $max); + $this->pool->initConnections($name, $max); } @@ -148,7 +162,7 @@ class Connection extends Component return; } - $this->getPool()->push($coroutineName, $client); + $this->pool->push($coroutineName, $client); Context::remove($coroutineName); } @@ -169,7 +183,7 @@ class Connection extends Component */ public function connection_clear($name) { - $this->getPool()->clean($name); + $this->pool->clean($name); } @@ -202,17 +216,8 @@ class Connection extends Component public function disconnect($coroutineName) { Context::remove($coroutineName); - $this->getPool()->clean($coroutineName); + $this->pool->clean($coroutineName); } - /** - * @return Pool - * @throws Exception - */ - public function getPool(): Pool - { - return Kiri::getDi()->get(Pool::class); - } - } diff --git a/kiri-engine/Pool/Pool.php b/kiri-engine/Pool/Pool.php index 30b0f654..73b26edf 100644 --- a/kiri-engine/Pool/Pool.php +++ b/kiri-engine/Pool/Pool.php @@ -5,11 +5,10 @@ namespace Kiri\Pool; use Exception; -use Kiri\Context; use Kiri\Abstracts\Component; use Kiri\Abstracts\Config; +use Kiri\Context; use Kiri\Exception\ConfigException; -use Kiri\Pool\Helper\SplQueue; use Swoole\Coroutine; use Swoole\Coroutine\Channel; diff --git a/kiri-engine/Pool/Helper/QueueInterface.php b/kiri-engine/Pool/QueueInterface.php similarity index 85% rename from kiri-engine/Pool/Helper/QueueInterface.php rename to kiri-engine/Pool/QueueInterface.php index 7e394c82..92809302 100644 --- a/kiri-engine/Pool/Helper/QueueInterface.php +++ b/kiri-engine/Pool/QueueInterface.php @@ -1,28 +1,28 @@ - 1, 'max' => 100]; - - $clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $pool['min'] ?? 1); - return Context::setContext($coroutineName, $clients); - } - - - /** - * @param string $name - * @param mixed $config - * @return Closure - */ - public function create(string $name, mixed $config): Closure - { - return static function () use ($name, $config) { - return Kiri::getDi()->create(\Kiri\Cache\Base\Redis::class, [$config]); - }; - } - - - /** - * @param array $config - * @param bool $isMaster - * @throws ConfigException - * @throws Exception - */ - public function release(array $config, bool $isMaster = false) - { - $coroutineName = $config['host']; - if (!Context::hasContext($coroutineName)) { - return; - } - - $this->getPool()->push($coroutineName, Context::getContext($coroutineName)); - Context::remove($coroutineName); - } - - /** - * @param array $config - * @param bool $isMaster - * @throws Exception - */ - public function destroy(array $config, bool $isMaster = false) - { - $this->getPool()->clean($config['host']); - Context::remove($config['host']); - } - - - /** - * @param array $config - * @param bool $isMaster - * @throws Exception - */ - public function connection_clear(array $config, bool $isMaster = false) - { - $this->getPool()->clean($config['host']); - } - - - /** - * @return Pool - * @throws Exception - */ - public function getPool(): Pool - { - return Kiri::getDi()->get(Pool::class); - } - - - /** - * @param $name - * @param $isMaster - * @param $max - * @throws Exception - */ - public function initConnections($name, $isMaster, $max) - { - $this->getPool()->initConnections($name, $max); - } - - -} diff --git a/kiri-engine/Pool/Helper/SplQueue.php b/kiri-engine/Pool/SplQueue.php similarity index 91% rename from kiri-engine/Pool/Helper/SplQueue.php rename to kiri-engine/Pool/SplQueue.php index e845c062..9c410170 100644 --- a/kiri-engine/Pool/Helper/SplQueue.php +++ b/kiri-engine/Pool/SplQueue.php @@ -1,101 +1,101 @@ -channel = new \SplQueue(); - } - - - /** - * @return bool - */ - public function isEmpty(): bool - { - // TODO: Implement isEmpty() method. - return $this->channel->count() < 1; - } - - - /** - * @param mixed $data - * @param float $timeout - * @return bool - */ - public function push(mixed $data, float $timeout = -1): bool - { - // TODO: Implement push() method. - $this->channel->enqueue($data); - return true; - } - - - /** - * @param float $timeout - * @return mixed - */ - public function pop(float $timeout = -1): mixed - { - // TODO: Implement pop() method. - return $this->channel->dequeue(); - } - - - /** - * @return array - */ - public function stats(): array - { - // TODO: Implement stats() method. - return []; - } - - - /** - * @return bool - */ - public function close(): bool - { - // TODO: Implement close() method. - return false; - } - - - /** - * @return int - */ - public function length(): int - { - // TODO: Implement length() method. - return $this->channel->count(); - } - - - /** - * @return bool - */ - public function isFull(): bool - { - // TODO: Implement isFull() method. - return $this->channel->count() >= $this->max; - } -} +channel = new \SplQueue(); + } + + + /** + * @return bool + */ + public function isEmpty(): bool + { + // TODO: Implement isEmpty() method. + return $this->channel->count() < 1; + } + + + /** + * @param mixed $data + * @param float $timeout + * @return bool + */ + public function push(mixed $data, float $timeout = -1): bool + { + // TODO: Implement push() method. + $this->channel->enqueue($data); + return true; + } + + + /** + * @param float $timeout + * @return mixed + */ + public function pop(float $timeout = -1): mixed + { + // TODO: Implement pop() method. + return $this->channel->dequeue(); + } + + + /** + * @return array + */ + public function stats(): array + { + // TODO: Implement stats() method. + return []; + } + + + /** + * @return bool + */ + public function close(): bool + { + // TODO: Implement close() method. + return false; + } + + + /** + * @return int + */ + public function length(): int + { + // TODO: Implement length() method. + return $this->channel->count(); + } + + + /** + * @return bool + */ + public function isFull(): bool + { + // TODO: Implement isFull() method. + return $this->channel->count() >= $this->max; + } +} diff --git a/kiri-engine/Cache/ICache.php b/kiri-engine/Redis/CacheInterface.php similarity index 90% rename from kiri-engine/Cache/ICache.php rename to kiri-engine/Redis/CacheInterface.php index f74b9ead..ef07d839 100644 --- a/kiri-engine/Cache/ICache.php +++ b/kiri-engine/Redis/CacheInterface.php @@ -1,65 +1,65 @@ -host = $config['host']; - $this->port = $config['port']; - $this->database = $config['databases']; - $this->auth = $config['auth']; - $this->prefix = $config['prefix']; - $this->timeout = $config['timeout']; - $this->read_timeout = $config['read_timeout']; - $this->pool = $config['pool']; - } - - - public function init() - { - $this->heartbeat_check(); - } - - - - - /** - * @param Kiri\Server\Events\OnWorkerExit $exit - * @return void - */ - public function onWorkerExit(OnWorkerExit $exit) - { - $this->stopHeartbeatCheck(); - } - - - /** - * - */ - public function heartbeat_check(): void - { - - if ($this->_timer === -1) { - $this->_timer = Timer::tick(1000, fn() => $this->waite()); - } - } - - - /** - * @throws Exception - */ - private function waite(): void - { - try { - if ($this->_timer === -1) { - Kiri::getDi()->get(Logger::class)->critical('timer end'); - $this->stopHeartbeatCheck(); - } - if (time() - $this->_last > intval($this->pool['tick'] ?? 60)) { - $this->stopHeartbeatCheck(); - - $this->pdo = null; - } - } catch (\Throwable $throwable) { - error($throwable); - } - } - - - /** - * - */ - public function stopHeartbeatCheck(): void - { - if ($this->_timer > -1) { - Timer::clear($this->_timer); - } - $this->_timer = -1; - } - - - /** - * @param string $name - * @param array $arguments - * @return mixed - * @throws RedisConnectException|RedisException - */ - public function __call(string $name, array $arguments) - { - if (!method_exists($this, $name)) { - return $this->_pdo()->{$name}(...$arguments); - } - return $this->{$name}(...$arguments); - } - - - /** - * @return \Redis - * @throws RedisConnectException - * @throws RedisException - */ - public function _pdo(): \Redis - { - if ($this->_timer === -1) { - $this->heartbeat_check(); - } - $this->_last = time(); - if (!($this->pdo instanceof \Redis) || !$this->pdo->ping('isOk')) { - $this->pdo = $this->newClient(); - } - return $this->pdo; - } - - - /** - * @return \Redis - * @throws RedisConnectException - */ - private function newClient(): \Redis - { - $redis = new \Redis(); - if (!$redis->connect($this->host, $this->port, $this->timeout)) { - throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $this->host, $this->port)); - } - if (!empty($this->auth) && !$redis->auth($this->auth)) { - throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $this->host, $this->auth)); - } - if ($this->read_timeout < 0) { - $this->read_timeout = 0; - } - $redis->select($this->database); - if ($this->read_timeout > 0) { - $redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->read_timeout); - } - $redis->setOption(\Redis::OPT_PREFIX, $this->prefix); - return $redis; - - } - -} +host = $config['host']; + $this->port = $config['port']; + $this->database = $config['databases']; + $this->auth = $config['auth']; + $this->prefix = $config['prefix']; + $this->timeout = $config['timeout']; + $this->read_timeout = $config['read_timeout']; + $this->pool = $config['pool']; + } + + + public function init() + { + $this->heartbeat_check(); + } + + + /** + * + */ + public function heartbeat_check(): void + { + + if ($this->_timer === -1) { + $this->_timer = Timer::tick(1000, fn() => $this->waite()); + } + } + + + /** + * @throws Exception + */ + private function waite(): void + { + try { + if ($this->_timer === -1) { + Kiri::getDi()->get(Logger::class)->critical('timer end'); + $this->stopHeartbeatCheck(); + } + if (time() - $this->_last > intval($this->pool['tick'] ?? 60)) { + $this->stopHeartbeatCheck(); + + $this->pdo = null; + } + } catch (\Throwable $throwable) { + error($throwable); + } + } + + + /** + * + */ + public function stopHeartbeatCheck(): void + { + if ($this->_timer > -1) { + Timer::clear($this->_timer); + } + $this->_timer = -1; + } + + + /** + * @param string $name + * @param array $arguments + * @return mixed + * @throws RedisConnectException|RedisException + */ + public function __call(string $name, array $arguments) + { + if (!method_exists($this, $name)) { + return $this->_pdo()->{$name}(...$arguments); + } + return $this->{$name}(...$arguments); + } + + + /** + * @return \Redis + * @throws RedisConnectException + * @throws RedisException + */ + public function _pdo(): \Redis + { + if ($this->_timer === -1) { + $this->heartbeat_check(); + } + $this->_last = time(); + if (!($this->pdo instanceof \Redis) || !$this->pdo->ping('isOk')) { + $this->pdo = $this->newClient(); + } + return $this->pdo; + } + + + /** + * @return \Redis + * @throws RedisConnectException + */ + private function newClient(): \Redis + { + $redis = new \Redis(); + if (!$redis->connect($this->host, $this->port, $this->timeout)) { + throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $this->host, $this->port)); + } + if (!empty($this->auth) && !$redis->auth($this->auth)) { + throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $this->host, $this->auth)); + } + if ($this->read_timeout < 0) { + $this->read_timeout = 0; + } + $redis->select($this->database); + if ($this->read_timeout > 0) { + $redis->setOption(\Redis::OPT_READ_TIMEOUT, $this->read_timeout); + } + $redis->setOption(\Redis::OPT_PREFIX, $this->prefix); + return $redis; + + } + +} diff --git a/kiri-engine/Cache/Redis.php b/kiri-engine/Redis/Redis.php similarity index 67% rename from kiri-engine/Cache/Redis.php rename to kiri-engine/Redis/Redis.php index 048be94d..7f5eddef 100644 --- a/kiri-engine/Cache/Redis.php +++ b/kiri-engine/Redis/Redis.php @@ -1,191 +1,201 @@ -get(PoolRedis::class); - - $config = $this->get_config(); - - $length = Config::get('cache.redis.pool.max', 10); - - $this->getEventProvider()->on(OnWorkerExit::class, [$this, 'destroy'], 0); - - $connections->initConnections('Redis:' . $config['host'], true, $length); - } - - - /** - * @param $name - * @param $arguments - * @return mixed - * @throws - */ - public function __call($name, $arguments): mixed - { - $time = microtime(true); - if (method_exists($this, $name)) { - $data = $this->{$name}(...$arguments); - } else { - $data = $this->proxy($name, $arguments); - } - if (microtime(true) - $time >= 0.02) { - $this->logger->warning('Redis:' . Json::encode([$name, $arguments]) . (microtime(true) - $time)); - } - return $data; - } - - - /** - * @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 = <<