The Redis multi-Library selection function of PHP implements a single-class code share

Source: Internet
Author: User
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; }}?>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.