search
Homephp教程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);
	}
}

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.