redis之Set(无序)类型常用方法总结
存--sadd key member [member ...]
取--SMEMBERS key
sadd key member [member ...] 向集合添加一个或多个成员
127.0.0.1:6379> sadd lt zhangsan lishi wangwu (integer) 3 127.0.0.1:6379> smembers lt 1) "wangwu" 2) "lishi" 3) "zhangsan"
smembers key 获取一个key对应的集合元素
127.0.0.1:6379> sadd lt zhangsan lishi wangwu (integer) 3 127.0.0.1:6379> smembers lt 1) "wangwu" 2) "lishi" 3) "zhangsan"
scard key 获取集合的成员数
127.0.0.1:6379> sadd lt zhangsan lishi wangwu (integer) 3 127.0.0.1:6379> smembers lt 1) "wangwu" 2) "lishi" 3) "zhangsan" 127.0.0.1:6379> scard lt (integer) 3
sdiff key1 [key2] 返回给定所有集合的差集
127.0.0.1:6379> sadd lt zhangsan lishi wangwu (integer) 3 127.0.0.1:6379> smembers lt 1) "wangwu" 2) "lishi" 3) "zhangsan" 127.0.0.1:6379> scard lt (integer) 3 127.0.0.1:6379> sadd plf lishi wangwu jia yi (integer) 4 127.0.0.1:6379> SDIFF plf lt 1) "yi" 2) "jia" 127.0.0.1:6379> SDIFF lt plf 1) "zhangsan"
SDIFFSTORE destination key1 [key2] 返回给定所有集合的差集并存储在 destination 中
redis 127.0.0.1:6379> SADD myset "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset "foo" (integer) 1 redis 127.0.0.1:6379> SADD myset "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "world" (integer) 1 redis 127.0.0.1:6379> SDIFFSTORE destset myset myset2 (integer) 2 redis 127.0.0.1:6379> SMEMBERS destset 1) "foo" 2) "bar"
SINTER key1 [key2] 返回给定所有集合的交集
127.0.0.1:6379> sadd lt zhangsan lishi wangwu (integer) 3 127.0.0.1:6379> sadd plf lishi wangwu jia yi (integer) 4 127.0.0.1:6379> sinter plf lt 1) "wangwu" 2) "lishi"
SINTERSTORE destination key1 [key2] 返回给定所有集合的交集并存储在 destination 中
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "foo" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "world" (integer) 1 redis 127.0.0.1:6379> SINTERSTORE myset myset1 myset2 (integer) 1 redis 127.0.0.1:6379> SMEMBERS myset 1) "hello"
SISMEMBER key member 判断 member 元素是否是集合 key 的成员
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SISMEMBER myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SISMEMBER myset1 "world" (integer) 0
SMEMBERS key 返回集合中的所有成员
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SMEMBERS myset1 1) "World" 2) "Hello"
SMOVE source destination member 将 member 元素从 source 集合移动到 destination 集合
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "foo" (integer) 1 redis 127.0.0.1:6379> SMOVE myset1 myset2 "bar" (integer) 1 redis 127.0.0.1:6379> SMEMBERS myset1 1) "World" 2) "Hello" redis 127.0.0.1:6379> SMEMBERS myset2 1) "foo" 2) "bar"
SPOP key 移除并返回集合中的一个随机元素
redis> SADD myset "one" (integer) 1 redis> SADD myset "two" (integer) 1 redis> SADD myset "three" (integer) 1 redis> SPOP myset "one" redis> SMEMBERS myset 1) "three" 2) "two" redis> SADD myset "four" (integer) 1 redis> SADD myset "five" (integer) 1 redis> SPOP myset 3 1) "five" 2) "four" 3) "two" redis> SMEMBERS myset 1) "three" redis>
SRANDMEMBER key [count] 返回集合中一个或多个随机数
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SRANDMEMBER myset1 "bar" redis 127.0.0.1:6379> SRANDMEMBER myset1 2 1) "Hello" 2) "world"
SREM key member1 [member2] 移除集合中一个或多个成员
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SREM myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SREM myset1 "foo" (integer) 0 redis 127.0.0.1:6379> SMEMBERS myset1 1) "bar" 2) "world"
SUNION key1 [key2] 返回所有给定集合的并集
redis> SADD key1 "a" (integer) 1 redis> SADD key1 "b" (integer) 1 redis> SADD key1 "c" (integer) 1 redis> SADD key2 "c" (integer) 1 redis> SADD key2 "d" (integer) 1 redis> SADD key2 "e" (integer) 1 redis> SUNION key1 key2 1) "a" 2) "c" 3) "b" 4) "e" 5) "d" redis>
SUNIONSTORE destination key1 [key2]所有给定集合的并集存储在 destination 集合中
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "world" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset2 "bar" (integer) 1 redis 127.0.0.1:6379> SUNIONSTORE myset myset1 myset2 (integer) 1 redis 127.0.0.1:6379> SMEMBERS myset 1) "bar" 2) "world" 3) "hello" 4) "foo"
SSCAN key cursor [MATCH pattern] [COUNT count] 迭代集合中的元素
redis 127.0.0.1:6379> SADD myset1 "hello" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "hi" (integer) 1 redis 127.0.0.1:6379> SADD myset1 "bar" (integer) 1 redis 127.0.0.1:6379> sscan myset1 0 match h* 1) "0" 2) 1) "hello" 2) "h1"