### 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?