3

嗨,我有以下内容:

// Named array of connection parameters:
    $redis = new Predis\Client([
        'scheme' => 'tcp',
        'host'   => $host,
        'port'   => $port,
        'password'   => $auth,
    ]);

    try {
        // explicitly call Predis\Client::connect()
        $redis->connect();
    }
    catch (Exception $e) {
        return Redirect::to('profile')
                ->with('result', '<div class="col-lg-12"><div class="alert alert-danger alert-border-left alert-gradient alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <i class="fa fa-check pr10"></i>
                <strong>Error - '.$e.'</strong> Unable to connect to redis server, please double check your database detals.</div></div>');
    }

但是,这并没有捕获诸如 NOAUTH 之类的错误。

有人可以指出我正确的方向吗?

4

1 回答 1

5

我广泛阅读了有关此问题的信息,尽管我的配置不同,因为它使用了多个服务器,并且我试图解决的问题是服务器故障,但这似乎已经为我完成了:

 try {
      $client = new Predis\Client($params);
      $client->connect();
 } catch(Predis\Connection\ConnectionException $e) {
      echo $e->getMessage();
 }

我尝试了许多变化,这似乎阻止了应用程序的所有尖叫。在我的情况下,我传入了一组服务器并尝试 ping 它们并删除失败的服务器。这似乎有点矫枉过正,但如果不这样做,我怎么知道服务器是否关闭?

此外,如果您不尝试连接,那么它将在其他地方失败,也就是说,连接是惰性的,并且会在实际需要时调用 - 对我来说,这总是导致连接错误。在使用它之前自己调用它可以让我捕捉到异常。然后我必须找出异常为什么不做,不得不将它切换到 Predis\Connection\ConnectionException。这比原本应该的要难得多。

于 2015-03-11T19:24:56.437 回答