Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。
Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。
添加记录:
127.0.0.1:6379> sadd disorder a b c
(integer) 3
删除记录:
127.0.0.1:6379> srem disorder a
(integer) 1
显示记录:
127.0.0.1:6379> smembers disorder
1) "c"
2) "b"
127.0.0.1:6379> sadd disorder2 b c d e a
(integer) 2
127.0.0.1:6379> smembers disorder2
1) "d"
2) "c"
3) "e"
4) "b"
5) "a"
对比不同:
127.0.0.1:6379> sdiff disorder disorder2
(empty list or set)
127.0.0.1:6379> sdiff disorder2 disorder
1) "d"
2) "e"
3) "a"
对比相同:
127.0.0.1:6379> sinter disorder disorder2
1) "c"
2) "b"
取多个集合中的所有值:
127.0.0.1:6379> sunion disorder disorder2
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
随机获取集合中的元素:
127.0.0.1:6379> srandmember disorder2 11
1) "d"
2) "c"
3) "b"
4) "a"
5) "e"
127.0.0.1:6379> srandmember disorder2 -10
1) "d"
2) "b"
3) "a"
4) "c"
5) "d"
6) "d"
7) "c"
8) "d"
9) "c"
10) "c"
pop元素:
127.0.0.1:6379> spop disorder2
"d"
127.0.0.1:6379> smembers disorder2
1) "c"
2) "a"
3) "b"
4) "e"
Redis 中 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。
添加记录:
127.0.0.1:6379> sadd disorder a b c
(integer) 3
删除记录:
127.0.0.1:6379> srem disorder a
(integer) 1
显示记录:
127.0.0.1:6379> smembers disorder
1) "c"
2) "b"
127.0.0.1:6379> sadd disorder2 b c d e a
(integer) 2
127.0.0.1:6379> smembers disorder2
1) "d"
2) "c"
3) "e"
4) "b"
5) "a"
对比不同:
127.0.0.1:6379> sdiff disorder disorder2
(empty list or set)
127.0.0.1:6379> sdiff disorder2 disorder
1) "d"
2) "e"
3) "a"
对比相同:
127.0.0.1:6379> sinter disorder disorder2
1) "c"
2) "b"
取多个集合中的所有值:
127.0.0.1:6379> sunion disorder disorder2
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
随机获取集合中的元素:
127.0.0.1:6379> srandmember disorder2 11
1) "d"
2) "c"
3) "b"
4) "a"
5) "e"
127.0.0.1:6379> srandmember disorder2 -10
1) "d"
2) "b"
3) "a"
4) "c"
5) "d"
6) "d"
7) "c"
8) "d"
9) "c"
10) "c"
pop元素:
127.0.0.1:6379> spop disorder2
"d"
127.0.0.1:6379> smembers disorder2
1) "c"
2) "a"
3) "b"
4) "e"