美文网首页
redis常用命令

redis常用命令

作者: 紫色红色黑色 | 来源:发表于2019-10-27 17:14 被阅读0次

    redis种对value支持5种数据结构。String、List、Set、Hash、ZSet。

    字符串

    基本命令 行为
    get 获取给定键的值
    set 设置给定键的值
    del 删除给定键的值
    redis-primary:6379> set name lucy
    OK
    redis-primary:6379> get name
    "lucy"
    redis-primary:6379> del name
    (integer) 1
    redis-primary:6379> get name
    (nil)
    redis-primary:6379>
    
    
    自增命令 行为
    incr 键存储的值加1
    decr 键存储的值减1
    incrby 键存储的值增加给定整数
    decrby 键存储的值减去给定整数
    incrbyfloat 键存储的值增加给定浮点数
    redis-primary:6379> set num 10
    OK
    redis-primary:6379> get num
    "10"
    redis-primary:6379> incr num
    (integer) 11
    redis-primary:6379> decr num
    (integer) 10
    redis-primary:6379> incrby num 10
    (integer) 20
    redis-primary:6379> decrby num 4
    (integer) 16
    redis-primary:6379> incrbyfloat num 0.99
    "16.99"
    redis-primary:6379>
    
    字符串处理命令 行为
    append 给定键的值末尾拼接给定的值
    getrange 获取值在偏移量start至end范围内的字符串,包含start,end在内
    setrange 将从偏移量start开始的字符串设置为给定值
    redis-primary:6379> set sayhi hello
    OK
    redis-primary:6379> get sayhi
    "hello"
    redis-primary:6379> append sayhi ,world
    (integer) 11
    redis-primary:6379> get sayhi
    "hello,world"
    redis-primary:6379> getrange sayhi 6 10
    "world"
    redis-primary:6379> setrange sayhi 5 /
    (integer) 11
    redis-primary:6379> get sayhi
    "hello/world"
    redis-primary:6379>
    

    列表(List)

    命令 行为
    lpush 列表左侧压入给定值
    lpop 列表左侧弹出给定值,返回弹出值
    lrange 获取列表在给定范围上的值
    lindex 获取列表给定下标下的值
    (integer) 1
    redis-primary:6379> lpush names lilly
    (integer) 2
    redis-primary:6379> lrange names 0 -1
    1) "lilly"
    2) "lucy"
    redis-primary:6379> lindex names 0
    "lilly"
    redis-primary:6379> lpop names
    "lilly"
    redis-primary:6379> lrange names 0 -1
    1) "lucy"
    redis-primary:6379>
    
    截取命令 行为
    ltrim 对列表截取,保留偏移量start至end在内的元素,包含start和end
    redis-primary:6379> lpush names lucy lilly hanmeimei kate
    (integer) 4
    redis-primary:6379> lrange names 0 -1
    1) "kate"
    2) "hanmeimei"
    3) "lilly"
    4) "lucy"
    redis-primary:6379> ltrim names 2 3
    OK
    redis-primary:6379> lrange names 0 -1
    1) "lilly"
    2) "lucy"
    redis-primary:6379>
    
    阻塞弹出命令 行为
    blpop 从非空列表中弹出最左侧元素,timeout内阻塞等待元素
    brpop 从非空列表中弹出最右侧元素,timeout内阻塞等待元素
    rpoplpush 从第一个列表中取出最右侧元素,放入第二个列表中最左侧
    brpoplpush 从第一个列表中取出最右侧元素,放入第二个列表中最左侧,timeout内阻塞等待元素
    //第一个客户端
    redis-primary:6379> blpop list1 10
    1) "list1"
    2) "a"
    (2.01s)
    redis-primary:6379>
    //第二个客户端
    redis-primary:6379> lpush list1 a
    (integer) 1
    redis-primary:6379>
    
    redis-primary:6379> lpush list1 a b
    (integer) 2
    redis-primary:6379> lpush list2 c d
    (integer) 2
    redis-primary:6379> lrange list1 0 -1
    1) "b"
    2) "a"
    redis-primary:6379> lrange list2 0 -1
    1) "d"
    2) "c"
    redis-primary:6379> rpoplpush list1 list2
    "a"
    redis-primary:6379> lrange list1 0 -1
    1) "b"
    redis-primary:6379> lrange list2 0 -1
    1) "a"
    2) "d"
    3) "c"
    redis-primary:6379>
    
    //第一个客户端
    redis-primary:6379> brpoplpush list1 list2 10
    "a"
    (1.78s)
    redis-primary:6379> lrange list2 0 -1
    1) "a"
    redis-primary:6379> lrange list1 0 -1
    (empty list or set)
    redis-primary:6379>
    //第二个客户端
    redis-primary:6379> lpush list1 a
    (integer) 1
    redis-primary:6379>
    

    集合(Set)

    无序不重复集合

    命令 行为
    sadd 向集合添加给定值
    smembers 返回集合所有值
    sismember 检查给定值是否存在于集合
    srem 在集合中删除给定值
    redis-primary:6379> sadd names lucy
    (integer) 1
    redis-primary:6379> sadd names lilly
    (integer) 1
    redis-primary:6379> smembers names
    1) "lilly"
    2) "lucy"
    redis-primary:6379> sismember names lucy
    (integer) 1
    redis-primary:6379> srem names lucy
    (integer) 1
    redis-primary:6379> smembers names
    1) "lilly"
    redis-primary:6379>
    
    随机移除命令 行为
    scard 返回集合元素数
    spop 随机移除一个元素,返回该元素
    smove 如果第一个集合包含该元素,就将该元素添加到第二个集合中
    redis-primary:6379> sadd names lucy lilly kate
    (integer) 3
    redis-primary:6379> smembers names
    1) "kate"
    2) "lilly"
    3) "lucy"
    redis-primary:6379> smove names names01 lilly
    (integer) 1
    redis-primary:6379> smembers names
    1) "kate"
    2) "lucy"
    redis-primary:6379> smembers names01
    1) "lilly"
    redis-primary:6379> spop names
    "lucy"
    redis-primary:6379> smembers names
    1) "kate"
    redis-primary:6379>
    
    组合命令 行为
    sdiff 返回存在于第一个集合,不存在其他集合中的元素,差集
    sdiffstore 差集数据存储到给定集合
    sinter 返回同时存在于所有集合的元素,交集
    sinterstore
    sunion 返回至少存在一次的元素,并集
    sunionstore
    redis-primary:6379> sadd alpha01 a b c d
    (integer) 4
    redis-primary:6379> sadd alpha02 b d e f
    (integer) 4
    redis-primary:6379> sdiff alpha01 alpha02
    1) "a"
    2) "c"
    redis-primary:6379> sinter alpha01 alpha02
    1) "b"
    2) "d"
    redis-primary:6379> sunion alpha01 alpha02
    1) "f"
    2) "b"
    3) "e"
    4) "d"
    5) "c"
    6) "a"
    redis-primary:6379> sdiffstore diffalpha alpha01 alpha02
    (integer) 2
    redis-primary:6379> smembers diffalpha
    1) "a"
    2) "c"
    redis-primary:6379> sinterstore interalpha alpha01 alpha02
    (integer) 2
    redis-primary:6379> smembers interalpha
    1) "b"
    2) "d"
    redis-primary:6379> sunionstore unionalpha alpha01 alpha02
    (integer) 6
    redis-primary:6379> smembers unionalpha
    1) "f"
    2) "b"
    3) "e"
    4) "d"
    5) "c"
    6) "a"
    redis-primary:6379>
    

    散列

    命令 行为
    hset 向散列添加给定的键值对
    hget 获取散列中指定键的值
    hgetall 获取给定散列所有的键值对
    hdel 删除散列中给定的键值对
    redis-primary:6379> hset lucy name lucy
    (integer) 1
    redis-primary:6379> hset lucy age 12
    (integer) 1
    redis-primary:6379> hset lucy gender girl
    (integer) 1
    redis-primary:6379> hget lucy name
    "lucy"
    redis-primary:6379> hgetall lucy
    1) "name"
    2) "lucy"
    3) "age"
    4) "12"
    5) "gender"
    6) "girl"
    redis-primary:6379> hdel lucy gender
    (integer) 1
    redis-primary:6379> del lucy
    (integer) 1
    redis-primary:6379>
    
    批量命令 行为
    hmset 批量设置键值对
    hmget 获取一个或多个键的值
    hdel 删除散列中的一个或多个键值对
    hlen 返回散列中键值对数量
    redis-primary:6379> hmset lucy name lucy age 12 gender girl
    OK
    redis-primary:6379> hlen lucy
    (integer) 3
    redis-primary:6379> hgetall lucy
    1) "name"
    2) "lucy"
    3) "age"
    4) "12"
    5) "gender"
    6) "girl"
    redis-primary:6379> hmget lucy name age
    1) "lucy"
    2) "12"
    redis-primary:6379> hdel lucy name age
    (integer) 2
    redis-primary:6379> hgetall lucy
    1) "gender"
    2) "girl"
    redis-primary:6379>
    
    键值对命令 行为
    hexists 键是否存在散列中
    hkeys 返回散列中所有的键
    hvals 返回散列中所有的值
    hincrby 散列中给定键的值增加指定整数
    hincrbyfloat 散列中给定键的值增加指定浮点数
    redis-primary:6379> hmset lucy name lucy age 12 gender girl
    OK
    redis-primary:6379> hexists lucy name
    (integer) 1
    redis-primary:6379> hkeys lucy
    1) "name"
    2) "age"
    3) "gender"
    redis-primary:6379> hvals lucy
    1) "lucy"
    2) "12"
    3) "girl"
    redis-primary:6379> hgetall lucy
    1) "name"
    2) "lucy"
    3) "age"
    4) "12"
    5) "gender"
    6) "girl"
    redis-primary:6379> hincrby lucy age 10
    (integer) 22
    redis-primary:6379> hget lucy age
    "22"
    redis-primary:6379> hincrbyfloat lucy age 10.2
    "32.2"
    redis-primary:6379> hget lucy age
    "32.2"
    redis-primary:6379>
    

    有序集合(ZSet)

    有序集合和散列一样,存储的是键值对。键称为成员(member),值称为分值(score)。是redis中唯一可以用键访问分值,可以用分值排列顺序访问元素的数据结构

    命令 行为
    zadd 向有序集合添加给定分值的成员
    zrange 根据元素的排列顺序获取给定范围的多个元素
    zrangebyscores 获取有序集合在给定分值范围内的所有元素
    zrem 在有序集合中删除给定成员
    redis-primary:6379> zadd sort 1 lucy
    (integer) 1
    redis-primary:6379> zadd sort 0 lilly
    (integer) 1
    redis-primary:6379> zadd sort 3 hanmeimei
    (integer) 1
    redis-primary:6379> zrange sort 0 -1 withscores
    1) "lilly"
    2) "0"
    3) "lucy"
    4) "1"
    5) "hanmeimei"
    6) "3"
    redis-primary:6379> zrem sort lucy
    (integer) 1
    redis-primary:6379> zrange sort 0 -1 withscores
    1) "lilly"
    2) "0"
    3) "hanmeimei"
    4) "3"
    redis-primary:6379> zrangebyscore sort 0 2
    1) "lilly"
    redis-primary:6379> zrangebyscore sort 0 2 withscores
    1) "lilly"
    2) "0"
    redis-primary:6379>
    
    命令 行为
    zcard 返回成员数
    zincrby 给定成员的分值增加指定值
    zscore 获取给定成员的分值
    zrank 获取成员在有序集合中的排序
    zcount 获取给定最小分值和最大分值中的成员数量
    redis-primary:6379> zadd sort 12 lucy 34 lilly 1 hanmeimei 28 kate
    (integer) 4
    redis-primary:6379> zcard sort
    (integer) 4
    redis-primary:6379> zincrby sort 10 lilly
    "44"
    redis-primary:6379> zscore sort lilly
    "44"
    redis-primary:6379> zcount sort 0 100
    (integer) 4
    redis-primary:6379> zrank sort lilly
    (integer) 3
    redis-primary:6379> zrank sort hanmeimei
    (integer) 0
    
    命令 行为
    zrevrank 返回成员的排名,成员按照从大到小排列
    zrevrange 返回有序集合给定排名范围内的成员,成员按照从大到小排列
    zrangebyscore 返回有序集合中分值位于min和max之间的所有成员
    zrevrangebyscore 返回有序集合中分值位于min和max之间的所有成员,并按照分值从大到小排列
    zremrangebyrank 移除有序集合中排名介于start和end之间的所有成员
    zremrangebyscore 移除有序集合中分值介于min和max之间的所有成员
    zinterstore 根据member执行交集,score根据指令sum|max|min执行相应操作
    zunionstore 根据member执行并集,score根据指令sum|max|min执行相应操作
    redis-primary:6379> zadd lucy 100 english 60 chinses 80 math
    (integer) 3
    redis-primary:6379> zadd lilly 90 english 89 chemical
    (integer) 2
    redis-primary:6379> zinterstore result1 2 lucy lilly
    (integer) 1
    redis-primary:6379> zrange result1 0 -1 withscores
    1) "english"
    2) "190"
    redis-primary:6379> zinterstore result1 2 lucy lilly aggregate min
    (integer) 1
    redis-primary:6379> zrange result1 0 -1 withscores
    1) "english"
    2) "90"
    redis-primary:6379>
    

    发布订阅

    命令 行为
    publish 向通道发送消息
    subscribe 订阅指定通道
    //发送方
    redis-primary:6379> publish channel01 hello
    (integer) 1
    redis-primary:6379> publish channel01 lucy
    (integer) 1
    redis-primary:6379> publish channel01 pattern:receive,message:lucy001
    (integer) 1
    redis-primary:6379>
    
    //接受方
    //redis-cli订阅消息后无法执行别的命令
    redis-primary:6379> subscribe channel01
    Reading messages... (press Ctrl-C to quit)
    1) "subscribe"
    2) "channel01"
    3) (integer) 1
    1) "message"
    2) "channel01"
    3) "hello"
    1) "message"
    2) "channel01"
    3) "lucy"
    1) "message"
    2) "channel01"
    3) "pattern:receive,message:lucy001"
    
    

    过期时间

    命令 行为
    expire 给键设置过期时间,单位秒
    expireat 设置过期时间为unix时间戳
    persist 移除键的过期时间
    ttl 查看键过期时间还剩多少秒
    redis-primary:6379> set name lucy
    OK
    redis-primary:6379> expire name 100
    (integer) 1
    redis-primary:6379> ttl name
    (integer) 97
    redis-primary:6379> persist name
    (integer) 1
    redis-primary:6379> ttl name
    (integer) -1
    redis-primary:6379> get name
    "lucy"
    redis-primary:6379> pttl name
    (integer) -1
    redis-primary:6379>
    

    事务

    命令 行为
    multi 开启事务
    exec 提交事务
    discard 取消事务
    watch 用于事务开始前监听任意数量的键,如果事务过程中监听的键被其他客户端修改,则整个事务不再执行
    //第一个客户端
    redis-primary:6379> watch name
    OK
    redis-primary:6379> multi
    OK
    redis-primary:6379> set name lucy
    QUEUED
    redis-primary:6379> exec
    (nil)
    redis-primary:6379> get name
    "lucy02"
    redis-primary:6379>
    
    //第二个客户端
    redis-primary:6379> set name lucy02
    OK
    redis-primary:6379>
    

    参考

    0.redis实战
    1.http://redisdoc.com/index.html
    2.https://redisbook.readthedocs.io/en/latest/feature/transaction.html

    相关文章

      网友评论

          本文标题:redis常用命令

          本文链接:https://www.haomeiwen.com/subject/iesyyctx.html