Home > Backend Development > PHP Tutorial > Redis multi-database selection function singleton class implemented in PHP (detailed explanation)

Redis multi-database selection function singleton class implemented in PHP (detailed explanation)

墨辰丷
Release: 2023-03-26 18:00:02
Original
1864 people have browsed it

This article mainly introduces the singleton class of the Redis multi-library selection function implemented by PHP. It analyzes the singleton mode of PHP to implement the multi-library selection function of the redis database based on the example form. Friends who need it can refer to it

The details are as follows:

Preface

Code

<?php
class MultiRedisConnect
{
  /**
   * hostname
   *
   * @var string
   */
  const REDISHOSTNAME = "127.0.0.1";
  /**
   * port
   *
   * @var int
   */
  const REDISPORT = 6379;
  /**
   * timeout
   *
   * @var int
   */
  const REDISTIMEOUT = 0;
  /**
   * password
   *
   * @var string
   */
  const REDISPASSWORD = "123456";
  /**
   * 类单例数组
   *
   * @var array
   */
  private static $instance = array();
  /**
   * redis连接句柄
   *
   * @var object
   */
  private $redis;
  /**
   * hash的key
   *
   * @var int
   */
  private $hash;
  /**
   * 私有化构造函数,防止类外实例化
   *
   * @param int $dbnumber
   */
  private function __construct ($dbnumber)
  {
    $dbnumber = (int) $dbnumber;
    $this->hash = $dbnumber;
    $this->redis = new Redis();
    $this->redis->connect(self::REDISHOSTNAME, self::REDISPORT, self::REDISTIMEOUT);
    $this->redis->auth(self::REDISPASSWORD);
    $this->redis->select($dbnumber);
  }
  private function __clone ()
  {}
  /**
   * 获取类单例
   *
   * @param int $dbnumber
   * @return object
   */
  public static function getRedisInstance ($dbnumber)
  {
    $hash = (int) $dbnumber;
    if (! isset(self::$instance[$hash])) {
      self::$instance[$hash] = new MultiRedisConnect($dbnumber);
    }
    return self::$instance[$hash];
  }
  /**
   * 获取redis的连接实例
   *
   * @return object
   */
  public function getRedisConnect ()
  {
    return $this->redis;
  }
  /**
   * 关闭单例时做清理工作
   */
  public function __destruct ()
  {
    $key = $this->hash;
    self::$instances[$key]->redis->close();
    self::$instances[$key] = null;
  }
}
?>
Copy after login

Related recommendations:

php implementationredisDefinition and use of cache classes

php uses redisWhat are the steps for long connections

##Laravel uses Redis to share the Session steps in detail

The above is the detailed content of Redis multi-database selection function singleton class implemented in PHP (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template