ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 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 */ ~~~