😺设计模式-工厂模式更新redis使用说明

This commit is contained in:
zyimm 2023-09-05 17:32:09 +08:00
parent 934c7bff78
commit 853e6c3ced

View File

@ -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());
}
}