当前位置:编程学习 > php >>

php-redis中文参考手册_zset_hash相关_zAdd_zRange_zDelete...

ZSET(stored set)


和 set 一样是字符串的集合,不同的是每个元素都会关联一个 double 类型的 score 。实现使用的是 skip list 和 hash table , skip list 的实现使用的是双线链表。 Score 的主要作用是排序,因此 sorted set 主要用作索引。

zAdd

Description
Adds the specified member with a given score to the sorted set stored at key.

增加一个或多个元素,如果该元素已经存在,更新它的socre值
虽然有序集合有序,但它也是集合,不能重复元素,添加重复元素只会
更新原有元素的score值

Parameters
key
score : double
value: string

Return value
Long 1 if the element is added. 0 otherwise.

Example
$redis->zAdd('key', 1, 'val1');
$redis->zAdd('key', 0, 'val0');
$redis->zAdd('key', 5, 'val5');
$redis->zRange('key', 0, -1); // array(val0, val1, val5)
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 ...

取得特定范围内的排序元素,0代表第一个元素,1代表第二个以此类推。-1代表最后一个,-2代表倒数第二个...

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) */
zDelete, zRem
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') */
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 interpretated as zero-based indices: 0 the first element, 1 the second ... -1 the last element, -2 the penultimate ...

返回key对应的有序集合中指定区间的所有元素。这些元素按照score从高到低的顺序进行排列。对于具有相同的score的元素而言,将会按照递减的字典顺序进行排列。该命令与ZRANGE类似,只是该命令中元素的排列顺序与前者不同。

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) */
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.

返回key对应的有序集合中score介于min和max之间的所有元素(包哈score等于min或者max的元素)。元素按照score从低到高的顺序排列。如果元素具有相同的score,那么会按照字典顺序排列。
可选的选项LIMIT可以用来获取一定范围内的匹配元素。如果偏移值较大,有序集合需要在获得将要返回的元素之前进行遍历,因此会增加O(N)的时间复杂度。可选的选项WITHSCORES可以使得在返回元素的同时返回元素的score,该选项自从Redis 2.0版本后可用。

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' => 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) */
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.

返回key对应的有序集合中介于min和max间的元素的个数。

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') */
zRemRangeByScore, zDeleteRangeByScore
Description
Deletes the elements of the sorted set stored at the specified key which have scores in the range [start,end].

移除key对应的有序集合中scroe位于min和max(包含端点)之间的所哟元素。从2.1.6版本后开始,区间端点min和max可以被排除在外,这和ZRANGEBYSCORE的语法一样。

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 */
zRemRangeByRank, zDeleteRangeByRank
Description
Deletes the elements of the sorted set stored at the specified key which have rank in the range [start,end].

移除key对应的有序集合中rank值介于start和stop之间的所有元素。start和stop均是从0开始的,并且两者均可以是负值。当索引值为负值时,表明偏移值从有序集合中score值最高的元素开始。例如:-1表示具有最高score的元素,而-2表示具有次高score的元素,以此类推。

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,

补充:Web开发 , php ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,