php redis 分布式类

配置:

$redis_config = array(
	'prefix'	=> 'ylmall_',
	'master'	=> array(
		'host'	=> "192.168.1.19",
		'port'	=> "6379",
	)
);

类文件:


class cls_redis {
	/**
	 *
	 * @var $_configMaster master config,default to empty array
	 */
	private $_configMaster = array ();
	
	/**
	 *
	 * @var $_configSlave slave config,default to empty array
	 */
	private $_configSlave = array ();
	
	/**
	 *
	 * @var $_redisKeyPrefix the redis key prefix
	 */
	private $_redisKeyPrefix = '';
	private $debug = false;
	public $queries = array ();
	
	/**
	 *
	 * @ignore
	 *
	 */
	function __construct($redis_config) {
		$this->_configMaster = $redis_config ['master'];
		// $this->_configSlave = $redis_config['slave'];
		$this->_redisKeyPrefix = $redis_config ['prefix'];
	}
	
	/**
	 * Get redis key prefix
	 *
	 * @return string
	 */
	public function getKeyPrefix() {
		return $this->_redisKeyPrefix;
	}
	
	/**
	 * Set redis key prefix
	 *
	 * @return
	 *
	 */
	public function setKeyPrefix($prefix) {
		$this->_redisKeyPrefix = $prefix;
	}
	
	/**
	 *
	 * @var $_redisMaster redis master,default to null
	 */
	private $_redisMaster = null;
	
	/**
	 * Get redis master server.If fail,throw ErrorException.
	 *
	 * @return Redis
	 */
	public function getRedisMaster() {
		if ($this->_redisMaster instanceof Redis) {
			return $this->_redisMaster;
		} else {
			$this->_redisMaster = new Redis ();
			try {
				$this->_redisMaster->connect ( $this->_configMaster ['host'], $this->_configMaster ['port'] );
				$this->_redisMaster->setOption ( Redis::OPT_PREFIX, $this->_redisKeyPrefix );
			} catch ( Exception $e ) {
				// $this->errorShow ();
				throw new ErrorException ( "Connect redis server " . implode ( ",", $this->_configMaster ) . " fail !" );
			}
			return $this->_redisMaster;
		}
	}
	/**
	 *
	 * @var $_redisSlave redis slave,default to null
	 */
	private $_redisSlave = null;
	
	/**
	 * Get redis salve server.If fail,throw a ErrorException.
	 *
	 * @return Redis
	 */
	public function getRedisSlave() {
		if ($this->_redisSlave instanceof Redis) {
			return $this->_redisSlave;
		} else {
			$this->_redisSlave = new Redis ();
			try {
				// $this->_redisSlave->connect($this->_configSlave['host'], $this->_configSlave['port']);
				// $this->_redisSlave->setOption(Redis::OPT_PREFIX, $this->_redisKeyPrefix);
				$this->_redisSlave->connect ( $this->_configMaster ['host'], $this->_configMaster ['port'] );
				$this->_redisSlave->setOption ( Redis::OPT_PREFIX, $this->_redisKeyPrefix );
			} catch ( Exception $e ) {
				// $this->errorShow();
				// throw new ErrorException("Connect redis server " . implode(",", $this->_configSlave) . " fail !");
				$this->errorShow ();
				throw new ErrorException ( "Connect redis server " . implode ( ",", $this->_configMaster ) . " fail !" );
			}
			
			return $this->_redisSlave;
		}
	}
	
	/**
	 *
	 * @var $_cmdScopeMaster master sever command scope
	 */
	private static $_cmdScopeMaster = array (
			'multi',
			'exec',
			'discard',
			'watch',
			'unwatch',
			// key - value structure
			'setex',
			'psetex',
			'setnx',
			'del',
			'delete',
			'incr',
			'incrBy',
			'incrByFloat',
			'decr',
			'decrBy',
			// list structrue
			'lPush',
			'rPush',
			'lPushx',
			'rPushx',
			'lSet',
			'lRem',
			'lRemove',
			'lInsert',
			'lTrim',
			'listTrim',
			// set structrue
			'sAdd',
			'sRem',
			'sRemove',
			'sMove',
			'sPop',
			// hash structrue
			'hSet',
			'hSetNx',
			'hDel',
			'hIncrBy',
			'hIncrByFloat',
			'hMset',
			// transaction
			'multi',
			'exec',
			// sorted set structrue
			'zAdd',
			'zDelete',
			'zDeleteRangeByRank',
			'zCount',
			'zRange',
			'zRangeByScore',
			'expire',
			// server
			'info' 
	);
	
	/**
	 * set master server commadn scope
	 *
	 * @param array $cmds        	
	 * @return void
	 */
	public function setCmdScopeMaster(array $cmds) {
		self::$_cmdScopeMaster = array_unique ( array_merge ( self::$_cmdScopeMaster, $cmds ) );
	}
	
	/**
	 *
	 * @var $_cmdScopeSlave slave sever command scope
	 */
	private static $_cmdScopeSlave = array (
			// key - value structure
			'exists',
			'mGet',
			'getMultiple',
			// list structure
			'lPop',
			'rPop',
			'blPop',
			'brPop',
			'lSize',
			'lIndex',
			'lGet',
			'lRange',
			'lGetRange',
			// set structrue
			'sIsMember',
			'sContains',
			'sCard',
			'sSize',
			'sRandMember',
			'sMembers',
			// hash structrue
			'hGetAll',
			'hGet',
			'hLen',
			'hKeys',
			'hVals',
			'hExists',
			'hMGet',
			// sorted set structrue
			'zRevRange',
			'zRevRangeByScore' 
	);
	
	/**
	 * set slave server commadn scope
	 *
	 * @param array $cmds        	
	 * @return void
	 */
	public function setCmdScopeSlave(array $cmds) {
		self::$_cmdScopeSlave = array_unique ( array_merge ( self::$_cmdScopeSlave, $cmds ) );
	}
	
	/**
	 * set a key value
	 *
	 * @param string $key        	
	 * @param mixed $value        	
	 * @param int $expire        	
	 * @return bool
	 */
	public function set($key, $value, $expire = 0) {
		if ($this->debug) {
			$this->queries [] = "custom set : $key $value";
		}
		$value = serialize ( $value );
		$this->getRedisMaster ();
		if ($expire) {
			return $this->_redisMaster->setex ( $key, $expire, $value );
		} else {
			return $this->_redisMaster->set ( $key, $value );
		}
	}
	
	/**
	 * Get the value of a key
	 *
	 * @param string $key        	
	 * @return mixed
	 */
	public function get($key) {
		if ($this->debug) {
			$this->queries [] = "custom get : $key";
		}
		$this->getRedisSlave ();
		return unserialize ( $this->_redisSlave->get ( $key ) );
	}
	
	/**
	 * Call Redis method use master or slave instance.If fail,throw a ErrorException.
	 *
	 * @return
	 *
	 */
	public function __call($name, $args) {
		if ($this->debug) {
			$this->queries [] = "call method : $name " . implode ( ',', $args );
		}
		
		if (in_array ( $name, self::$_cmdScopeMaster )) {
			$this->getRedisMaster ();
			return call_user_func_array ( array (
					$this->_redisMaster,
					$name 
			), $args );
		} elseif (in_array ( $name, self::$_cmdScopeSlave )) {
			$this->getRedisSlave ();
			return call_user_func_array ( array (
					$this->_redisSlave,
					$name 
			), $args );
		} else {
			throw new ErrorException ( "It is an invalidate method : {$name}!" );
		}
	}
	
	/**
	 * Set redis resource to null when serializing
	 */
	public function __sleep() {
		$this->_redisMaster = $this->_redisSlave = null;
	}
	
	/**
	 * Set redis resource to null when destruct
	 */
	public function __destruct() {
		$this->_redisMaster = $this->_redisSlave = null;
	}
	public function errorShow() {
	}
}

  

      $redis = new Redis();
      $redis->pconnect("192.168.1.19", 6379);


      $redis->zadd();

      $time = time();
      $list = $redis->zRangeByScore("powerlist",0,1441951035);
 
原文地址:https://www.cnblogs.com/icyy/p/4584345.html