《PHP學習:PHP實現的Redis多庫選擇功能單例類》要點:
本文介紹了PHP學習:PHP實現的Redis多庫選擇功能單例類,希望對您有用。如果有疑問,可以聯系我們。
PHP實例本文實例講述了PHP實現的Redis多庫選擇功能單例類.分享給大家供大家參考,具體如下:
PHP實例前言
PHP實例qq群里有同學問redis如何進行多庫選擇,用php實現了一下,還望各位多多指點
PHP實例代碼
PHP實例
<?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;
}
}
?>
PHP實例更多關于PHP相關內容感興趣的讀者可查看本站專題:《php+redis數據庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《PHP基本語法入門教程》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP實例希望本文所述對大家PHP程序設計有所幫助.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/419.html