Search code examples
phpexceptionredispredis

How to check if redis server is running using Predis library in PHP


I am integrating Redis cache with one of my website, I am using Predis as client side. I have created a static class for initializing.

I need to check if the redis server is running or not.

I have tried so many thins but it did not work, it is not able to catch the exception.

My code is

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();
}

The above function in a class is not able to catch the error, instead it is showing the error on whole site.

What to do in this situation?


Solution

  • Your try / catch block cannot intercept the exception for a very simple reason: the class Predis\Network\ConnectionException doesn't exist, the correct one is Predis\Connection\ConnectionException (see also here)