Redis Note7:Redis-Key—Set

Redis Note7:Redis-Key—Set

1. Set

set中的值不能重!,set命令都有一个S

1.1 添加:SADD

127.0.0.1:6379[3]> SADD myset "hello" (integer) 1 127.0.0.1:6379[3]> SADD myset "world" (integer) 1 127.0.0.1:6379[3]> SADD myset "redis" (integer) 1 # 查看指定set 127.0.0.1:6379[3]> SMEMBERS myset 1) "hello" 2) "world" 3) "redis" # 判断某一个值是否在指定set 127.0.0.1:6379[3]> SISMEMBER myset hello (integer) 1 127.0.0.1:6379[3]> SISMEMBER myset mongodb (integer) 0

1.2 查看集合元素个数:SCARD

127.0.0.1:6379[3]> SCARD myset (integer) 3

1.1 移除set中的指定元素:srem

127.0.0.1:6379[3]> srem myset hello (integer) 1 127.0.0.1:6379[3]> SMEMBERS myset 1) "world" 2) "redis"

1.2 随机抽选出一个元素:SRANDMEMBER

# 随意抽出一个 127.0.0.1:6379[3]> SRANDMEMBER myset "world" # 随机抽出指定个数 127.0.0.1:6379[3]> SRANDMEMBER myset 2 1) "hello" 2) "redis"

1.3 随机移除元素:spop

127.0.0.1:6379[3]> spop myset "redis" 127.0.0.1:6379[3]> SMEMBERS myset 1) "hello" 2) "world"

1.4 移动:smove

127.0.0.1:6379[3]> SMEMBERS myset 1) "hello" 2) "world" 3) "redis" # 移动指定集合中元素,移动到另一个集合 127.0.0.1:6379[3]> smove myset myset2 "redis" (integer) 1 127.0.0.1:6379[3]> SMEMBERS myset 1) "hello" 2) "world" 127.0.0.1:6379[3]> SMEMBERS myset2 1) "redis" 127.0.0.1:6379[3]>

1.5 差集、交集、并集:SDIFF 、SINTER 、SUNION

127.0.0.1:6379[3]> SMEMBERS myset1 1) "b" 2) "a" 3) "c" 127.0.0.1:6379[3]> SMEMBERS myset2 1) "e" 2) "d" 3) "c" # 差集 127.0.0.1:6379[3]> SDIFF myset1 myset2 1) "b" 2) "a" # 交集 127.0.0.1:6379[3]> SINTER myset1 myset2 1) "c"# 并集 127.0.0.1:6379[3]> SUNION myset1 myset2 1) "a" 2) "e" 3) "c" 4) "b" 5) "d"