PHP實現(xiàn)的Redis多庫選擇功能單例類
本文實例講述了PHP實現(xiàn)的Redis多庫選擇功能單例類。分享給大家供大家參考,具體如下:
前言
qq群里有同學問redis如何進行多庫選擇,用php實現(xiàn)了一下,還望各位多多指點
代碼
<?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";
/**
* 類單例數(shù)組
*
* @var array
*/
private static $instance = array();
/**
* redis連接句柄
*
* @var object
*/
private $redis;
/**
* hash的key
*
* @var int
*/
private $hash;
/**
* 私有化構造函數(shù),防止類外實例化
*
* @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+redis數(shù)據(jù)庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
- php Redis函數(shù)用法實例總結【附php連接redis單例類】
- php操作redis中的hash和zset類型數(shù)據(jù)的方法和代碼例子
- PHP實現(xiàn)操作redis的封裝類完整實例
- php 使用redis鎖限制并發(fā)訪問類示例
- php實現(xiàn)的redis緩存類定義與使用方法示例
- PHP實現(xiàn)的Redis操作通用類示例
- PHP操作redis實現(xiàn)的分頁列表,新增,刪除功能封裝類與用法示例
- PHP基于redis計數(shù)器類定義與用法示例
- PHP購物車類Cart.class.php定義與用法示例
- php實現(xiàn)仿寫CodeIgniter的購物車類
- PHP+redis實現(xiàn)的購物車單例類示例
相關文章
比較strtr, str_replace和preg_replace三個函數(shù)的效率
本篇文章是對strtr, str_replace和preg_replace三個函數(shù)的效率問題進行了詳細的分析介紹,需要的朋友參考下2013-06-06
php正則匹配html中帶class的div并選取其中內容的方法
這篇文章主要介紹了php正則匹配html中帶class的div并選取其中內容的方法,涉及curl的使用及正則匹配的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01

