sadd:添加集合元素
127.0.0.1:6379> sadd runnoobkey redis mongodb mysql
(integer) 3
scard:查看集合數(shù)量
127.0.0.1:6379> SCARD runnoobkey
(integer) 3
sdiff:集合左交差集
127.0.0.1:6379> sadd key1 'a' 'c' 'b'
(integer) 3
127.0.0.1:6379> sadd key1 'd' 'f' 'j'
(integer) 3
127.0.0.1:6379> SADD key2 'a' 'b' 'n'
(integer) 3
127.0.0.1:6379> SDIFF key1 key2
1) "c"
2) "f"
3) "j"
4) "d"
Sdiffstore: 命令將給定集合之間的差集存儲(chǔ)在指定的集合中稽亏。如果指定的集合 key 已存在,則會(huì)被覆蓋念颈。
127.0.0.1:6379> sadd name 'liyi' 'lier' 'lisan'
(integer) 3
127.0.0.1:6379> sadd name1 'liyi' 'wangyi'
(integer) 2
127.0.0.1:6379> SDIFFSTORE name name1
(integer) 2
Sinter :命令返回給定所有給定集合的交集浙于。 不存在的集合 key 被視為空集稻轨。 當(dāng)給定集合當(dāng)中有一個(gè)空集時(shí)政钟,結(jié)果也為空集(根據(jù)集合運(yùn)算定律)
127.0.0.1:6379> SMEMBERS name1
1) "liyi"
2) "wangyi"
127.0.0.1:6379> SMEMBERS name
1) "wangwu"
2) "liyi"
3) "wangyi"
127.0.0.1:6379> sinter name name1
1) "liyi"
2) "wangyi"
Smembers:返回集合中的所有成員
127.0.0.1:6379> SMEMBERS name1
1) "liyi"
2) "wangyi"
127.0.0.1:6379> SMEMBERS name
1) "wangwu"
2) "liyi"
3) "wangyi"
sismember: 命令判斷成員元素是否是集合的成員
127.0.0.1:6379> SMEMBERS name
1) "wangwu"
2) "liyi"
3) "wangyi"
127.0.0.1:6379> SISMEMBER name liyi
(integer) 1
127.0.0.1:6379> SISMEMBER name wangwang
(integer) 0
127.0.0.1:6379>
Sinterstore:返回給定所有集合的交集并存儲(chǔ)在 destination 中
127.0.0.1:6379> SINTERSTORE key3 name name1
(integer) 2
127.0.0.1:6379> SMEMBERS key3
1) "liyi"
2) "wangyi"
Smove :命令將指定成員 wangwu元素從 name集合移動(dòng)到 name1集合
127.0.0.1:6379> SMEMBERS name1
1) "liyi"
2) "wangyi"
127.0.0.1:6379> SMEMBERS name
1) "wangwu"
2) "liyi"
127.0.0.1:6379> smove name name1 "wangwu"
(integer) 1
127.0.0.1:6379> SMEMBERS name1
1) "wangwu"
2) "liyi"
3) "wangyi"
127.0.0.1:6379> SMEMBERS name
1) "liyi"
Spop:移除并返回集合中的一個(gè)隨機(jī)元素
127.0.0.1:6379> SMEMBERS name1
1) "wangwu"
2) "liyi"
3) "wangyi"
127.0.0.1:6379> spop name1
"liyi"
127.0.0.1:6379> SMEMBERS name1
1) "wangwu"
2) "wangyi"
127.0.0.1:6379>
Srandmember :命令用于返回集合中的一個(gè)隨機(jī)元素。
127.0.0.1:6379> SRANDMEMBER name1 2
1) "wangwu"
2) "wangyi"
127.0.0.1:6379> SRANDMEMBER name1 1
1) "wangyi"
Srem:移除集合中一個(gè)或多個(gè)成員
127.0.0.1:6379> SMEMBERS name1
1) "wangwu"
2) "wangyi"
127.0.0.1:6379> SREM name1 'wangwu' 'wangyi'
(integer) 2
127.0.0.1:6379> SMEMBERS name1
(empty list or set)
127.0.0.1:6379>
Sunion:返回所有給定集合的并集
127.0.0.1:6379> SMEMBERS name
1) "wugangang"
2) "wuyue"
3) "liyi"
127.0.0.1:6379> sadd name1 'zhangsan' 'zhangyi'
(integer) 2
127.0.0.1:6379> SMEMBERS name1
1) "zhangsan"
2) "zhangyi"
127.0.0.1:6379> SUNION name name1
1) "zhangsan"
2) "wugangang"
3) "liyi"
4) "wuyue"
5) "zhangyi"
Sunionstore:所有給定集合的并集存儲(chǔ)在 name2 集合中
127.0.0.1:6379> SMEMBERS name1
1) "zhangsan"
2) "zhangyi"
127.0.0.1:6379> SMEMBERS name
1) "zhangsan"
2) "ganggang"
3) "zhangyi"
127.0.0.1:6379> sunionstore name2 name1 name
(integer) 3
127.0.0.1:6379> SMEMBERS name2
1) "zhangsan"
2) "ganggang"
3) "zhangyi"
Sscan:迭代集合中的元素
127.0.0.1:6379> sscan name2 0 match z*
1) "0"
2) 1) "zhangyi"
2) "zhangsan"