From 1dc4a8628a1cd465068f5ef9d21c448561919164 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Wed, 27 Dec 2023 17:20:07 +0800 Subject: [PATCH] Support set prefix for redis database --- src/core/Database/RedisConfig.php | 13 +++++++++++++ src/core/Database/RedisPool.php | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/src/core/Database/RedisConfig.php b/src/core/Database/RedisConfig.php index 2236b99..d25e685 100644 --- a/src/core/Database/RedisConfig.php +++ b/src/core/Database/RedisConfig.php @@ -29,6 +29,8 @@ class RedisConfig protected int $dbIndex = 0; + protected string $prefix = ''; + public function getHost(): string { return $this->host; @@ -116,4 +118,15 @@ public function withDbIndex(int $dbIndex): self $this->dbIndex = $dbIndex; return $this; } + + public function getPrefix(): string + { + return $this->prefix; + } + + public function withPrefix(string $prefix): self + { + $this->prefix = $prefix; + return $this; + } } diff --git a/src/core/Database/RedisPool.php b/src/core/Database/RedisPool.php index b965af8..7978641 100644 --- a/src/core/Database/RedisPool.php +++ b/src/core/Database/RedisPool.php @@ -47,6 +47,12 @@ public function __construct(protected RedisConfig $config, int $size = self::DEF if ($this->config->getDbIndex() !== 0) { $redis->select($this->config->getDbIndex()); } + + /* Set Redis options. */ + if ($this->config->getPrefix() !== '') { + $redis->setOption(Redis::OPT_PREFIX, $this->config->getPrefix()); + } + return $redis; }, $size); }