qqq
This commit is contained in:
+48
-25
@@ -10,6 +10,7 @@ declare(strict_types=1);
|
|||||||
namespace Kiri\Redis;
|
namespace Kiri\Redis;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
use Kiri;
|
use Kiri;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Events\EventProvider;
|
use Kiri\Events\EventProvider;
|
||||||
@@ -62,28 +63,7 @@ class Redis extends Component
|
|||||||
*/
|
*/
|
||||||
public function init(): void
|
public function init(): void
|
||||||
{
|
{
|
||||||
$config = $this->get_config();
|
|
||||||
|
|
||||||
$length = \config('cache.redis.pool.max', 10);
|
|
||||||
on(OnWorkerExit::class, [$this, 'destroy']);
|
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;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -108,7 +88,6 @@ class Redis extends Component
|
|||||||
* @param $key
|
* @param $key
|
||||||
* @param int $timeout
|
* @param int $timeout
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws RedisException
|
|
||||||
*/
|
*/
|
||||||
public function waite($key, int $timeout = 5): bool
|
public function waite($key, int $timeout = 5): bool
|
||||||
{
|
{
|
||||||
@@ -161,7 +140,7 @@ SCRIPT;
|
|||||||
*/
|
*/
|
||||||
public function destroy(): void
|
public function destroy(): void
|
||||||
{
|
{
|
||||||
Kiri::getPool()->clean($this->host);
|
$this->pool()->clean($this->host);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -182,7 +161,7 @@ SCRIPT;
|
|||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
$response = addError($throwable, 'redis');
|
$response = addError($throwable, 'redis');
|
||||||
} finally {
|
} finally {
|
||||||
Kiri::getPool()->push($this->host, $client);
|
$this->pool()->push($this->host, $client);
|
||||||
}
|
}
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
@@ -195,13 +174,57 @@ SCRIPT;
|
|||||||
*/
|
*/
|
||||||
private function getClient(): \Redis
|
private function getClient(): \Redis
|
||||||
{
|
{
|
||||||
return Kiri::getPool()->get($this->host);
|
return $this->pool()->get($this->host);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Pool
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
protected function pool(): Pool
|
||||||
|
{
|
||||||
|
$pool = Kiri::getPool();
|
||||||
|
if (!$pool->hasChannel($this->host)) {
|
||||||
|
$config = $this->get_config();
|
||||||
|
$length = \config('cache.redis.pool.max', 10);
|
||||||
|
$pool->created($config['host'], $length, $this->connect($config));
|
||||||
|
}
|
||||||
|
return $pool;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $config
|
||||||
|
* @return \Closure
|
||||||
|
*/
|
||||||
|
protected function connect($config): \Closure
|
||||||
|
{
|
||||||
|
return 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;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
#[ArrayShape(['host' => "string", 'port' => "int", 'prefix' => "string", 'auth' => "string", 'databases' => "int", 'timeout' => "int", 'read_timeout' => "int", 'pool' => "array|int[]"])]
|
||||||
public function get_config(): array
|
public function get_config(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
|||||||
Reference in New Issue
Block a user