7.sinter setkey1 setkey2 setkey3...:返回setkey1和setkey2的交集
127.0.0.1:6379> smembers setkey1
1) "world"
2) "hello"
3) "2000"
127.0.0.1:6379> smembers setkey2
1) "3000"
2) "world"
3) "2000"
127.0.0.1:6379> smembers setkey3
1) "3000"
127.0.0.1:6379> sinter setkey1 setkey2
1) "world"
2) "2000"
127.0.0.1:6379> sinter setkey1 setkey2 setkey3
(empty list or set)
127.0.0.1:6379> sadd setkey3 2000
(integer) 1
127.0.0.1:6379> sinter setkey1 setkey2 setkey3
1) "2000"
8.sinterstore setkey3 setkey1 setkey2..:将取到的交集存储到setkey3中。同sdiffstore
127.0.0.1:6379> sinterstore setkey5 setkey1 setkey2
(integer) 2
127.0.0.1:6379> smembers setkey5
1) "world"
2) "2000"
9.sunion setkey1 setkey2 setkey3....:取给定所有key的并集
127.0.0.1:6379> smembers setkey1
1) "world"
2) "hello"
3) "2000"
127.0.0.1:6379> smembers setkey2
1) "3000"
2) "world"
3) "2000"
127.0.0.1:6379> sunion setkey1 setkey2
1) "hello"
2) "2000"
3) "world"
4) "3000"
10.sunionstore setkey3 setkey1 setkey2...:将并集存到setkey3中
127.0.0.1:6379> sunionstore setkey6 setkey1 setkey2
(integer) 4
127.0.0.1:6379> smembers setkey6
1) "hello"
2) "2000"
3) "world"
4) "3000"
11.smove setkey1 setkey2 value:从setkey1中移除一个元素value到setkey2中。
127.0.0.1:6379> smembers setkey1
1) "world"
2) "hello"
3) "2000"
127.0.0.1:6379> smembers setkey2
1) "3000"
2) "world"
3) "2000"
127.0.0.1:6379> smove setkey1 setkey2 hello
(integer) 1
127.0.0.1:6379> smembers setkey2
1) "3000"
2) "world"
3) "hello"
4) "2000"
127.0.0.1:6379> smembers setkey1
1) "world"
2) "2000"
127.0.0.1:6379> smove setkey1 setkey2 world
(integer) 1
127.0.0.1:6379> smembers setkey1
1) "2000"
127.0.0.1:6379> smembers setkey2
1) "3000"
2) "world"
3) "hello"
4) "2000"
大家仔细看一下上面的例子。就会发现,如果被移动的value在setkey2中已经存在,那么就相当于srem setkey1 value,仅仅是将value元素从setkey1中删除。
12.scard setkey:查看setkey集合中元素的个数
127.0.0.1:6379> smembers setkey1
1) "3000"
2) "2000"
127.0.0.1:6379> scard setkey1
(integer) 2
如果集合中没有元素那么返回integer 0
13.sismember setkey value:测试value是否是名称为setkey的集合的元素。返回1代表是,返回0代表不是。
127.0.0.1:6379> smembers setkey1
1) "3000"
2) "2000"
127.0.0.1:6379> sismember setkey1 2000
(integer) 1
127.0.0.1:6379> sismember setkey1 4000
(integer) 0
14.srandmember setkey:随机返回setkey的一个元素,但是不删除元素。
127.0.0.1:6379> smembers setkey11) "1000"
2) "4000"
3) "3000"
4) "5000"
5) "2000"
127.0.0.1:6379> srandmember setkey1
"5000"
127.0.0.1:6379> srandmember setkey1
"2000"
127.0.0.1:6379> srandmember setkey1
"1000"
最后强调一点,set里的数据是无序的。
通过学习并且测试上面的方法,一些细心的同学很容易能察觉到,用set来操作目前流行的sns或者微博的一些动态信息,实时消息或者实时推荐和分析都会非常方便。