Home>Article>Backend Development> Code sharing on singleton class implementation of Redis multi-library selection function in PHP

Code sharing on singleton class implementation of Redis multi-library selection function in PHP

黄舟
黄舟 Original
2017-07-27 14:56:14 1234browse

This article mainly introduces the singleton class of the Redis multi-library selection function implemented by PHP, and analyzes the singleton mode of PHP to implement the multi-library selection function of the redis database in the form of examples. Friends in need can refer to it

The example of this article describes the Redis multi-database selection function singleton class implemented in PHP. Share it with everyone for your reference, the details are as follows:

Preface

Some students in the QQ group asked how to select multiple libraries in redis, using PHP I have implemented it, and I hope you can give me more advice

code


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; } } ?>

The above is the detailed content of Code sharing on singleton class implementation of Redis multi-library selection function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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