A
A
Albert Kazan2019-03-13 19:46:07
PHP
Albert Kazan, 2019-03-13 19:46:07

How to ping Redis in PHP?

Just started using Redis. Did a lot of things that are cached. It seems as it should be, there is a key-value in redis, we issue it, or we take it from the database, write it to redis and issue it. I use Predis as a PHP wrapper. But I can’t make the redis part be ignored if there is no redis on the server (it is not yet on the main server, but the code needs to be updated soon).

Predis\Autoloader::register();
    self::$redis = new Predis\Client([
      'scheme' => 'tcp',
      'host' => REDIS_HOST ?: '127.0.0.1',
      'port' => REDIS_PORT ?: '6379',
    ]);
    try {
      self::$redis->ping();
      self::$available = true;
    } catch (\Exception $e) {
      self::$available = false;
    }

I always end up with an error
Fatal error: in C:\OSPanel\domains\gene.com\vendor\predis\predis\src\Connection\AbstractConnection.php on line 155

Just in case, I put everything in my shell to work with it.
Full class here
namespace Superior;

use \Predis as Predis;

class Redis {
  static public $redis;
  static private $available = false;
  
  static public function k($key) {
    if (self::$redis) {
      return self::$redis->get($key);
    }
    return false;
  }
  
  static public function ss($key, $value, $time = 604800) {
    if (self::$redis) {
      self::$redis->set($key, $value);
      self::$redis->expire($key, $time);
    }
  }
  
  static public function SetUp() {
    Predis\Autoloader::register();
    self::$redis = new Predis\Client([
      'scheme' => 'tcp',
      'host' => REDIS_HOST ?: '127.0.0.1',
      'port' => REDIS_PORT ?: '6379',
    ]);
    try {
      self::$redis->ping();
      self::$available = true;
    } catch (\Exception $e) {
      self::$available = false;
    }
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question