php-redis中文幫助手冊_set相關_sAdd_sRem_sRemove_sMove_s…

set數據類型相關操作

      在Redis中,我們可以將Set類型看作為沒有排序的字符集合,和List類型一樣,我們也可以在該類型的數據值上執行添加、刪除或判斷某一元素是否存在等操作。需要說明的是,這些操作的時間復雜度為O(1),即常量時間內完成次操作。Set可包含的最大元素數量是4294967295。

      和List類型不同的是,Set集合中不允許出現重復的元素,這一點和C++標準庫中的set容器是完全相同的。換句話說,如果多次添加相同元素,Set中將僅保留該元素的一份拷貝。和List類型相比,Set類型在功能上還存在著一個非常重要的特性,即在伺服器端完成多個Sets之間的聚合計算操作,如unions、intersections和differences。由於這些操作均在服務端完成,因此效率極高,而且也節省瞭大量的網絡IO開銷。

sAdd
Description
Adds a value to the set value stored at key. If this value is already in the set, FALSE is returned.

添加一個VALUE到SET容器中,如果這個VALUE已經存在於SET中,那麼返回FLASE。

Parameters
key value

Return value
BOOL TRUE if value didn't exist and was added successfully, FALSE if the value is already present.

如果VALUE不存在於SET中,那麼ADDED成功,返回TRUE,負責返回FALSE。

Example
$redis->sAdd('key1' , 'member1'); /* TRUE, 'key1' => {'member1'} */
$redis->sAdd('key1' , 'member2'); /* TRUE, 'key1' => {'member1', 'member2'}*/
$redis->sAdd('key1' , 'member2'); /* FALSE, 'key1' => {'member1', 'member2'}*/
sRem, sRemove
Description
Removes the specified member from the set value stored at key.

移除指定的VALUE從SET容器中

Parameters
key member

Return value
BOOL TRUE if the member was present in the set, FALSE if it didn't.

Example
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member1', 'member2', 'member3'}*/
$redis->sRem('key1', 'member2'); /* 'key1' => {'member1', 'member3'} */
sMove
Description
Moves the specified member from the set at srcKey to the set at dstKey.

移動一個指定的MEMBER從源SET到指定的另一個SET中。

Parameters
srcKey dstKey member

Return value
BOOL If the operation is successful, return TRUE. If the srcKey and/or dstKey didn't exist, and/or the member didn't exist in srcKey,FALSE is returned.

如果操作成功返回TRUE,如果源SET或者目標SET不存在,或者MEMBER不存在於源SET,那麼返回FLASE。

Example
$redis->sAdd('key1' , 'member11');
$redis->sAdd('key1' , 'member12');
$redis->sAdd('key1' , 'member13'); /* 'key1' => {'member11', 'member12', 'member13'}*/
$redis->sAdd('key2' , 'member21');
$redis->sAdd('key2' , 'member22'); /* 'key2' => {'member21', 'member22'}*/
$redis->sMove('key1', 'key2', 'member13'); /* 'key1' =>  {'member11', 'member12'} */
                    /* 'key2' =>  {'member21', 'member22', 'member13'} */
sIsMember, sContains
Description
Checks if value is a member of the set stored at the key key.

檢查VALUE是否是SET容器中的成員。

Parameters
key value

Return value
BOOL TRUE if value is a member of the set at key key, FALSE otherwise.

Example
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member1', 'member2', 'member3'}*/

$redis->sIsMember('key1', 'member1'); /* TRUE */
$redis->sIsMember('key1', 'memberX'); /* FALSE */
sCard, sSize
Description
Returns the cardinality of the set identified by key.

返回SET容器的成員數

Parameters
key

Return value
LONG the cardinality of the set identified by key, 0 if the set doesn't exist.

Example
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member1', 'member2', 'member3'}*/
$redis->sCard('key1'); /* 3 */
$redis->sCard('keyX'); /* 0 */
sPop
Description
Removes and returns a random element from the set value at Key.

隨機返回一個元素,並且在SET容器中移除該元素。

Parameters
key

Return value
String "popped" value
Bool FALSE if set identified by key is empty or doesn't exist.

Example
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member3', 'member1', 'member2'}*/
$redis->sPop('key1'); /* 'member1', 'key1' => {'member3', 'member2'} */
$redis->sPop('key1'); /* 'member3', 'key1' => {'member2'} */
sRandMember
Description
Returns a random element from the set value at Key, without removing it.

取得指定SET容器中的一個隨機元素,但不會在SET容器中移除它。

Parameters
key

Return value
String value from the set
Bool FALSE if set identified by key is empty or doesn't exist.

Example
$redis->sAdd('key1' , 'member1');
$redis->sAdd('key1' , 'member2');
$redis->sAdd('key1' , 'member3'); /* 'key1' => {'member3', 'member1', 'member2'}*/
$redis->sRandMember('key1'); /* 'member1', 'key1' => {'member3', 'member1', 'member2'} */
$redis->sRandMember('key1'); /* 'member3', 'key1' => {'member3', 'member1', 'member2'} */
sInter
Description
Returns the members of a set resulting from the intersection of all the sets held at the specified keys. If just a single key is specified, then this command produces the members of this set. If one of the keys is missing, FALSE is returned.

返回指定SETS集合的交集結果。如果隻是指定瞭一個SET集合,那麼返回該SET集合。如果在參數中有參數錯誤,那麼則返回FLASE。

Parameters
key1, key2, keyN: keys identifying the different sets on which we will apply the intersection.

參數列表,代表不同的SET集合。

Return value
Array, contain the result of the intersection between those keys. If the intersection beteen the different sets is empty, the return value will be empty array.

返回數組,數組中的結果為所有SET集合的交集。如果所涉及到的SET集合沒有交集結果,那麼將返回一個空數組。

Examples
$redis->sAdd('key1', 'val1');
$redis->sAdd('key1', 'val2');
$redis->sAdd('key1', 'val3');
$redis->sAdd('key1', 'val4');

$redis->sAdd('key2', 'val3');
$redis->sAdd('key2', 'val4');

$redis->sAdd('key3', 'val3');
$redis->sAdd('key3', 'val4');

var_dump($redis->sInter('key1', 'key2', 'key3'));
Output:

array(2) {
  [0]=>
  string(4) "val4"
  [1]=>
  string(4) "val3"
}
sInterStore
Description
Performs a sInter command and stores the result in a new set.

執行一個交集操作,並把結果存儲到一個新的SET容器中。

Parameters
Key: dstkey, the key to store the diff into.

key 儲存結果的SET容器KEY

Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.

求交集的KEYS

Return value
INTEGER: The cardinality of the resulting set, or FALSE in case of a missing key.

Example
$redis->sAdd('key1', 'val1');
$redis->sAdd('key1', 'val2');
$redis->sAdd('key1', 'val3');
$redis->sAdd('key1', 'val4');

$redis->sAdd('key2', 'val3');
$redis->sAdd('key2', 'val4');

$redis->sAdd('key3', 'val3');
$redis->sAdd('key3', 'val4');

var_dump($redis->sInterStore('output', 'key1', 'key2', 'key3'));
var_dump($redis->sMembers('output'));
Output:

int(2)

array(2) {
  [0]=>
  string(4) "val4"
  [1]=>
  string(4) "val3"
}
sUnion
Description
Performs the union between N sets and returns it.

執行一個並集操作在N個SET容器之間,並返回結果。

Parameters
Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.

Return value
Array of strings: The union of all these sets.

返回一個數組

Example
$redis->delete('s0', 's1', 's2');

$redis->sAdd('s0', '1');
$redis->sAdd('s0', '2');
$redis->sAdd('s1', '3');
$redis->sAdd('s1', '1');
$redis->sAdd('s2', '3');
$redis->sAdd('s2', '4');

var_dump($redis->sUnion('s0', 's1', 's2'));
Return value: all elements that are either in s0 or in s1 or in s2.

array(4) {
  [0]=>
  string(1) "3"
  [1]=>
  string(1) "4"
  [2]=>
  string(1) "1"
  [3]=>
  string(1) "2"
}
sUnionStore
Description
Performs the same action as sUnion, but stores the result in the first key

執行一個並集操作就和sUnion()一樣,但是結果儲存在第一個參數中。

Parameters
Key: dstkey, the key to store the diff into.

存儲結果的SET集合KEY

Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.

求並集的KEYS

Return value
INTEGER: The cardinality of the resulting set, or FALSE in case of a missing key.

返回整數值:並集結果的個數。

Example
$redis->delete('s0', 's1', 's2');

$redis->sAdd('s0', '1');
$redis->sAdd('s0', '2');
$redis->sAdd('s1', '3');
$redis->sAdd('s1', '1');
$redis->sAdd('s2', '3');
$redis->sAdd('s2', '4');

var_dump($redis->sUnionStore('dst', 's0', 's1', 's2'));
var_dump($redis->sMembers('dst'));
Return value: the number of elements that are either in s0 or in s1 or in s2.

int(4)
array(4) {
  [0]=>
  string(1) "3"
  [1]=>
  string(1) "4"
  [2]=>
  string(1) "1"
  [3]=>
  string(1) "2"
}
sDiff
Description
Performs the difference between N sets and returns it.

執行差集操作在N個不同的SET容器之間,並返回結果。這個操作取得結果是第一個SET相對於其他參與計算的SET集合的差集。(Result = SET0 – (SET1 UNION SET2 UNION ….SET N))

Parameters
Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis.

Return value
Array of strings: The difference of the first set will all the others.

返回數組,返回的是第一個SET集合相對於其他集合的差集(first set – (N sets))

返回數組:第一個SET集合的補

Example
$redis->delete('s0', 's1', 's2');

$redis->sAdd('s0', '1');
$redis->sAdd('s0', '2');
$redis->sAdd('s0', '3');
$redis->sAdd('s0', '4');

$redis->sAdd('s1', '1');
$redis->sAdd('s2', '3');

var_dump($redis->sDiff('s0', 's1', 's2'));
Return value: all elements of s0 that are neither in s1 nor in s2.

array(2) {
  [0]=>
  string(1) "4"
  [1]=>
  string(1) "2"
}
sDiffStore
Description
Performs the same action as sDiff, but stores the result in the first key

與sDiff函數功能一直,隻是結果為一個新的SET集合,存儲到dstkey。

Parameters
Key: dstkey, the key to store the diff into.

Key:存儲結果的SET集合KEY

Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis

參與操作的SET集合

Return value
INTEGER: The cardinality of the resulting set, or FALSE in case of a missing key.

返回整數:為結果集的個數。

Example
$redis->delete('s0', 's1', 's2');

$redis->sAdd('s0', '1');
$redis->sAdd('s0', '2');
$redis->sAdd('s0', '3');
$redis->sAdd('s0', '4');

$redis->sAdd('s1', '1');
$redis->sAdd('s2', '3');

var_dump($redis->sDiffStore('dst', 's0', 's1', 's2'));
var_dump($redis->sMembers('dst'));
Return value: the number of elements of s0 that are neither in s1 nor in s2.

int(2)
array(2) {
  [0]=>
  string(1) "4"
  [1]=>
  string(1) "2"
}
sMembers, sGetMembers
Description
Returns the contents of a set.

返回SET集合中的所有元素。

Parameters
Key: key

Return value
An array of elements, the contents of the set.

Example
$redis->delete('s');
$redis->sAdd('s', 'a');
$redis->sAdd('s', 'b');
$redis->sAdd('s', 'a');
$redis->sAdd('s', 'c');
var_dump($redis->sMembers('s'));
Output:

array(3) {
  [0]=>
  string(1) "c"
  [1]=>
  string(1) "a"
  [2]=>
  string(1) "b"
}
The order is random and corresponds to redis' own internal representation of the set structure.

結果集的順序是隨機的,這也符合Redis本身對SET數據結構的定義。不重復,無順序的集合。

 

作者:四雲麒麟

You May Also Like