ZSET(stored set)
Like set, it is a collection of strings. The difference is that each element is associated with a double type score. The implementation uses skip list and hash table, and the implementation of skip list uses a double-line linked list. The main role of Score is sorting, so the sorted set is mainly used as an index.
zAdd
Description
Adds the specified member with a given score to the sorted set stored at key.
Add one or more elements. If the element already exists, update its socre value
Although an ordered set is ordered, it is also a set and cannot duplicate elements. Adding duplicate elements will only
Update the score value of the original element
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 ...
Get the sorted elements within a specific range, 0 represents the first element, 1 represents the second element, and so on. -1 represents the last one, -2 represents the penultimate one...
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.
Remove the specified member from the sorted 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 interpreted as zero-based indices: 0 the first element, 1 the second ... -1 the last element, -2 the penultimate ...
Returns all elements in the specified range in the ordered set corresponding to key. These elements are arranged in order from high to low score. Elements with the same score will be sorted in descending lexicographic order. This command is similar to ZRANGE, except that the order of elements in this command is different from the former.
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.
Returns all elements in the sorted set corresponding to key whose score is between min and max (including elements whose score is equal to min or max). The elements are arranged in order from low to high score. If elements have the same score, they are sorted in lexicographic order.
The optional LIMIT option can be used to obtain matching elements within a certain range. If the offset value is large, the sorted collection needs to be traversed before obtaining the element to be returned, thus increasing the time complexity of O(N). The optional option WITHSCORES allows the element's score to be returned along with the element. This option is available since 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, array('withscores' => TRUE)); /* array('three' => 3) */
zSize, zCard
Description
Returns the cardinality of an ordered set.
返回存储在key对应的有序集合中的元素的个数。
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 */
zScore
Description
Returns the score of a given member in the specified sorted set.
返回key对应的有序集合中member的score值。如果member在有序集合中不存在,那么将会返回nil。
Parameters
key
member
Return value
Double
Example
$redis->zAdd('key', 2.5, 'val2');
$redis->zScore('key', 'val2'); /* 2.5 */
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.
Returns the index value of the member element in the ordered set corresponding to the key. The elements are arranged from low to high according to the score. The rank value (or index) is 0-based, which means that the element with the lowest score value has a rank value of 0. Use ZREVRANK to get the rank (or index) of elements arranged from high to low.
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 */
zIncrBy
Description
Increments the score of a member from a sorted set by a given amount.
Add increment to the scroe of the member element in the ordered set corresponding to the key. If the specified member does not exist, the element will be added and its initial score will be increment. If key does not exist, a new ordered list will be created containing the only element member. If the value corresponding to the key is not an ordered list, an error will occur. The value of the specified score should be a string that can be converted to a numeric value, and accepts double-precision floating point numbers. Also, you can provide a negative value, which will reduce the score value.
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 */
zUnion
Description
Creates an union of sorted sets given in second argument. The result of the union will be stored in the sorted set defined by the first argument. The third optionnel argument defines weights to apply to the sorted sets in input. In this case, the weights will be multiplied by the score of each element in the sorted set before applying the aggregation. The forth argument defines the AGGREGATEoption which specify how the results of the union are aggregated.
Calculate the collection of numkeys ordered sets corresponding to keys, and store the results in destination. The number of input keys and other optional parameters must be provided before passing the input keys. By default, the result score of an element is the sum of the scores in all sorted sets containing the element. If you use the WEIGHTS option, you can specify an operating factor for each sorted set. This means that the score of each element in each sorted set is multiplied by this factor before being passed to the aggregate function. When WEIGHTS is not specified, the operation factor defaults to 1.
Using the AGGREGATE option, you can specify how the results from the intersection are aggregated. The default value of this option is SUM, in which case all score values of an element are added. When the option is set to MIN or MAX, the result set will contain the maximum or minimum score value of an element. If the destination already exists, it will be overridden.
Parameters
keyOutput
arrayZSetKeys
arrayWeights
aggregateFunction Either "SUM", "MIN", or "MAX": defines the behavior to use on duplicate entries during the zUnion.
Return value
LONG The number of values in the new sorted set.
Example
$redis->delete('k1');
$redis->delete('k2');
$redis->delete('k3');
$redis->delete('ko1');
$redis->delete('ko2');
$redis->delete('ko3');
$redis->zAdd('k1', 0, 'val0');
$redis->zAdd('k1', 1, 'val1');
$redis->zAdd('k2', 2, 'val2');
$redis->zAdd('k2', 3, 'val3');
$redis->zUnion('ko1', array('k1', 'k2')); /* 4, 'ko1' => array('val0', 'val1', 'val2', 'val3') */
/* Weighted zUnion */
$redis->zUnion('ko2', array('k1', 'k2'), array(1, 1)); /* 4, 'ko1' => array('val0', 'val1', 'val2', 'val3') */
$redis->zUnion('ko3', array('k1', 'k2'), array(5, 1)); /* 4, 'ko1' => array('val0', 'val2', 'val3', 'val1') */
zInter
Description
Creates an intersection of sorted sets given in second argument. The result of the union will be stored in the sorted set defined by the first argument. The third optionnel argument defines weights to apply to the sorted sets in input. In this case, the weights will be multiplied by the score of each element in the sorted set before applying the aggregation. The forth argument defines the AGGREGATEoption which specify how the results of the union are aggregated.
Calculate the intersection of numkeys ordered sets specified by keys, and store the results in destination. In this command, before you pass the input keys, you must provide the number of input keys and other optional parameters.
By default, the resulting score of an element is the sum of the scores of all sorted sets with that element. For the WEIGHTS and AGGREGATE options, see the ZUNIONSTORE command. If the target already exists, it will be overwritten.
Parameters
keyOutput
arrayZSetKeys
arrayWeights
aggregateFunction Either "SUM", "MIN", or "MAX": defines the behavior to use on duplicate entries during the zInter.
Return value
LONG The number of values in the new sorted set.
Example
$redis->delete('k1');
$redis->delete('k2');
$redis->delete('k3');
$redis->delete('ko1');
$redis->delete('ko2');
$redis->delete('ko3');
$redis->delete('ko4');
$redis->zAdd('k1', 0, 'val0');
$redis->zAdd('k1', 1, 'val1');
$redis->zAdd('k1', 3, 'val3');
$redis->zAdd('k2', 2, 'val1');
$redis->zAdd('k2', 3, 'val3');
$redis->zInter('ko1', array('k1', 'k2')); /* 2, 'ko1' => array('val1', 'val3') */
$redis->zInter('ko2', array('k1', 'k2'), array(1, 1)); /* 2, 'ko2' => array('val1', 'val3') */
/* Weighted zInter */
$redis->zInter('ko3', array('k1', 'k2'), array(1, 5), 'min'); /* 2, 'ko3' => array('val1', 'val3') */
$redis->zInter('ko4', array('k1', 'k2'), array(1, 5), 'max'); /* 2, 'ko4' => array('val3', 'val1') */
hSet
Description
Adds a value to the hash stored at key. If this value is already in the hash, FALSE is returned.
Add a VALUE to HASH. If VALUE already exists in the HASH, return FALSE.
Parameters
key
hashKey
value
Return value
LONG 1 if value didn't exist and was added successfully, 0 if the value was already present and was replaced, FALSE if there was an error.
Example
$redis->delete('h')
$redis->hSet('h', 'key1', 'hello'); /* 1, 'key1' => 'hello' in the hash at "h" */
$redis->hGet('h', 'key1'); /* returns "hello" */
$redis->hSet('h', 'key1', 'plop'); /* 0, value was replaced. */
$redis->hGet('h', 'key1'); /* returns "plop" */
hSetNx
Description
Adds a value to the hash stored at key only if this field isn't already in the hash.
Add a VALUE to the HASH STORE if the FIELD does not exist.
Return value
BOOL TRUE if the field was set, FALSE if it was already present.
Example
$redis->delete('h')
$redis->hSetNx('h', 'key1', 'hello'); /* TRUE, 'key1' => 'hello' in the hash at "h" */
$redis->hSetNx('h', 'key1', 'world'); /* FALSE, 'key1' => 'hello' in the hash at "h". No change since the field wasn't replaced . */
hGet
Description
Gets a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, FALSE is returned.
Get the VALUE in HASH. If HASH does not exist or KEY does not exist, return FLASE.
Parameters
key
hashKey
Return value
STRING The value, if the command executed successfully BOOL FALSE in case of failure
hLen
Description
Returns the length of a hash, in number of items
取得HASH表的长度。
Parameters
key
Return value
LONG the number of items in a hash, FALSE if the key doesn't exist or isn't a hash.
Example
$redis->delete('h')
$redis->hSet('h', 'key1', 'hello');
$redis->hSet('h', 'key2', 'plop');
$redis->hLen('h'); /* returns 2 */
hDel
Description
Removes a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, FALSE is returned.
删除指定的元素。
Parameters
key
hashKey
Return value
BOOL TRUE in case of success, FALSE in case of failure
hKeys
Description
Returns the keys in a hash, as an array of strings.
取得HASH表中的KEYS,以数组形式返回。
Parameters
Key: key
Return value
An array of elements, the keys of the hash. This works like PHP's array_keys().
Example
$redis->delete('h');
$redis->hSet('h', 'a', 'x');
$redis->hSet('h', 'b', 'y');
$redis->hSet('h', 'c', 'z');
$redis->hSet('h', 'd', 't');
var_dump($redis->hKeys('h'));
Output:
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
}
The order is random and corresponds to redis' own internal representation of the set structure.
hVals
Description
Returns the values in a hash, as an array of strings.
取得HASH表中所有的VALUE,以数组形式返回。
Parameters
Key: key
Return value
An array of elements, the values of the hash. This works like PHP's array_values().
Example
$redis->delete('h');
$redis->hSet('h', 'a', 'x');
$redis->hSet('h', 'b', 'y');
$redis->hSet('h', 'c', 'z');
$redis->hSet('h', 'd', 't');
var_dump($redis->hVals('h'));
Output:
array(4) {
[0]=>
string(1) "x"
[1]=>
string(1) "y"
[2]=>
string(1) "z"
[3]=>
string(1) "t"
}
The order is random and corresponds to redis' own internal representation of the set structure.
hGetAll
Description
Returns the whole hash, as an array of strings indexed by strings.
取得整个HASH表的信息,返回一个以KEY为索引VALUE为内容的数组。
Parameters
Key: key
Return value
An array of elements, the contents of the hash.
Example
$redis->delete('h');
$redis->hSet('h', 'a', 'x');
$redis->hSet('h', 'b', 'y');
$redis->hSet('h', 'c', 'z');
$redis->hSet('h', 'd', 't');
var_dump($redis->hGetAll('h'));
Output:
array(4) {
["a"]=>
string(1) "x"
["b"]=>
string(1) "y"
["c"]=>
string(1) "z"
["d"]=>
string(1) "t"
}
The order is random and corresponds to redis' own internal representation of the set structure.
hExists
Description
Verify if the specified member exists in a key.
验证HASH表中是否存在指定的KEY-VALUE
Parameters
key
memberKey
Return value
BOOL: If the member exists in the hash table, return TRUE, otherwise return FALSE.
Examples
$redis->hSet('h', 'a', 'x');
$redis->hExists('h', 'a'); /* TRUE */
$redis->hExists('h', 'NonExistingKey'); /* FALSE */
hIncrBy
Description
Increments the value of a member from a hash by a given amount.
根据HASH表的KEY,为KEY对应的VALUE自增参数VALUE。
Parameters
key
member
value: (integer) value that will be added to the member's value
Return value
LONG the new value
Examples
$redis->delete('h');
$redis->hIncrBy('h', 'x', 2); /* returns 2: h[x] = 2 now. */
$redis->hIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */
hIncrByFloat
Description
Increments the value of a hash member by the provided float value
根据HASH表的KEY,为KEY对应的VALUE自增参数VALUE。浮点型
Parameters
key
member
value: (float) value that will be added to the member's value
Return value
FLOAT the new value
Examples
$redis->delete('h');
$redis->hIncrByFloat('h','x', 1.5); /* returns 1.5: h[x] = 1.5 now */
$redis->hIncrByFLoat('h', 'x', 1.5); /* returns 3.0: h[x] = 3.0 now */
$redis->hIncrByFloat('h', 'x', -3.0); /* returns 0.0: h[x] = 0.0 now */
hMset
Description
Fills in a whole hash. Non-string values are converted to string, using the standard (string) cast. NULL values are stored as empty strings.
批量填充HASH表。不是字符串类型的VALUE,自动转换成字符串类型。使用标准的值。NULL值将被储存为一个空的字符串。
Parameters
key
members: key → value array
Return value
BOOL
Examples
$redis->delete('user:1');
$redis->hMset('user:1', array('name' => 'Joe', 'salary' => 2000));
$redis->hIncrBy('user:1', 'salary', 100); // Joe earns 100 more now.
hMGet
Description
Retrieve the values associated to the specified fields in the hash.
批量取得HASH表中的VALUE。
Parameters
key
memberKeys Array
Return value
Array An array of elements, the values of the specified fields in the hash, with the hash keys as array keys.
Examples
$redis->delete('h');
$redis->hSet('h', 'field1', 'value1');
$redis->hSet('h', 'field2', 'value2');
$redis->hmGet('h', array('field1', 'field2')); /* returns array('field1' => 'value1', 'field2' => 'value2') */