Redis Sets是字符串的无序集合,可以实现O(1)时间复杂度完成添加、删除等操作。并且,Redis中不允许出现重复的成员,多次添加相同的元素也只会存在唯一的一个副本。Redis Sets对象最多能够存储2^32-1个成员。
1.SADD/SMEMBERS
SADD命令向set对象中添加一个或多个成员,如果指定的成员已经存在,则不产生任何作用;如果待添加的set对象不存在,将创建一个新的set对象并添加相应成员。
(1)命令格式
1 |
SADD key member [member ...] |
(2)示例
1 2 3 4 5 6 7 8 9 |
127.0.0.1:6379> SADD myset "hello" (integer) 1 127.0.0.1:6379> SADD myset "world" (integer) 1 127.0.0.1:6379> SADD myset "world" (integer) 0 127.0.0.1:6379> SMEMBERS myset 1) "world" 2) "hello" |
可以发现,set对象中的成员是无序的。
(3)SMEMBERS
1 |
SMEMBERS key |
该命令返回set对象的所有成员,示例如上所示。
2.SISMEMBER
(1)命令格式
1 |
SISMEMBER key member |
该命令判断指定的member是否是key中的成员,1表示肯定;0表示否定。
(2)示例
1 2 3 4 5 6 |
127.0.0.1:6379> SADD myset 1 2 3 (integer) 3 127.0.0.1:6379> SISMEMBER myset 1 (integer) 1 127.0.0.1:6379> SISMEMBER myset 10 (integer) 0 |
3.SINTER/SINTERSTORE
(1)SINTER
1 |
SINTER key [key ...] |
求集合的交集,并返回。有如下示例:
1 2 3 4 5 6 7 8 |
127.0.0.1:6379> SADD set1 a b c (integer) 3 127.0.0.1:6379> SADD set2 c (integer) 1 127.0.0.1:6379> SADD set3 a c e (integer) 3 127.0.0.1:6379> SINTER set1 set2 set3 1) "c" |
(2)SINTERSTORE
1 |
SINTERSTORE destination key [key ...] |
求集合的交集,并将结果存储至destination中,示例:
1 2 3 4 5 6 7 8 9 10 |
127.0.0.1:6379> SADD set1 a b c (integer) 3 127.0.0.1:6379> SADD set2 c (integer) 1 127.0.0.1:6379> SADD set3 a c e (integer) 3 127.0.0.1:6379> SINTERSTORE set set1 set2 set3 (integer) 1 127.0.0.1:6379> SMEMBERS set 1) "c" |
4.SUNION/SUNIONSTORE
1 2 |
SUNION key [key ...] SUNIONSTORE destination key [key ...] |
这两个命令都是用来求结合的并集,不同的是,后者可以将结果存储到另一个指定的set对象中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
127.0.0.1:6379> SADD set1 a b c (integer) 3 127.0.0.1:6379> SADD set2 c (integer) 1 127.0.0.1:6379> SADD set3 a c e (integer) 3 127.0.0.1:6379> SUNION set1 set2 set3 1) "a" 2) "c" 3) "e" 4) "b" 127.0.0.1:6379> SUNIONSTORE set set1 set2 set3 (integer) 4 127.0.0.1:6379> SMEMBERS set 1) "a" 2) "c" 3) "e" 4) "b" |
5.SDIFF/SDIFFSTORE
1 2 |
SDIFF key [key ...] SDIFFSTORE destination key [key ...] |
返回第一个set与后面set的差,后者将结果存储到指定的set对象中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
127.0.0.1:6379> SADD set1 a b c d (integer) 4 127.0.0.1:6379> SADD set2 c (integer) 1 127.0.0.1:6379> SADD set3 a c e (integer) 3 127.0.0.1:6379> SDIFF set1 set2 set3 1) "b" 2) "d" 127.0.0.1:6379> SDIFFSTORE set set1 set2 set3 (integer) 2 127.0.0.1:6379> SMEMBERS set 1) "b" 2) "d" |
6.SPOP
该命令随机的从set集合中移除并返回一个或多个成员,命令格式如下:
1 |
SPOP key [count] |
有如下示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
127.0.0.1:6379> SADD myset "one" "two" "three" (integer) 3 127.0.0.1:6379> SPOP myset "one" 127.0.0.1:6379> SMEMBERS myset 1) "two" 2) "three" 127.0.0.1:6379> SADD myset "four" "five" (integer) 2 127.0.0.1:6379> SPOP myset 3 1) "four" 2) "five" 3) "two" 127.0.0.1:6379> SMEMBERS myset 1) "three" |
7.SCARD
1 |
SCARD key |
该命令返回key中元素的个数,有如下示例:
1 2 3 4 |
127.0.0.1:6379> SADD myset "hello" "world" (integer) 2 127.0.0.1:6379> SCARD myset (integer) 2 |
8.SMOVE/SREM
(1)SMOVE
1 |
SMOVE source destination member |
原子操作,将source集合中的member成员移动到destionation集合中,示例:
1 2 3 4 5 6 7 8 9 10 11 |
127.0.0.1:6379> SADD source "one" "two" (integer) 2 127.0.0.1:6379> SADD destionation "three" (integer) 1 127.0.0.1:6379> SMOVE source destionation "two" (integer) 1 127.0.0.1:6379> SMEMBERS source 1) "one" 127.0.0.1:6379> SMEMBERS destionation 1) "two" 2) "three" |
(2)SREM
1 |
SREM key member [member ...] |
从key集合中移除一个或多个成员member,示例:
1 2 3 4 5 6 7 8 9 |
127.0.0.1:6379> SADD myset one two three four five (integer) 5 127.0.0.1:6379> SREM myset one (integer) 1 127.0.0.1:6379> SREM myset two three (integer) 2 127.0.0.1:6379> SMEMBERS myset 1) "five" 2) "four" |
9.SRANDMEMBER
1 |
SRANDMEMBER key [count] |
从key中随机返回一个或多个成员,示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
127.0.0.1:6379> SADD myset one two three (integer) 3 127.0.0.1:6379> SRANDMEMBER myset "three" 127.0.0.1:6379> SRANDMEMBER myset 2 1) "two" 2) "three" 127.0.0.1:6379> SMEMBERS myset 1) "two" 2) "one" 3) "three" 127.0.0.1:6379> SRANDMEMBER myset -3 1) "two" 2) "two" 3) "three" |
当count为正数时,返回的成员不会重复;但是若count为负数时,返回的成员可能会重复。
参考:
https://redis.io/topics/data-types-intro
https://redis.io/commands/sscan