搜尋
首頁php教程PHP源码Redis Client,单例
Redis Client,单例May 23, 2016 pm 05:10 PM

php代码

<?php  //if(!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed...&#39;);

/**
 * ---------------- redis.conf.php ------------------
 * $redis_conf = array();
 * $redis_conf[&#39;master&#39;] = array();;
 * $redis_conf[&#39;master&#39;][&#39;host&#39;] = &#39;127.0.0.1&#39;;
 * $redis_conf[&#39;master&#39;][&#39;port&#39;] = 6379;
 * $redis_conf[&#39;master&#39;][&#39;passwd&#39;] = &#39;&#39;;
 * $redis_conf[&#39;master&#39;][&#39;timeout&#39;] = 1;
 * ---------------- author:  simon ------------------
 */

/**
 * Redis Client Interface
 *
 * ------------------- Usage ---------------------
 * $conf_dir = __DIR__ .&#39;/../conf/redis.conf.php&#39;;
 * $redis = RedisCli::getInstance($conf_dir, &#39;master&#39;);
 * $ret = $redis->set(&#39;xxxxx&#39;, &#39;good&#39;);
 * $ret = $redis->get(&#39;xxxxx&#39;);
 *
 */

class RedisCli {
	private static $_instance = array();
	private $_redis = false;
	
	/**
	 * 单例模型,构造函数
	 */
	public static function getInstance($conf_dir=&#39;/xxx/redis.conf.php&#39;, $serverName=&#39;master&#39;) {
		if (isset(self::$_instance[$serverName])) {
			return self::$_instance[$serverName];
		}
		require_once($conf_dir);
		if (!isset($redis_conf[$serverName])) {
			throw new Exception("param serverName Or redis config error...");
		}
		$conf = $redis_conf[$serverName];
		if (!class_exists(&#39;Redis&#39;)) {
			throw new Exception("Class Redis not exists, please install the php Redis extension...");
		}
		if (isset($conf[&#39;host&#39;]) && isset($conf[&#39;port&#39;]) && isset($conf[&#39;passwd&#39;]) && isset($conf[&#39;timeout&#39;])) {
			self::$_instance[$serverName] = new self($conf[&#39;host&#39;], $conf[&#39;port&#39;], $conf[&#39;passwd&#39;], $conf[&#39;timeout&#39;]);
			return self::$_instance[$serverName];
		}
		return false;
	}

	/**
	 *  私有, 单例模型,禁止外部构造
	 */
	private function __construct($host, $port, $passwd, $timeout) {
		$this->_redis = new Redis();
		if ($this->_redis->connect($host, $port, $timeout)) {
			if (!empty($passwd)) {
				$this->_redis->auth($passwd);
			}
		}
	}

	/**
	 *  私有, 单例模型,禁止克隆
	 */
	private function __clone() {
	}

    /**
	 *  公有,调用对象函数
	 */
	public function __call($method, $args) {
		if (!$this->_redis || !$method) {
			return false;
		}
		if (!method_exists($this->_redis, $method)) {
			throw new Exception("Class RedisCli not have method ($method) ");
		}
		return call_user_func_array(array($this->_redis, $method), $args);
	}
}

?>
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境