image-20230220172907736

  • 通用命令

    • KEYS查看所有符合模板的keys,损耗性能不建议使用

    • DEL删除n个指定key

    • EXISTS查看指定key是否操作

    • EXPIRE设置有效期,单位s

    • TTL查询剩余有效期

      为-1时表示永久有效

  • String类型

    image-20230221163951094

    image-20230221171230034

    • SET添加或者修改已经存在的的键值对
      • SET key value
      • SET key value [nx] [ex [expire]]
    • GET获取指定key的value
    • MSET批量添加多个Strinf类型的键值对
    • MGET获取多个key的value
    • INCR让整型类型自增1
    • INCRBY让整型类型自增指定步长
    • INCRBYFLOAT让浮点类型自增指定步长
    • SETNX添加一个不存在的key,如果key存在,则不进行任何操作
    • SETEX添加指定key的value,并且设置有效期
      • SETEX key expire value
  • Hash类型

    image-20230221171432233

    常见命令

    • HSET添加或修改指定key的field的值
    • HGET获取指定key的field的值
    • HMSET批量
    • HMGET批量
    • HGETALL获取指定key所有的field和value
    • HKEYS获取指定key所有的field
    • HVALS获取指定key所有的value
    • HINCRBY指定key的指定field自增指定步长
    • HSETNX指定key的field是否存在,不存在则添加
  • List类型

    image-20230221185139963

    image-20230221185359862

    运行测试

    127.0.0.1:6379[1]> LPUSH user a b c
    (integer) 3
    127.0.0.1:6379[1]> RPUSH user x y z
    (integer) 6
    127.0.0.1:6379[1]> 

    image-20230221185836460

    127.0.0.1:6379[1]> LPOP user
    "c"
    127.0.0.1:6379[1]> RPOP user
    "z"

    image-20230221185948464

  • Set类型

    image-20230221190537920

    image-20230221191050827

    练习测试

    127.0.0.1:6379[1]> SADD s1 a b c 9 8 7
    (integer) 6
    127.0.0.1:6379[1]> SADD s2 b c 9 8 x y z
    (integer) 7
    127.0.0.1:6379[1]> SMEMBERS s1
    1) "c"
    2) "b"
    3) "a"
    4) "8"
    5) "9"
    6) "7"
    127.0.0.1:6379[1]> SREM s1 a b
    (integer) 2
    127.0.0.1:6379[1]> SMEMBERS s1
    1) "c"
    2) "8"
    3) "9"
    4) "7"
    127.0.0.1:6379[1]> SCARD s1
    (integer) 4
    127.0.0.1:6379[1]> SISMEMBER s1 a
    (integer) 0
    127.0.0.1:6379[1]> SISMEMBER s1 c
    (integer) 1
    127.0.0.1:6379[1]> SINTER s1 s2
    1) "c"
    2) "8"
    3) "9"
    127.0.0.1:6379[1]> SDIFF s1 s2
    1) "7"
    127.0.0.1:6379[1]> SDIFF s2 s1
    1) "y"
    2) "z"
    3) "x"
    4) "b"
    127.0.0.1:6379[1]> SUNION s1 s2
    1) "x"
    2) "c"
    3) "8"
    4) "b"
    5) "y"
    6) "z"
    7) "9"
    8) "7"
    SDIFF key1 key2 比较的是key1中,key2所没有的,两者对调,结果不同

    image-20230221192122191

    127.0.0.1:6379[1]> SADD zhangsan lisi wangwu  zhaoliu
    (integer) 3
    127.0.0.1:6379[1]> SADD lisi wangwu mazi ergou
    (integer) 3
    127.0.0.1:6379[1]> SCARD zhangsan
    (integer) 3
    127.0.0.1:6379[1]> SINTER zhangsan lisi
    1) "wangwu"
    127.0.0.1:6379[1]> SDIFF zhangsan lisi
    1) "zhaoliu"
    2) "lisi"
    127.0.0.1:6379[1]> SUNION zhangsan lisi
    1) "zhaoliu"
    2) "lisi"
    3) "wangwu"
    4) "ergou"
    5) "mazi"
    127.0.0.1:6379[1]> SISMEMBER zhangsan lisi
    (integer) 1
    127.0.0.1:6379[1]> SISMEMBER lisi zhangsan
    (integer) 0
    127.0.0.1:6379[1]> SREM zhangsan lisi
    (integer) 1
  • SortedSet类型

    image-20230221192328304

    底层与Set差异较大

    image-20230221192935171

    测试练习

    image-20230221193433967

    image-20230221193424555

    127.0.0.1:6379[1]> ZADD stus 85 Jack 89 Lucy 82 Rose 95 Tom 78 Jerry 92 Amy 76 Miles
    (integer) 7
    127.0.0.1:6379[1]> ZREM stus Tom
    (integer) 1
    127.0.0.1:6379[1]> ZSCORE stus Amy
    "92"
    127.0.0.1:6379[1]> ZRANK stus Rose
    (integer) 2
    127.0.0.1:6379[1]> ZCOUNT stus 0 80
    (integer) 2
    127.0.0.1:6379[1]> ZINCRBY stus 2 Amy
    "94"
    127.0.0.1:6379[1]> ZREVRANGE stus 0 2
    1) "Amy"
    2) "Lucy"
    3) "Jack"
    127.0.0.1:6379[1]> ZRANGEBYSCORE stus 0 80
    1) "Miles"
    2) "Jerry"