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有没有遇到一些坑?我笑了.]之后,又有一位粉丝和我说在面试过程中被虐了.鉴于这位粉丝是之前肥朝的粉丝,而且周一又要开启新一轮的面试,为了回馈他长期以来的支持,所 ...
随机推荐
- [反汇编练习] 160个CrackMe之024
[反汇编练习] 160个CrackMe之024. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- 最全的Android源码目录结构详解(转)
Android 2.1|-- Makefile|-- bionic (bionic C库)|-- bootable (启动 ...
- OK335xS ethtool 移植
/******************************************************************* * OK335xS ethtool 移植 * 声明: * 由于 ...
- C与C++的区别无随时更新
C没有calss类,只有结构体struct class A; 在C中这样写就是错误的,C没有关键字class C的字符指针不会自动开辟内存空间,必须对这个指针指向的地址手动开辟空间后才可以写入数据. ...
- 理解 Bias 与 Variance 之间的权衡
有监督学习中,预测误差的来源主要有两部分,分别为 bias 与 variance,模型的性能取决于 bias 与 variance 的 tradeoff ,理解 bias 与 variance 有助 ...
- Android 程序员必须掌握的三种自动化测试方法
在日常的开发中,尤其是app开发,因为不像web端那样 出错以后可以热更新,所以app开发 一般对软件质量有更高的要求(你可以想一下 一个发出去的版本如果有重大缺陷 需要强制更新新客户端是多么蛋疼的事 ...
- 【,net】发布网站问题
今天解决了一个发布后网站访问不了的问题: 问题: 发布网站:http://172.168.1.102:2222/nologin.html,但是页面一直处于刷新状态,进行不了系统: 解决方法: 问开发他 ...
- [转]linux系统磁盘分区之parted
转自:http://blog.csdn.net/h249059945/article/details/12668793 对于linux的分区通常可以使用fdisk命令工具和parted工具对于分区表通 ...
- LR之性能分析基础
1.判断测试结果有效性 2.分析要点提示 3.Analysis主要提供的6大类分析图 4.分析流程
- 网站压力测试工具-Webbench源码笔记
Ubuntu 下安装使用 1.安装依赖包CTAGS sudo apt-get install ctage 2.下载及安装 Webbench http://home.tiscali.cz/~cz2105 ...