Rumah 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);
	}
}

?>
Salin selepas log masuk
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

Video Face Swap

Video Face Swap

Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

<🎜>: Bubble Gum Simulator Infinity - Cara Mendapatkan dan Menggunakan Kekunci Diraja
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Sistem Fusion, dijelaskan
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers of the Witch Tree - Cara Membuka Kunci Cangkuk Bergelut
3 minggu yang lalu By 尊渡假赌尊渡假赌尊渡假赌

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas

Tutorial Java
1665
14
Tutorial PHP
1270
29
Tutorial C#
1249
24