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, 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.
返回key對應的有序集合中member元素的索引值,元素按照score從低到高進行排列。rank值(或index)是從0開始的,這意味著具有最低score值的元素的rank值為0。使用ZREVRANK可以獲得從高到低排列的元素的rank(或index)。
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.
將key對應的有序集合中member元素的scroe加上increment。如果指定的member不存在,那麼將會新增該元素,並且其score的初始值為increment。如果key不存在,那麼將會建立一個新的有序列表,其中包含member這一唯一的元素。如果key對應的值不是有序列表,那麼將會發生錯誤。指定的score的值應該是能夠轉換為數字值的字串,並且接收雙精度浮點數。同時,你也可用提供一個負值,這樣將減少score的值。
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 AGGREGATE
option which specify how the results of the union are aggregated.
對keys對應的numkeys個有序集合計算合集,並將結果儲存在destination中。在傳遞輸入keys之前必須提供輸入keys的個數和其它可選引數。在預設情況下,元素的結果score是包含該元素的所有有序集合中score的和。如果使用WEIGHTS選項,你可以對每一個有序集合指定一個操作因子。這意味著每一個有序集合中的每個元素的score在傳遞給聚合函式之前均會被乘以該因子。當WEIGHTS沒有指定時,操作因子預設為1。
使用AGGREGATE選項,你可以指定交集中的結果如何被聚合。該選項預設值為SUM,在這種情況下,一個元素的所有score值均會被相加。當選項被設定為MIN或MAX時,結果集合中將會包含一個元素的最大或者最小的score值。如果destination已經存在,那麼它將會被重寫。
Parameters
keyOutput
arrayZSetKeys
arrayWeights
aggregateFunction Either "SUM", "MIN", or "MAX": defines the behaviour 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 AGGREGATE
option which specify how the results of the union are aggregated.
計算numkeys個由keys指定的有序集合的交集,並且將結果儲存在destination中。在該命令中,在你傳遞輸入keys之前,必須提供輸入keys的個數和其它可選的引數。
在預設情況下,一個元素的結果score是具有該元素的所有有序集合的score的和。關於WEIGHTS和AGGREGATE選項,可以參看ZUNIONSTORE命令。如果目標已經存在,那麼它將會被重寫。
Parameters
keyOutput
arrayZSetKeys
arrayWeights
aggregateFunction Either "SUM", "MIN", or "MAX": defines the behaviour 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.
新增一個VALUE到HASH中。如果VALUE已經存在於HASH中,則返回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.
新增一個VALUE到HASH STORE中,如果FIELD不存在。
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.
取得HASH中的VALUE,如何HASH不存在,或者KEY不存在返回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') */