How to check whether the Redis server is running

后端 未结 3 1867
我在风中等你
我在风中等你 2020-12-15 03:59

How to check whether the Redis server is running?

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

I\'m using the FuelPHP framework, so I\'m o

相关标签:
3条回答
  • 2020-12-15 04:22

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

    redis-cli ping
    

    you should get back

    PONG
    

    that indicates redis is up and running.

    0 讨论(0)
  • 2020-12-15 04:24

    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.

    0 讨论(0)
  • 2020-12-15 04:25

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

    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.

    0 讨论(0)
提交回复
热议问题