存储Set 和List类型不同的是Set集合中不允许出现重复的元素。 Set可包含 的最大元素数量是4294967295。 存储set常用的命令 添加/删除元素、集合中的差集运算、集合中的并集运算、获得集合中的元素、集合中的交集运算、扩展命令
Microsoft Windows [ 版本 10.0 .16299 .125 ]
( c) 2017 Microsoft Corporation。保留所有权利。
C:\Users\YUANRUI> redis- cli
127.0 .0 .1 :6379 > sadd myset a b c
( integer ) 3
127.0 .0 .1 :6379 > sadd myset a
( integer ) 0
127.0 .0 .1 :6379 > sadd myset 1 2 3
( integer ) 3
127.0 .0 .1 :6379 > srem myset 1 2
( integer ) 2
127.0 .0 .1 :6379 > smembers myset
1 ) "a"
2 ) "b"
3 ) "3"
4 ) "c"
127.0 .0 .1 :6379 > sismember myset a
( integer ) 1
127.0 .0 .1 :6379 > sismember myset x
( integer ) 0
127.0 .0 .1 :6379 > sadd mya1 a b c
( integer ) 3
127.0 .0 .1 :6379 > sadd myb1 a c 1 2
( integer ) 4
127.0 .0 .1 :6379 > sdiff myal myb1
( empty list or set )
127.0 .0 .1 :6379 > sdiff mya1 myb1
1 ) "b"
127.0 .0 .1 :6379 > sadd mya2 a b c
( integer ) 3
127.0 .0 .1 :6379 > sadd myb2 a c 2 1
( integer ) 4
127.0 .0 .1 :6379 > sinter mya2 myb2
1 ) "c"
2 ) "a"
127.0 .0 .1 :6379 > sadd mya3 a b c
( integer ) 3
127.0 .0 .1 :6379 > sadd myb3 a c 1 2
( integer ) 4
127.0 .0 .1 :6379 > sunion mya3 myb3
1 ) "b"
2 ) "1"
3 ) "a"
4 ) "2"
5 ) "c"
127.0 .0 .1 :6379 > smembers myset
1 ) "a"
2 ) "b"
3 ) "3"
4 ) "c"
127.0 .0 .1 :6379 > scard myset
( integer ) 4
127.0 .0 .1 :6379 > srandmember myset
"3"
127.0 .0 .1 :6379 > srandmember myset
"b"
127.0 .0 .1 :6379 > sdiffstore my1 mya1 myb1
( integer ) 1
127.0 .0 .1 :6379 > smembers my1
1 ) "b"
127.0 .0 .1 :6379 > sinter mya2 myb2
1 ) "c"
2 ) "a"
127.0 .0 .1 :6379 > sinterstore my2 mya2 myb2
( integer ) 2
127.0 .0 .1 :6379 > smembers my2
1 ) "a"
2 ) "c"
127.0 .0 .1 :6379 > sunion mya3 myb3
1 ) "b"
2 ) "1"
3 ) "a"
4 ) "2"
5 ) "c"
127.0 .0 .1 :6379 > sunionstore my3 mya3 myb3
( integer ) 5
127.0 .0 .1 :6379 > smembers my3
1 ) "b"
2 ) "1"
3 ) "a"
4 ) "2"
5 ) "c"
127.0 .0 .1 :6379 >
存储Set使用场景: Set常用与跟踪一些唯一性的数据 比如访问某一博客的唯一IP; 同一客户购买两个商品,在不同商品set中存入客户id,利用交集可以知道客户购买的所有东西。 用于维护数据对象之间的关联关系