php对redis的zset(有序集合)操作
2016-03-03 00:00
676 查看
摘要: php对redis的zset(有序集合)操作
<?php require('redistest.php'); class zsetcache extends cache{ /** *向名称为key的zset中添加元素member,score用亍排序。如果该元素已经存在,则根据score更新该元素的顺序。 *ZADD命令的返回值是新加入到集合中的元素个数 * * @param $key string 集合名称 * @param $score int 分数 * @param $member string 值 * @return int 返回插入的个数 */ public function zAdd($key,$score,$member){ return $this->redis->zAdd($key,$score,$member); } /** * 返回名称为key的zset(元素已按score从小到大排序)中的index从start到end的所有元素 * ZRANGE命令会按照元素分数从小到大的顺序返回索引从start到stop之间的所有元素(包 * 含两端的元素)。ZRANGE命令与LRANGE命令十分相似,如索引都是从0开始,负数代表从后 * 向前查找(-1表示最后一个元素) * * @param $key string 集合名称 * @param $start int 开始 * @param $end int 结束 * @param bool $withscores 是否查找分数 * @return array 返回集合的的数组 */ public function zRange($key,$start,$end,$withscores=False){ if($withscores){ return $this->redis->zRange($key,$start,$end,true); }else{ return $this->redis->zRange($key,$start,$end); } } /** * 删除名称为key的zset中的元素member,返回删除的个数 *作用同zDelete * * @param $key string 集合 * @param $member string 元素 * @return int 删除的个数 */ public function zRem($key,$member){ return $this->redis->zRem($key,$member); } /** * 与zRange功能一样,只是zRevRange返回的元素是按score从大到小排序的 * * @param $key string 集合名称 * @param $start int 开始 * @param $end int 结束 * @param bool $withscores 是否查找分数 * @return array 返回集合的的数组 */ public function zRevRange($key,$start,$end,$withscores=False){ if($withscores){ return $this->redis->zRevRange($key,$start,$end,true); }else{ return $this->redis->zRevRange($key,$start,$end); } } /** * @param $key string 集合名称 * @param $start int 开始 * @param $end int 结束 * @param $limit * @param bool|False $withscores 是否查找分数 * @return mixed */ public function zRangeByScore($key,$start,$end,$limit,$withscores=False){ if($withscores){ return $this->redis->zRangeByScore($key,$start,$end,array('$withscores'=>TRUE,'limit'=>$limit)); }else{ return $this->redis->zRangeByScore($key,$start,$end,array('limit'=>$limit)); } } /** * 返回名称为key的zset中score >= star且score <= end的所有元素的个数 * * @param $key string 集合名称 * @param $start int 开始 * @param $end int 结束 * @return int */ public function zCount($key,$start,$end){ return $this->redis->zCount($key,$start,$end); } /** *删除名称为key的zset中score >= star且score <= end的所有元素,返回删除个数 * * @param $key string 集合名称 * @param $start int 开始 * @param $end int 结束 * @return int */ public function zRemRangeByScore($key,$start,$end){ return $this->redis->zRemRangeByScore($key,$start,$end); } /** * 返回名称为key的zset的所有元素的个数 * 作用同zCard * @param $key 集合名称 * @return Int 返回个数 */ public function zSize($key){ return $this->redis->zSize($key); } /** * 返回集合key中元素member的分数 * * @param $key string 集合 * @param $member string 元素 * @return float|bool 分数(如果不存在就返回false) */ public function zScore($key,$member){ return $this->redis->zScore($key,$member); } /** *返回名称为key的zset(元素已按score从小到大排序)中val元素的rank(即index,从0开始),若没有val元素,返回“0”。 * 就是返回比元素$member分数小的元素的个数 * * @param $key string 集合 * @param $member string 元素 * @return int */ public function zRank($key,$member){ return $this->redis->zRank($key,$member); } /** * 就是返回比元素$member分数大的元素的个数 * @param $key string 集合 * @param $member string 元素 * @return int */ public function zRevRank($key,$member){ return $this->redis->zRevRank($key,$member); } /** * 如果在名称为key的zset中已经存在元素member,则该元素的score增加increment;否则向集合中添加该元素,其score的值为increment * * @param $key 集合 * @param $increment 分数 * @param $member 元素 * @return int 增加的分数 */ public function zIncrBy($key,$increment,$member){ return $this->redis->zIncrBy($key,$increment,$member); } /** * 求有序集合的并集 * * @param $output要保存的集合 * @param $zsetkey 求并集的集合 * @return int 集合的数目 */ public function zUnion($output,$zsetkey){ return $this->redis->zUnion($output,$zsetkey); } /** * 求有序集合的交集 * * @param $output要保存的集合 * @param $zsetkey 求并集的集合 * @return int 集合的数目 */ public function zInter($output,$zsetkey){ return $this->redis->zInter($output,$zsetkey); } }
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- redis安装问题小结
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- 使用 Redis 和 Python 构建一个共享单车的应用程序
- Redis偶发连接失败案例实战记录
- Redis中实现查找某个值的范围
- redis的hGetAll函数的性能问题(记Redis那坑人的HGETALL)
- Redis和Memcached的区别详解
- 分割超大Redis数据库例子
- Redis总结笔记(一):安装和常用命令
- Redis sort 排序命令详解
- 用Redis实现微博关注关系
- PHP+Apache在Windows 9x下的安装和配置