This article mainly introduces the implementation of the Redis multi-Library selection of PHP single-case class, combined with the example of PHP to use a single-case model to achieve the Redis database multi-Library selection function, the need for friends can refer to the next
In this paper, we describe the Redis multi-Library selection function of PHP implementation of the Singleton class. Share to everyone for your reference, as follows:
Objective
QQ Group has the same learning Redis how to make a multi-Library selection, with PHP to achieve a bit, but also hope that you have a lot of guidance
Code
<?phpclass 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"; /** * Class Singleton array * * @var array */private static $instance = Array (); /** * REDIS Connection Handle * * @var Object */Private $redis; /** * Hash Key * * @var int */private $hash; /** * Privatisation constructor to prevent out-of-class instantiation * * @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 () {}/** * Get class Singleton * * @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]; /** * Gets the connection instance of Redis * * @return Object */Public function Getredisconnect () {return $this->redis; /** * Cleanup work when closing a singleton */Public Function __destruct () {$key = $this->hash; Self:: $instances [$key]->redis->close (); Self:: $instances [$key] = null; }}?>