Redis(六)—— Set 集合类型详解

Set(集合)

Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。

集合对象的编码可以是 intset 或者 hashtable。

Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。

集合中最大的成员数为 2^32 - 1 (4294967295, 每个集合可存储40多亿个成员)。

使用实例

设置集合的值:sadd

#================================================================
127.0.0.1:6379> sadd myset world
(integer) 1
127.0.0.1:6379> sadd myset hello
(integer) 1
127.0.0.1:6379> sadd myset hello #重复添加则失败
(integer) 0


查看集合的所有值:smembers,为1存在,为0不存在

#================================================================
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"

判断某个值是否在集合中:sismember,为1存在,为0不存在

#================================================================
127.0.0.1:6379> sismember myset hello
(integer) 1  #存在
127.0.0.1:6379> sismember myset hi
(integer) 0  #不存在

查看集合中值的个数:scard

#================================================================
127.0.0.1:6379> scard myset
(integer) 2

移除集合中的指定值:srem

#================================================================
127.0.0.1:6379> smembers myset
1) "hi"
2) "world"
3) "hello"
127.0.0.1:6379> srem myset hi
(integer) 1
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"

随机移除集合中的值:spop

#================================================================
127.0.0.1:6379> smembers myset  #查看集合中所有值
1) "wan"
2) "world"
3) "hello"
4) "li"
127.0.0.1:6379> spop myset 1  #随机移除myset集合中的一个值
1) "wan"
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"
3) "li"
127.0.0.1:6379> spop myset 2  #随机移除myset集合中的两个值
1) "world"
2) "li"
127.0.0.1:6379> smembers myset  #myset集合中值的剩余情况
1) "hello"

从集合中随机抽取元素,但不移除:srandmember

#================================================================
127.0.0.1:6379> sadd myset hello1
(integer) 1
127.0.0.1:6379> sadd myset hello2
(integer) 1
127.0.0.1:6379> sadd myset hello3
(integer) 1
127.0.0.1:6379> sadd myset hello4
(integer) 1
127.0.0.1:6379> sadd myset hello5
(integer) 1
127.0.0.1:6379> sadd myset hello6
(integer) 1
127.0.0.1:6379> sadd myset hello7
(integer) 1
127.0.0.1:6379> sadd myset hello8
(integer) 1
127.0.0.1:6379> sadd myset hello9
(integer) 1
127.0.0.1:6379> smembers myset
1) "hello7"
2) "hello5"
3) "hello3"
4) "hello4"
5) "hello6"
6) "hello1"
7) "hello8"
8) "hello2"
9) "hello9"
127.0.0.1:6379> srandmember myset  #从myset集合中随机抽取一个元素
"hello6"
127.0.0.1:6379> srandmember myset
"hello3"
127.0.0.1:6379> srandmember myset
"hello9"
127.0.0.1:6379> srandmember myset
"hello3"
127.0.0.1:6379> srandmember myset 2  #从myset集合中随机抽取两个元素
1) "hello7"
2) "hello8"
127.0.0.1:6379> srandmember myset 2
1) "hello5"
2) "hello2"
127.0.0.1:6379> srandmember myset 2
1) "hello6"
2) "hello9"
127.0.0.1:6379> srandmember myset 2
1) "hello3"
2) "hello8"

移动一个集合中的指定元素到另一个集合中:smove

#================================================================
127.0.0.1:6379> sadd myset1 hello1
(integer) 1
127.0.0.1:6379> sadd myset2 hello2
(integer) 1
127.0.0.1:6379> smove myset1 myset2 hello1 #移动myset1集合中的hello1元素到myset2集合中
(integer) 1
127.0.0.1:6379> smembers myset2
1) "hello1"
2) "hello2"
127.0.0.1:6379> smembers myset1
(empty array)

两个集合的差集(sdiff)和并集(sunion)和交集(sinter)

#================================================================
127.0.0.1:6379> sadd set1 a
(integer) 1
127.0.0.1:6379> sadd set1 b
(integer) 1
127.0.0.1:6379> sadd set1 c
(integer) 1
127.0.0.1:6379> sadd set2 c
(integer) 1
127.0.0.1:6379> sadd set2 d
(integer) 1
127.0.0.1:6379> sadd set2 e
(integer) 1
#==============================差集==================================
#集合set1中独有的元素
127.0.0.1:6379> sdiff set1 set2
1) "b"
2) "a"
#集合set2中独有的元素
127.0.0.1:6379> sdiff set2 set1
1) "e"
2) "d"
#==============================两个集合的并集==================================
127.0.0.1:6379> sunion set1 set2
1) "a"
2) "c"
3) "b"
4) "e"
5) "d"
127.0.0.1:6379> sunion set2 set1
1) "d"
2) "c"
3) "e"
4) "b"
5) "a"
#==============================两个集合的交集==================================
127.0.0.1:6379> sinter set1 set2
1) "c"

更多命令请参考:https://redis.io/commands

上一篇:MySQL基础总结(sqlyog中询问语句)


下一篇:大话Redis系列--实战案例总结(上)