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);
php redis 分布式类的更多相关文章
- redis分布式工具类 ----RedisShardedPoolUtil
这个是redis分布式的工具类,看非分布式的看 这里 说一下redis的分布式,分布式,无疑,肯定不是一台redis服务器.假如说,我们有两台redis服务器,一个6379端口,一个6380端口.那 ...
- c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具
c#实例化继承类,必须对被继承类的程序集做引用 0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...
- Redis分布式锁的实现以及工具类
一.应用场景: 本文应用的场景为在查询数据时,发现数据不存在此时就需要去查询数据库并且更新缓存,此时可能存在高并发的请求同时打在数据库上,而针对这种情况必须要给这些请求加锁,故而采用了分布式锁的方式. ...
- 一致性Hash算法在Redis分布式中的使用
由于redis是单点,但是项目中不可避免的会使用多台Redis缓存服务器,那么怎么把缓存的Key均匀的映射到多台Redis服务器上,且随着缓存服务器的增加或减少时做到最小化的减少缓存Key的命中率呢? ...
- j2ee分布式架构 dubbo + springmvc + mybatis + ehcache + redis 分布式架构
介绍 <modules> <!-- jeesz 工具jar --> <module>jeesz-utils</module> ...
- springMVC 实现redis分布式锁
1.先配置spring-data-redis 首先是依赖 <dependency> <groupId>org.springframework.data</groupId& ...
- Lua脚本在redis分布式锁场景的运用
目录 锁和分布式锁 锁是什么? 为什么需要锁? Java中的锁 分布式锁 redis 如何实现加锁 锁超时 retry redis 如何释放锁 不该释放的锁 通过Lua脚本实现锁释放 用redis做分 ...
- Redlock:Redis分布式锁最牛逼的实现
普通实现 说道Redis分布式锁大部分人都会想到:setnx+lua,或者知道set key value px milliseconds nx.后一种方式的核心实现命令如下: - 获取锁(unique ...
- 面试官问我,Redis分布式锁如何续期?懵了。
前言 上一篇[面试官问我,使用Dubbo有没有遇到一些坑?我笑了.]之后,又有一位粉丝和我说在面试过程中被虐了.鉴于这位粉丝是之前肥朝的粉丝,而且周一又要开启新一轮的面试,为了回馈他长期以来的支持,所 ...
随机推荐
- spring tx:advice 和 aop:config 配置事务
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- activiti参考5-任务TASK
一.概要 1,设计TASK的表主要是:ACT_RU_TASK,ACT_HI_TASKINST(见参考-activiti表): 2,任务主要有:人工任务(usertask),服务任务(serviceta ...
- 正则表达式 java版
众所周知,在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力.因此,学习及使用正则表达式,便成了解决这一矛 ...
- 给新centos系统虚拟机配置网络服务
记录下今天新建虚拟机的过程吧. 镜像:CentOS-6.3-x86_64-minimal; 虚拟机版本: vm 8.0 LET'S----------------------->GO 手动新建一 ...
- javamail模拟邮箱功能--邮件删除-中级实战篇【邮件标记方法】(javamail API电子邮件实例)
前言: JavaMail jar包下载地址:http://java.sun.com/products/javamail/downloads/index.html 本章可能是讲解javamail的最后一 ...
- 剑指offer-第三章高质量的代码(调整数组顺序使得奇数位于偶数的前面)
题目:输入一个整数数组,实现一个函数中调整该数组中数字的顺序,使得所有的奇数位于数组的前半部,所有偶数位于数组的后半部. 思路:用两个指针p1和p2,分别指向数组的头和尾部,p1只向后移,p2只向前移 ...
- requirejs之demo (转)
具体的理论就不讲了,可以参考 http://www.ruanyifeng.com/blog/2012/10/javascript_module.html http://www.ruanyifeng.c ...
- 基于jQuery的AJAX和JSON的实例
通过jQuery内置的AJAX功能,直接访问后台获得JSON格式的数据,然后通过jQuer把数据绑定到事先设计好的html模板上,直接在页面上显示. 我们先来看一下html模板: ...
- php 序列化(serialize)格式详解
1.前言 PHP (从 PHP 3.05 开始)为保存对象提供了一组序列化和反序列化的函数:serialize.unserialize.不过在 PHP 手册中对这两个函数的说明仅限于如何使用,而对序列 ...
- Free Candies
题意: 有4堆东西,每堆有n个每个有一个颜色,现在有一个篮子最多能装5个不同的颜色的东西,每次都从堆顶拿,当篮子出现两个相同颜色,可以获得这两个东西,求获得的最大数量 分析: 因为就4推,可以把各堆的 ...