diff --git a/CHANGELOG.md b/CHANGELOG.md index 13ee673..dce257e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Fixed: Changed: * MR swoole/library#160 : Allow to pass array key/index to the callback function of function _\Swoole\Coroutine::map()_. (by @maxiaozhi ) +* MR swoole/library#166 : Support configurable options for _Redis_. (by @sy-records ) * Improved type declarations and return types. ## 5.1.1 (2023-11-26) diff --git a/src/core/Database/RedisConfig.php b/src/core/Database/RedisConfig.php index b6161b8..a6049e4 100644 --- a/src/core/Database/RedisConfig.php +++ b/src/core/Database/RedisConfig.php @@ -29,6 +29,9 @@ class RedisConfig protected int $dbIndex = 0; + /** + * @var array + */ protected array $options = []; public function getHost(): string @@ -119,18 +122,31 @@ public function withDbIndex(int $dbIndex): self return $this; } + /** + * Add a configurable option. + */ public function withOption(int $option, mixed $value): self { $this->options[$option] = $value; return $this; } + /** + * Add/override configurable options. + * + * @param array $options + */ public function setOptions(array $options): self { $this->options = $options; return $this; } + /** + * Get configurable options. + * + * @return array + */ public function getOptions(): array { return $this->options; diff --git a/src/core/Database/RedisPool.php b/src/core/Database/RedisPool.php index 45dbb67..7ef7c75 100644 --- a/src/core/Database/RedisPool.php +++ b/src/core/Database/RedisPool.php @@ -49,10 +49,8 @@ public function __construct(protected RedisConfig $config, int $size = self::DEF } /* Set Redis options. */ - if ($this->config->getOptions() !== []) { - foreach ($this->config->getOptions() as $key => $value) { - $redis->setOption($key, $value); - } + foreach ($this->config->getOptions() as $key => $value) { + $redis->setOption($key, $value); } return $redis;