### Sorted Set
有序集合(Sorted Set)是Redis一个很重要的数据结构,它用来保存需要排序的数据。例如排行榜,一个班的语文成绩,一个公司的员工工资,一个论坛的帖子等。
它有三个元素:key、member和score。
有序集合中,每个元素都带有score(权重),以此来对元素进行排序。
以语文成绩为例,key是考试名称(期中考试、期末考试等),member是学生名字,score是成绩。
有序集合有两大基本用途:排序和聚合,以下用几个例子分别说明。
[TOC]
#### zCard, zSize
Description: Returns the cardinality of an ordered set.
Parameters
key
Return value
Long, the set's cardinality
Example
~~~
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zSize('key'); /* 3 */
~~~
#### zCount
Description: Returns the number of elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before start or end excludes it from the range. +inf and -inf are also valid limits.
Parameters
key
start: string
end: string
Return value
LONG the size of a corresponding zRangeByScore.
Example
~~~
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zCount('key', 0, 3); /* 2, corresponding to array('val0', 'val2') */
~~~
> 备注; 如果以时间戳作为score的话,可以获取到区间记录。
#### zIncrBy
Description: Increments the score of a member from a sorted set by a given amount.
Parameters
key
value: (double) value that will be added to the member's score
member
Return value
DOUBLE the new value
Examples
~~~
$redis->delete('key');
$redis->zIncrBy('key', 2.5, 'member1'); /* key or member1 didn't exist, so member1's score is to 0 before the increment */
/* and now has the value 2.5 */
$redis->zIncrBy('key', 1, 'member1'); /* 3.5 */
~~~
#### zRange
Description: Returns a range of elements from the ordered set stored at the specified key, with values in the range [start, end].
Start and stop are interpreted as zero-based indices:
0 the first element, 1 the second ...
-1 the last element, -2 the penultimate ...
Parameters
key start: long
end: long
withscores: bool = false
Return value
Array containing the values in specified range.
Example
~~~
$redis->zAdd('key1', 0, 'val0');
$redis->zAdd('key1', 2, 'val2');
$redis->zAdd('key1', 10, 'val10');
$redis->zRange('key1', 0, -1); /* array('val0', 'val2', 'val10') */
// with scores
$redis->zRange('key1', 0, -1, true); /* array('val0' => 0, 'val2' => 2, 'val10' => 10) */
~~~
#### zRangeByScore, zRevRangeByScore
Description: Returns the elements of the sorted set stored at the specified key which have scores in the range [start,end]. Adding a parenthesis before start or end excludes it from the range. +inf and -inf are also valid limits. zRevRangeByScore returns the same items in reverse order, when the start and end parameters are swapped.
Parameters
key
start: string
end: string
options: array
Two options are available: withscores => TRUE, and limit => array($offset, $count)
Return value
Array containing the values in specified range.
Example
~~~
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zRangeByScore('key', 0, 3); /* array('val0', 'val2') */
$redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE); /* array('val0' => 0, 'val2' => 2) */
$redis->zRangeByScore('key', 0, 3, array('limit' => array(1, 1)); /* array('val2') */
$redis->zRangeByScore('key', 0, 3, array('withscores' => TRUE, 'limit' => array(1, 1)); /* array('val2' => 2) */
~~~
#### zRank, zRevRank
Description: Returns the rank of a given member in the specified sorted set, starting at 0 for the item with the smallest score. zRevRank starts at 0 for the item with the largest score.
Parameters
key
member
Return value
Long, the item's score.
Example
~~~
$redis->delete('z');
$redis->zAdd('key', 1, 'one');
$redis->zAdd('key', 2, 'two');
$redis->zRank('key', 'one'); /* 0 */
$redis->zRank('key', 'two'); /* 1 */
$redis->zRevRank('key', 'one'); /* 1 */
$redis->zRevRank('key', 'two'); /* 0 */
~~~
#### zRem, zDelete
Description: Deletes a specified member from the ordered set.
Parameters
key
member
Return value
LONG 1 on success, 0 on failure.
Example
~~~
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zDelete('key', 'val2');
$redis->zRange('key', 0, -1); /* array('val0', 'val10') */
~~~
#### zRemRangeByRank, zDeleteRangeByRank
Description: Deletes the elements of the sorted set stored at the specified key which have rank in the range [start,end].
Parameters
key
start: LONG
end: LONG
Return value
LONG The number of values deleted from the sorted set
Example
~~~
$redis->zAdd('key', 1, 'one');
$redis->zAdd('key', 2, 'two');
$redis->zAdd('key', 3, 'three');
$redis->zRemRangeByRank('key', 0, 1); /* 2 */
$redis->zRange('key', 0, -1, array('withscores' => TRUE)); /* array('three' => 3) */
~~~
#### zRemRangeByScore, zDeleteRangeByScore
Description: Deletes the elements of the sorted set stored at the specified key which have scores in the range [start,end].
Parameters
key
start: double or "+inf" or "-inf" string
end: double or "+inf" or "-inf" string
Return value
LONG The number of values deleted from the sorted set
Example
~~~
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zRemRangeByScore('key', 0, 3); /* 2 */
~~~
#### zRevRange
Description: Returns the elements of the sorted set stored at the specified key in the range [start, end] in reverse order. start and stop are interpreted as zero-based indices:
0 the first element, 1 the second ...
-1 the last element, -2 the penultimate ...
Parameters
key
start: long
end: long
withscores: bool = false
Return value
Array containing the values in specified range.
Example
~~~
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 2, 'val2');
$redis->zAdd('key', 10, 'val10');
$redis->zRevRange('key', 0, -1); /* array('val10', 'val2', 'val0') */
// with scores
$redis->zRevRange('key', 0, -1, true); /* array('val10' => 10, 'val2' => 2, 'val0' => 0) */
~~~
#### zScore
Description: Returns the score of a given member in the specified sorted set.
Parameters
key
member
Return value
Double
Example
~~~
$redis->zAdd('key', 2.5, 'val2');
$redis->zScore('key', 'val2'); /* 2.5 */
~~~
- 目录
- 安装扩展
- 在 Windows 上安装 PHP 扩展
- 测试Redis扩展函数
- 教程
- 简介
- Redis 安装
- Redis 配置
- 运行
- 测试
- 书籍
- 《Redis开发与运维》
- 《Redis入门指南》
- 《Redis实战》
- 《当 Redis 遇上 ThinkPHP5》
- 参考站点
- 下载
- 命令参考
- 管理工具
- 视频
- 云数据库 Redis 版使用教程
- Redis 深入之道
- Redis高可用教程
- Redis入门
- NoSQL概述
- Redis概述
- Redis安装
- Jedis入门
- PHP命令
- PHP中利用Redis管道加快执行
- Hash操作
- Set操作
- Gearman
- MySQL - Redis配合使用方案
- 应用场景
- 缓存应用
- Redis实现简单的条件查询功能
- 获取网站中点击量最高的前n篇文章
- 显示最新的项目列表
- 排行榜相关
- 设计技巧
- SortedSets
- List列表
- 消息队列
- 最新文章
- Set集合
- 共同好友
- 独立 IP
- Linux教程
- 常用命令
- 哈希命令
- 字符串
- 集合
- 有序集合
- Redis 有序集合命令
- 有序集合命令(中)
- 发布订阅
- 用例
- 列表
- Lindex
- Ltrim
- Rpush
- Lset
- Llen
- Lpush
- 信息
- info memory
- 安装
- 数据类型
- Redis管道(pipeline)
- Memory Command
- 阿里云Redis
- 架构
- 4.0版本
- Redis 4.0 新功能介绍
- Redis Desktop Manager
- 创建hash列表数据
- Lua: 给 Redis 用户的入门指导
- Lua入门
- 乐观锁介绍
- 悲观锁介绍
- 脏数据
- Redis核心概念
- Redis事务
- Lua
- 在Redis中使用lua脚本
- php-redis
- mysql缓存服务器
- redis setnx 实现分布式锁和单机锁
- 为什么分布式一定要有Redis?