redis基本操作——set
一、向集合添加一个或多个成员
命令:sadd
格式:sadd key member1 [member2]
127.0.0.1:6379> sadd set_key m1(integer)
1127.0.0.1:6379> sadd set_key m2 m3
(integer) 2
二、返回集合中的所有成员
命令:smembers
格式:smembers key
127.0.0.1:6379> smembers set_key1) "m3"2) "m2"
3) "m1"
三、移除并返回集合中的一个随机元素
命令:spop
格式:spop key
127.0.0.1:6379> sadd set_key m1 m2 m3 m4 m5 m6 m7 m8 m9(integer)
9127.0.0.1:6379> smembers set_key
1) "m8"
2) "m2"
3) "m3"
4) "m6"
5) "m1"
6) "m5"
7) "m4"
8) "m7"
9) "m9"
127.0.0.1:6379> spop set_key
"m5"
127.0.0.1:6379> smembers set_key
1) "m6"
2) "m1"
3) "m4"
4) "m7"
5) "m8"
6) "m3"
7) "m2"
8) "m9"
四、移除集合中一个或多个成员
命令:srem
格式:srem set_key member1 [member2]
127.0.0.1:6379> smembers set_key1) "m6"2) "m1"
3) "m4"
4) "m7"
5) "m8"
6) "m3"
7) "m2"
8) "m9"
127.0.0.1:6379> srem set_key m1 m2 m3
(integer) 3
127.0.0.1:6379> smembers set_key
1) "m8"
2) "m4"
3) "m6"
4) "m9"
5) "m7"
127.0.0.1:6379> srem set_key m1 m2 m3
(integer) 0
127.0.0.1:6379> smembers set_key
1) "m8"
2) "m4"
3) "m6"
4) "m9"
5) "m7"
五、判断 member 元素是否是集合 key 的成员
命令:sismember
格式:sismember key member
127.0.0.1:6379> smembers set_key1) "m8"2) "m4"
3) "m6"
4) "m9"
5) "m7"
127.0.0.1:6379> sismember set_key m8
(integer) 1
127.0.0.1:6379> sismember set_key m1
(integer) 0
六、获取集合的成员数
命令:scard
格式:scard key
127.0.0.1:6379> smembers set_key1) "m8"2) "m4"
3) "m6"
4) "m9"
5) "m7"
127.0.0.1:6379> scard set_key
(integer) 5
七、返回给定所有集合的差集
命令:sdiff
格式:sdiff key1 [key2] ...
127.0.0.1:6379> sadd k1 m1 m2 m3(integer)
3127.0.0.1:6379> sadd k2 m3 m4 m5
(integer) 3
127.0.0.1:6379> sdiff k1
1) "m2"
2) "m3"
3) "m1"
127.0.0.1:6379> sdiff k1 k2
1) "m2"
2) "m1"
127.0.0.1:6379> sdiff k1 k2
1) "m2"
2) "m1"
127.0.0.1:6379> sdiff k2 k1
1) "m4"
2) "m5"
127.0.0.1:6379> sadd k3 m5 m6 m7
(integer) 3
127.0.0.1:6379> sdiff k1 k2 k3
1) "m2"
2) "m1"
127.0.0.1:6379> sdiff k2 k1 k3
1) "m4"
八、返回给定所有集合的差集并存储在 destination 中
命令:sdiffstore
格式:sdiffstore destination key1 [key2] ...
127.0.0.1:6379> sadd k1 m1 m2 m3(integer)
3127.0.0.1:6379> sadd k2 m3 m4 m5
(integer) 3
127.0.0.1:6379> sadd k3 m5 m6 m7
(integer) 3
127.0.0.1:6379> sdiffstore d1 k1 k2
(integer) 2
127.0.0.1:6379> smembers d1
1) "m2"
2) "m1"
127.0.0.1:6379> sdiffstore d2 k2 k1
(integer) 2
127.0.0.1:6379> smembers d2
1) "m4"
2) "m5"
127.0.0.1:6379> sdiff d1 k3
1) "m2"
2) "m1"
127.0.0.1:6379> sdiff k1 k2 k3
1) "m2"
2) "m1"
127.0.0.1:6379> sdiff d2 k3
1) "m4"
127.0.0.1:6379> sdiff k2 k1 k3
1) "m4"
九、返回给定所有集合的交集
命令:sinter
格式:sinter key1 [key2] ...
127.0.0.1:6379> sadd k1 m1 m2 m3(integer)
3127.0.0.1:6379> sadd k2 m2 m3 m4
(integer) 3
127.0.0.1:6379> sadd k3 m3 m4 m5
(integer) 3
127.0.0.1:6379> sinter k1 k2
1) "m3"
2) "m2"
127.0.0.1:6379> sinter k1 k2 k3
1) "m3"
127.0.0.1:6379> sinter k2 k1
1) "m3"
2) "m2"
127.0.0.1:6379> sinter k2 k1 k3
1) "m3"
十、返回给定所有集合的交集并存储在 destination 中
命令:sinterstore
格式:sinterstore destination key1 [key2] ...
127.0.0.1:6379> sadd k1 m1 m2 m3(integer)
3127.0.0.1:6379> sadd k2 m2 m3 m4
(integer) 3
127.0.0.1:6379> sadd k3 m3 m4 m5
(integer) 3
127.0.0.1:6379> sinter k1 k2
1) "m3"
2) "m2"
127.0.0.1:6379> sinter k1 k2 k3
1) "m3"
127.0.0.1:6379> sinter k2 k1
1) "m3"
2) "m2"
127.0.0.1:6379> sinter k2 k1 k3
1) "m3"
127.0.0.1:6379> sinterstore d1 k1 k2
(integer) 2
127.0.0.1:6379> smembers d1
1) "m2"
2) "m3"
127.0.0.1:6379> sinterstore d2 k1 k2 k3
(integer) 1
127.0.0.1:6379> smembers d2
1) "m3"
十一、将 member 元素从 source 集合移动到 destination 集合
命令:smove
格式:smove src des members
127.0.0.1:6379> sadd src m1 m2 m3(integer)
3127.0.0.1:6379> sadd des m4 m5 m6
(integer) 3
127.0.0.1:6379> smembers src
1) "m3"
2) "m2"
3) "m1"
127.0.0.1:6379> smembers des
1) "m4"
2) "m6"
3) "m5"
127.0.0.1:6379> smove src des m1
(integer) 1
127.0.0.1:6379> smembers src
1) "m3"
2) "m2"
127.0.0.1:6379> smembers des
1) "m4"
2) "m6"
3) "m1"
4) "m5"
127.0.0.1:6379> smove src des m0
(integer) 0
127.0.0.1:6379> smembers src
1) "m3"
2) "m2"
127.0.0.1:6379> smembers des
1) "m4"
2) "m6"
3) "m1"
4) "m5"
十二、返回集合中一个或多个随机数
命令:srandmember
格式:srandmember key [count]
127.0.0.1:6379> sadd rand_set m1 m2 m3 m4 m5 m6 m7 m8(integer)
8127.0.0.1:6379> smembers rand_set
1) "m8"
2) "m2"
3) "m3"
4) "m6"
5) "m1"
6) "m5"
7) "m4"
8) "m7"
127.0.0.1:6379> srandmember rand_set
"m8"
127.0.0.1:6379> srandmember rand_set
"m6"
127.0.0.1:6379> srandmember rand_set 3
1) "m8"
2) "m5"
3) "m7"
十三、返回所有给定集合的并集
命令:sunion
格式:sunion key1 [key2] ...
127.0.0.1:6379> sadd k1 m1 m2(integer)
2127.0.0.1:6379> sadd k2 m3 m4
(integer) 2
127.0.0.1:6379> sadd k3 m1 m3 m5 m6
(integer) 4
127.0.0.1:6379> sunion k1 k2
1) "m4"
2) "m3"
3) "m2"
4) "m1"
127.0.0.1:6379> sunion k1 k3
1) "m6"
2) "m1"
3) "m2"
4) "m3"
5) "m5"
127.0.0.1:6379> sunion k1 k2 k3
1) "m2"
2) "m3"
3) "m6"
4) "m1"
5) "m5"
6) "m4"
十四、所有给定集合的并集存储在 destination 集合中
命令:sunionstore
格式:sunionstore destination key1 [key2] ...
127.0.0.1:6379> sadd k1 m1 m2(integer)
2127.0.0.1:6379> sadd k2 m3 m4
(integer) 2
127.0.0.1:6379> sadd k3 m1 m3 m5 m6
(integer) 4
127.0.0.1:6379> sunion k1 k2
1) "m4"
2) "m3"
3) "m2"
4) "m1"
127.0.0.1:6379> sunion k1 k3
1) "m6"
2) "m1"
3) "m2"
4) "m3"
5) "m5"
127.0.0.1:6379> sunion k1 k2 k3
1) "m2"
2) "m3"
3) "m6"
4) "m1"
5) "m5"
6) "m4"
127.0.0.1:6379> sunionstore d1 k1 k2
(integer) 4
127.0.0.1:6379> smembers d1
1) "m4"
2) "m3"
3) "m2"
4) "m1"
127.0.0.1:6379> sunionstore d2 k1 k2 k3
(integer) 6
127.0.0.1:6379> smembers d2
1) "m2"
2) "m3"
3) "m6"
4) "m1"
5) "m5"
6) "m4"
十五、迭代集合中的元素
命令:sscan
格式:sscan key cursor [MATCH pattern] [COUNT count]
127.0.0.1:6379> sadd set_key m1 m2 m3 member1 member2 member3(integer)
6127.0.0.1:6379> sscan set_key 0
1) "0"
2) 1) "member1"
2) "member3"
3) "m2"
4) "m3"
5) "member2"
6) "m1"
127.0.0.1:6379> sscan set_key 0 MATCH m*
1) "0"
2) 1) "member1"
2) "member3"
3) "m2"
4) "m3"
5) "member2"
6) "m1"
127.0.0.1:6379> sscan set_key 0 MATCH me*
1) "0"
2) 1) "member1"
2) "member3"
3) "member2"
127.0.0.1:6379> sscan set_key 0 MATCH me* COUNT 1
1) "1"
2) 1) "member1"
2) "member3"
127.0.0.1:6379> sscan set_key 0 MATCH me* COUNT 0
(error) ERR syntax error
127.0.0.1:6379> sscan set_key 0 MATCH me* COUNT 3
1) "3"
2) 1) "member1"
2) "member3"
3) "member2"
参考链接:https://www.runoob.com/redis/redis-strings.html
以上是 redis基本操作——set 的全部内容, 来源链接: utcz.com/z/533989.html