set数据类型相关操作
在Redis中,我们可以将Set类型看作为没有排序的字符集合,和List类型一样,我们也可以在该类型的数据值上执行添加、删除或判断某一元素是否存在等操作。需要说明的是,这些操作的时间复杂度为O(1),即常量时间内完成次操作。Set可包含的最大元素数量是4294967295。
和List类型不同的是,Set集合中不允许出现重复的元素,这一点和C++标准库中的set容器是完全相同的。换句话说,如果多次添加相同元素,Set中将仅保留该元素的一份拷贝。和List类型相比,Set类型在功能上还存在着一个非常重要的特性,即在服务器端完成多个Sets之间的聚合计算操作,如unions、intersections和differences。由于这些操作均在服务端完成,因此效率极高,而且也节省了大量的网络IO开销。
sAddDescription
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数据结构的定义。不重复,无顺序的集合。