配置:

$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 分布式类的更多相关文章

  1. redis分布式工具类 ----RedisShardedPoolUtil

    这个是redis分布式的工具类,看非分布式的看  这里 说一下redis的分布式,分布式,无疑,肯定不是一台redis服务器.假如说,我们有两台redis服务器,一个6379端口,一个6380端口.那 ...

  2. 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 ...

  3. Redis分布式锁的实现以及工具类

    一.应用场景: 本文应用的场景为在查询数据时,发现数据不存在此时就需要去查询数据库并且更新缓存,此时可能存在高并发的请求同时打在数据库上,而针对这种情况必须要给这些请求加锁,故而采用了分布式锁的方式. ...

  4. 一致性Hash算法在Redis分布式中的使用

    由于redis是单点,但是项目中不可避免的会使用多台Redis缓存服务器,那么怎么把缓存的Key均匀的映射到多台Redis服务器上,且随着缓存服务器的增加或减少时做到最小化的减少缓存Key的命中率呢? ...

  5. j2ee分布式架构 dubbo + springmvc + mybatis + ehcache + redis 分布式架构

    介绍 <modules>        <!-- jeesz 工具jar -->        <module>jeesz-utils</module> ...

  6. springMVC 实现redis分布式锁

    1.先配置spring-data-redis 首先是依赖 <dependency> <groupId>org.springframework.data</groupId& ...

  7. Lua脚本在redis分布式锁场景的运用

    目录 锁和分布式锁 锁是什么? 为什么需要锁? Java中的锁 分布式锁 redis 如何实现加锁 锁超时 retry redis 如何释放锁 不该释放的锁 通过Lua脚本实现锁释放 用redis做分 ...

  8. Redlock:Redis分布式锁最牛逼的实现

    普通实现 说道Redis分布式锁大部分人都会想到:setnx+lua,或者知道set key value px milliseconds nx.后一种方式的核心实现命令如下: - 获取锁(unique ...

  9. 面试官问我,Redis分布式锁如何续期?懵了。

    前言 上一篇[面试官问我,使用Dubbo有没有遇到一些坑?我笑了.]之后,又有一位粉丝和我说在面试过程中被虐了.鉴于这位粉丝是之前肥朝的粉丝,而且周一又要开启新一轮的面试,为了回馈他长期以来的支持,所 ...

随机推荐

  1. web.xml整合s2sh内容

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  2. 可以Ping通和DNS解析,但打不开网页的解决办法

    一. 网络故障表现为: 1.Ping地址正常,能ping通任何本来就可以ping通地址,如网关.域名. 2.能DNS解析域名. 3.无法打开网页,感觉是网页打开的一瞬间就显示无网络连接. 4.只需要连 ...

  3. source 命令

    作用: 当我修改了/etc/profile文件,我想让它立刻生效,而不用重新登录:这时就想到用source命令,如:source /etc/profile 介绍:source命令也称为“点命令”,也就 ...

  4. LINUX下的tty,console与串口分析

    1.LINUX下TTY.CONSOLE.串口之间是怎样的层次关系?具体的函数接口是怎样的?串口是如何被调用的? 2.printk函数是把信息发送到控制台上吧?如何让PRINTK把信息通过串口送出?或者 ...

  5. anything vs everything

    everything多用于肯定而anything多用于否定和疑问语气 anything 1) 任何事情/东西,可以用在肯定句/否定句/疑问句中.如:You can take anything you ...

  6. php常用正则

    平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 2.    "^\d+$" //非负整数(正整数 + 0) 3.    "^[0-9]*[1 ...

  7. JQuery中的事件以及动画

    .bind事件 <script src="script/jquery-1.7.1.min.js"></script> <script> $(fu ...

  8. Windows Live Writer安装与使用

    无耻的转贴一份WLW的安转与使用指南 =========================转贴分隔线===========================   [Windows Live Writer安 ...

  9. 剑指offer

    今天完成了剑指offer上的66道编程题,感觉自己还是很多代码实现能力和算法积累都还不够!还需要继续联系,坚持自己独立写代码实现. 最后将今天的两道题目奉上,都有异曲同工之妙: 矩阵中的路径: #in ...

  10. Bluebird-Collections

    Promise实例方法和Promise类核心静态方法用于处理promise或者混合型(mixed)promise和值的集合. 所有的集合实例方法都等效于Promise对象中的静态方法,例如:someP ...