From f20f25e912d1422f50ced8eea94979cec5394a2a Mon Sep 17 00:00:00 2001 From: kriss <462679766@qq.com> Date: Tue, 21 Jun 2022 10:44:24 +0800 Subject: [PATCH] Support prefix --- src/Redis.php | 3 ++- src/RedisConnection.php | 5 ++++- src/config/plugin/webman/redis-queue/redis.php | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Redis.php b/src/Redis.php index 4319405..21e697a 100644 --- a/src/Redis.php +++ b/src/Redis.php @@ -50,7 +50,7 @@ protected static function connect($config) if (!extension_loaded('redis')) { throw new \RuntimeException('Please make sure the PHP Redis extension is installed and enabled.'); } - + $redis = new RedisConnection(); $address = $config['host']; $config = [ @@ -60,6 +60,7 @@ protected static function connect($config) 'auth' => $config['options']['auth'] ?? '', 'timeout' => $config['options']['timeout'] ?? 2, 'ping' => $config['options']['ping'] ?? 55, + 'prefix' => $config['options']['prefix'] ?? '', ]; $redis->connectWithConfig($config); return $redis; diff --git a/src/RedisConnection.php b/src/RedisConnection.php index 8a2c9fa..074e4fa 100644 --- a/src/RedisConnection.php +++ b/src/RedisConnection.php @@ -19,7 +19,7 @@ class RedisConnection extends \Redis { /** - * @var array + * @var array */ protected $config = []; @@ -42,6 +42,9 @@ public function connectWithConfig(array $config = []) if (!empty($this->config['db'])) { $this->select($this->config['db']); } + if (!empty($this->config['prefix'])) { + $this->setOption(\Redis::OPT_PREFIX, $this->config['prefix']); + } if (Worker::getAllWorkers() && !$timer) { $timer = Timer::add($this->config['ping'] ?? 55, function () { $this->execCommand('ping'); diff --git a/src/config/plugin/webman/redis-queue/redis.php b/src/config/plugin/webman/redis-queue/redis.php index 813ebcd..0876302 100644 --- a/src/config/plugin/webman/redis-queue/redis.php +++ b/src/config/plugin/webman/redis-queue/redis.php @@ -5,6 +5,7 @@ 'options' => [ 'auth' => null, // 密码,字符串类型,可选参数 'db' => 0, // 数据库 + 'prefix' => '', // key 前缀 'max_attempts' => 5, // 消费失败后,重试次数 'retry_seconds' => 5, // 重试间隔,单位秒 ]