From 853e6c3cedc16cf3f8f65928f088841e3f1c4526 Mon Sep 17 00:00:00 2001 From: zyimm Date: Tue, 5 Sep 2023 17:32:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=BA=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-=E5=B7=A5=E5=8E=82=E6=A8=A1=E5=BC=8F=E6=9B=B4?= =?UTF-8?q?=E6=96=B0redis=E4=BD=BF=E7=94=A8=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../编程/设计模式/行为模式/factory-method.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/_posts/编程/设计模式/行为模式/factory-method.md b/_posts/编程/设计模式/行为模式/factory-method.md index 6fa4559..852a8aa 100644 --- a/_posts/编程/设计模式/行为模式/factory-method.md +++ b/_posts/编程/设计模式/行为模式/factory-method.md @@ -35,6 +35,8 @@ class Config private string $userName;// 用户名 + private int $port; //端口 + public function getHost(): string { return $this->host; @@ -78,6 +80,18 @@ class Config $this->userName = $userName; return $this; } + + + public function getPort(): string + { + return $this->port; + } + + public function setPort(int $port): static + { + $this->port = $port; + return $this; + } } // 工厂类 @@ -111,6 +125,8 @@ class Cache class Redis extends Cache { + private Redis $redis + public function __construct(Config $config) { parent::__construct(Config $config); @@ -121,6 +137,14 @@ class Redis extends Cache protected function connect() { //根据Config进行缓存连接 + $redis = new \Redis(); + + // 连接 Redis 服务器 + $redis->connect($this->config->getHost() , $this->config->getPort() ?? 6379); + + // 可选:设置 Redis 密码 + $redis->auth($this->config->getPassword()); + } }