【发布时间】:2018-05-19 19:50:54
【问题描述】:
我正在使用 phpredis 连接到 Redis 服务器 (https://github.com/phpredis/phpredis)。我为我的应用程序编写了一个非常基本的 redis 客户端,它应该只接受 env 参数并连接到 redis 服务器。我的 redis 客户端如下所示:
<?php
use \Redis
class Redis
{
/** @var Redis */
private $redis;
/**
* @param RedisServer $redis
* @param string $redisHost
* @param string $redisPort
*/
public function __construct(RedisServer $redis, string $redisHost, string $redisPort)
{
$this->redis = $redis;
if (!$this->redis->connect($redisHost, $redisPort)) {
throw new ConnectionException('Failed to connection to redis server', 500);
}
}
/**
* Close connection when Redis service is destroyed
*/
public function __destruct()
{
$this->redis->close();
$this->redis = null;
}
//....
}
是否需要在 __destruct() 中显式关闭 Redis 连接?
提前致谢!
【问题讨论】: