gpt4 book ai didi

redis - 拉维尔 : How to Check Redis Availability?

转载 作者:IT王子 更新时间:2023-10-29 06:01:50 25 4
gpt4 key购买 nike

如何在 Laravel 5.4 中检查 Redis 连接的可用性。我尝试了下面的代码,但 ping 线出现异常。如果没有连接 Redis,我该怎么做才能避免异常?

No connection could be made because the target machine actively refused it. [tcp://127.0.0.1:6379]

use Redis;

class SocketController extends Controller
{
public function sendMessage(){
$redis = Redis::connection();

if($redis->ping()){
print_r("expression");
}
else{
print_r("expression2");
}
}
}

也试过这个:

$redis = Redis::connection();
try{
$redis->ping();
} catch (Exception $e){
$e->getMessage();
}

但无法捕获异常

最佳答案

如果你正在使用 predis ,那么它会抛出 Predis\Connection\ConnectionException

Error while reading line from the server. [tcp://127.0.0.1:6379] <--when redis is not connected

所以捕获那个异常,你会知道你的 redis 是否连接。

use Illuminate\Support\Facades\Redis;

public function redis_test(Request $request){
try{
$redis=Redis::connect('127.0.0.1',3306);
return response('redis working');
}catch(\Predis\Connection\ConnectionException $e){
return response('error connection redis');
}

关于redis - 拉维尔 : How to Check Redis Availability?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45582882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com