Home > Backend Development > PHP Tutorial > redis operation class (supports master-slave)

redis operation class (supports master-slave)

WBOY
Release: 2016-07-29 09:05:18
Original
1133 people have browsed it

Basic configuration file:

<?php
$CONFIG_REDIS_DEFAULT = array(
	'master' => array('127.0.0.1',6379),
	'slaver'  => array('127.0.0.1',6379)
);

?>
Copy after login

Operation class:

<?php

class FSRedis {
	
	const RD_MASTER = 'master';
	const RD_SLAVER = 'slaver';
	
	//默认key
	private static $mKey = '';

	//实例对象
	private static $mInstance = array();

	//服务器配置
	private $_serverConfig = array(); 

	private $_redisMaster;
	private $_redisSlaver;
	
	
	public static function Instance($defaultConfig = array()) {
		self::$mKey = md5(serialize($defaultConfig));
		if (isset( self::$mInstance[self::$mKey])) {
			return self::$mInstance[self::$mKey];
		}
		return self::$mInstance[self::$mKey] = new FSRedis($defaultConfig);
	}
	
	public function __construct($defaultConfig = array()) {
		$this->_serverConfig = $defaultConfig;
	}	

	private function createRedisInstance($config = array()) {
		if (count($config) < 2) {
			throw new Exception(strtr('Config invalid', array()));
		}
		$redis = new Redis();
		$redis->connect($config[0], $config[1]);
		return $redis;
	}	
	
	public function getRedisMaster() {
		if ($this->_redisMaster === null) {
			$this->_redisMaster = $this->createRedisInstance($this->_serverConfig[self::RD_MASTER]) ;			
		}
		return $this->_redisMaster;
	}

	public function getRedisSlave() {
		if ($this->_redisSlaver === null) {
			$this->_redisSlaver = $this->createRedisInstance($this->_serverConfig[self::RD_SLAVER]) ;			
		}
		return $this->_redisSlaver;
	}
	
	public function get($key, $master = false) {
		if(empty($key)) {
			return false;
		}
		$func = is_array($key) ? 'mGet' : 'get';
		if($master) {
			$redis = $this->getRedisMaster();
		}else{
			$redis = $this->getRedisSlave();
		}
		if(!$redis) {
			return false;
		}
		$value = $redis->$func($key);
		if($value === false) {			
			return false;
		}
		return $value;
	}

	public function set($key, $value, $expire = 0) {
		if(empty($key)) {			
			return false;
		}
		$redis = $this->getRedisMaster();		
		if(!$redis) {
			return false;
		}
		if($expire) {
			$ret = $redis->setex($key, $expire, $value);
		}
		else {
			$ret = $redis->set($key, $value);
		}
		if($ret === false) {
			return false;
		}
		return $ret;
	}

    
    /**
     * 条件形式设置缓存,如果 key 不存时就设置,存在时设置失败
     *
     * @param string $key 缓存KEY
     * @param string $value 缓存值
     * @return boolean
     */
	public function setnx($key, $value){
        return $this->getRedisMaster()->setnx($key, $value);
    }
       
    /**
     * 删除缓存
     *
     * @param string || array $key 缓存KEY,支持单个健:"key1" 或多个健:array('key1','key2')
     * @return int 删除的健的数量
     */
	 public function remove($key){
		return $this->getRedisMaster()->delete($key);
    }
       
    public function __call($name,$value) {
		/**
		 * 读写分离
		 */
		$redisWrite = array('zAdd','zRem','hIncrBy','info','rename', 'rpush', 'lpop','hMset', 'sadd', 'srem', 'incr', 'spop');
		$redisRead = array('zCard','zRank','zrevRank','zRevRange','hLen','hKeys','hVals','zRange','hMget','hGetAll', 'lrange', 'llen','mGet', 'smembers', 'sismember', 'scard', 'keys');
		if(in_array($name,$redisWrite)) { //master
			$redis = $this->getRedisMaster();
			return call_user_func_array(array($redis,$name),$value);
		}
		elseif(in_array($name,$redisRead)) { //slave		
			$redis = $this->_getRedisSlave();
			return call_user_func_array(array($redis,$name),$value);
		}
	}
}

?>
Copy after login

The above introduces the redis operation class (supports master-slave), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template