php 如何检查Redis服务器是否正在运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9866541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:49:05  来源:igfitidea点击:

How to check whether the Redis server is running

phpfuelphp

提问by Chris Harrison

How to check whether the Redis server is running?

如何检查Redis服务器是否正在运行?

If it's not running, I want to fallback to using the database.

如果它没有运行,我想回退到使用数据库。

I'm using the FuelPHP framework, so I'm open to a solution based on this, or just standard PHP.

我正在使用 FuelPHP 框架,所以我愿意接受基于此的解决方案,或者只是标准的 PHP。

采纳答案by Frank de Jonge

What you can do is try to get an instance (\Redis::instance()) and work with it like this:

您可以做的是尝试获取一个实例 (\Redis::instance()) 并像这样使用它:

try
{
    $redis = \Redis::instance();
    // Do something with Redis.
}
catch(\RedisException $e)
{
    // Fall back to other db usage.
}

But preferably you'd know whether redis is running or not. This is just the way to detect it on the fly.

但最好你知道 redis 是否正在运行。这只是动态检测它的方法。

回答by Hassan Azimi

You can use command line to determine if redis is running:

您可以使用命令行来确定 redis 是否正在运行:

redis-cli ping

you should get back

你应该回来

PONG

that indicates redis is up and running.

这表明 redis 已启动并正在运行。

回答by YannҶ

you can do it by this way.

你可以这样做。

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

echo $redis->ping();

and then check if it print +PONG, which show redis-server is running.

然后检查它是否打印+PONG,这表明 redis-server 正在运行。