• set类型以及其操作


    sets类型

    sets类型以及操作
    Set是无序集合,它是string类型的无序集合。set是通过hash table实现的,添加、删除和查找的复杂度都是0(1)。对集合我们可以取并集、交集、差集。通过这些操作我们可以实现sns中的好友推荐和blog的tag功能。

    sadd
    向名称为key的set中添加元素,返回添加成功的个数

    127.0.0.1:6379> sadd key member [member ...]
    127.0.0.1:6379> sadd myset1 one
    (integer) 1
    127.0.0.1:6379> sadd myset1 two three
    (integer) 2



    smembers
    查看集合元素

    127.0.0.1:6379> smembers key
    127.0.0.1:6379> smembers myset1
    1) "one"
    2) "two"
    3) "three"



    srem
    删除名称为key的set中元素,返回成功执行的个数

    127.0.0.1:6379> srem key member [member ...]
    127.0.0.1:6379> srem myset1 one
    (integer) 1
    127.0.0.1:6379> smembers myset1
    1) "two"
    2) "three"


    spop
    随机返回并删除名称为key的set中的一个元素

    spop key [count]
    127.0.0.1:6379> spop myset1 2
    1) "two"
    2) "three"
    127.0.0.1:6379> smembers myset1
    (empty list or set)



    sdiff
    返回所有给定key与第一个key的差集
    谁在前面以谁为标准,返回其他集合少于标准的

    127.0.0.1:6379> sdiff key [key ...]
    127.0.0.1:6379> smembers myset2
    1) "two"
    2) "four"
    3) "three"
    127.0.0.1:6379> smembers myset1
    1) "one"
    2) "two"
    3) "three"
    127.0.0.1:6379> sdiff myset1 myset2
    1) "one"
    127.0.0.1:6379> sdiff myset2 myset1
    1) "four"


    sdiffstore
    返回两个集合的差集,并将差集存储到另一个集合里面

    127.0.0.1:6379> sdiffstore destination key [key ...]
    127.0.0.1:6379> sdiffstore myset3 myset1
    (integer) 3
    127.0.0.1:6379> smembers myset3
    1) "one"
    2) "two"
    3) "three"
    127.0.0.1:6379> sdiff myset4 myset1 myset2
    (empty list or set)
    127.0.0.1:6379> smembers myset1
    1) "one"
    2) "two"
    3) "three"
    127.0.0.1:6379> smembers myset2
    1) "two"
    2) "four"
    3) "three"
    127.0.0.1:6379> sdiffstore myset4 myset1 myset2
    (integer) 1
    127.0.0.1:6379> smembers myset4
    1) "one"




    sinter
    返回所有给定key的交集

    127.0.0.1:6379> sinter key [key ...]
    127.0.0.1:6379> smembers myset1
    1) "one"
    2) "two"
    3) "three"
    127.0.0.1:6379> smembers myset2
    1) "two"
    2) "four"
    3) "three"
    127.0.0.1:6379> sinter myset1 myset2
    1) "two"
    2) "three"




    sinterstore
    返回两个集合的交集并且储存在指定集合内

    127.0.0.1:6379> sinterstore destination key [key ...]
    127.0.0.1:6379> sinterstore myset4 myset1 myset2
    (integer) 2
    127.0.0.1:6379> smembers myset4
    1) "two"
    2) "three"



    sunion
    返回所有给定集合的并集

    127.0.0.1:6379> sunion key [key ...]
    127.0.0.1:6379> smembers myset1
    1) "one"
    2) "two"
    3) "three"
    127.0.0.1:6379> smembers myset2
    1) "two"
    2) "four"
    3) "three"
    127.0.0.1:6379> sunion myset1 myset2
    1) "one"
    2) "two"
    3) "four"
    4) "three"



    sunionstrore
    返回给顶集合的并集,并且存储在另一个集合

    127.0.0.1:6379> sunionstore destination key [key ...]
    127.0.0.1:6379> sunionstore myset5 myset1 myset2
    (integer) 4
    127.0.0.1:6379> smembers myset5
    1) "one"
    2) "two"
    3) "four"
    4) "three"



    smove
    从个一的个key中的对应的set中移除member并添加到第二个对应的set中

    127.0.0.1:6379> smove source destination member
    127.0.0.1:6379> smembers myset1
    1) "one"
    2) "two"
    127.0.0.1:6379> smembers myset2
    1) "two"
    2) "four"
    3) "three"
    127.0.0.1:6379> smove myset1 myset2 one
    (integer) 1
    127.0.0.1:6379> smembers myset1
    1) "two"
    127.0.0.1:6379> smembers myset2
    1) "one"
    2) "two"
    3) "four"
    4) "three"



    scard
    返回名称为key的集合中的集合的个数

    127.0.0.1:6379> scard key
    127.0.0.1:6379> scard myset1
    (integer) 1
    127.0.0.1:6379> scard myset2
    (integer) 4



    sismember
    测试某个元素是不是某个集合的元素

    127.0.0.1:6379> sismember key member
    127.0.0.1:6379> sismember myset1 one
    (integer) 0
    127.0.0.1:6379> sismember myset two
    (integer) 0
    127.0.0.1:6379> smembers myset1
    1) "two"
    127.0.0.1:6379> sismember myset1 two
    (integer) 1



    srandmember
    随机返回集合中的一个元素,但不删除

    127.0.0.1:6379> srandmember key [count]
    127.0.0.1:6379> srandmember myset2
    "four"
    127.0.0.1:6379> srandmember myset2
    "one"
    127.0.0.1:6379> srandmember myset2 6
    1) "one"
    2) "two"
    3) "three"
    4) "four"



  • 相关阅读:
    7. ZooKeeper的stat结构
    6. ZooKeeper访问控制列表
    5. 监视和ZooKeeper操作
    4. ZooKeeper 基本操作
    3.Apache ZooKeeper数据模型
    Eclipse安装Activiti Designer插件
    Javascript Canvas验证码
    Tomcat9配置SSL连接
    JAVA将异常的堆栈信息转成String
    SpringBoot2静态资料访问
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/6598894.html
Copyright © 2020-2023  润新知