redis之Set(无序)类型常用方法总结

redis之Set(无序)类型常用方法总结

存--sadd key member [member ...]
取--SMEMBERS key
  1. 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"
  2. 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"
  3. 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
  4. 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"
  5. 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"
  6. 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"
  7. 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"
  8. 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
  9. 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"
  10. 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"
  11. 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> 
  12. 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"
  13. 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"
  14. 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> 
  15. 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"
  16. 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"

转载于:https://www.cnblogs.com/plf-Jack/p/11080467.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值