如何使用 PHP 中的 Predis 库检查 redis 服务器是否 运行
How to check if redis server is running using Predis library in PHP
我正在将 Redis 缓存与我的网站之一集成,我正在使用 Predis 作为客户端。我为初始化创建了一个静态 class。
我需要检查redis服务器是否运行。
我试了很多方法都没有用,它无法捕获异常。
我的密码是
public static function checkRedisConnection()
{
self::initialize();
$client = new Predis\Client();
try
{
$client->connect();
}
catch (Predis\Network\ConnectionException $exception)
{
exit("whoops, couldn't connect to the remote redis instance!");
}
$client->info();
}
class 中的上述函数无法捕获错误,而是在整个站点上显示错误。
遇到这种情况怎么办?
你的 try / catch 块无法拦截异常,原因很简单:class Predis\Network\ConnectionException
不存在,正确的是 Predis\Connection\ConnectionException
(see also here)
我正在将 Redis 缓存与我的网站之一集成,我正在使用 Predis 作为客户端。我为初始化创建了一个静态 class。
我需要检查redis服务器是否运行。
我试了很多方法都没有用,它无法捕获异常。
我的密码是
public static function checkRedisConnection()
{
self::initialize();
$client = new Predis\Client();
try
{
$client->connect();
}
catch (Predis\Network\ConnectionException $exception)
{
exit("whoops, couldn't connect to the remote redis instance!");
}
$client->info();
}
class 中的上述函数无法捕获错误,而是在整个站点上显示错误。
遇到这种情况怎么办?
你的 try / catch 块无法拦截异常,原因很简单:class Predis\Network\ConnectionException
不存在,正确的是 Predis\Connection\ConnectionException
(see also here)