封装单例模式的Redis持久链接

设置Redis链接信息

修改Config.php的User config,加入以下信息

  1. "REDIS"=>array(
  2. "HOST"=>'ip',
  3. "PORT"=>port,
  4. "AUTH"=>'password'
  5. )

Redis class

  1. namespace App\Vendor\Db;
  2. use Conf\Config;
  3. class Redis
  4. {
  5. private static $instance;
  6. private $con;
  7. static function getInstance(){
  8. if(!isset(self::$instance)){
  9. self::$instance = new Redis();
  10. }
  11. return self::$instance;
  12. }
  13. function __construct()
  14. {
  15. $conf = Config::getInstance()->getConf("REDIS");
  16. $this->con = new \Redis();
  17. $this->con->connect($conf['HOST'],$conf['PORT']);
  18. $this->con->auth($conf['AUTH']);
  19. $this->con->setOption(\Redis::OPT_SERIALIZER,\Redis::SERIALIZER_PHP);
  20. }
  21. function getConnect(){
  22. return $this->con;
  23. }
  24. }

Redis 高版本的单机版已经在内部处理了断线问题。