美文网首页
02-Redis 常用命令

02-Redis 常用命令

作者: 过桥 | 来源:发表于2021-04-09 15:52 被阅读0次

    1、启动

    [root@localhost /]# cd /usr/local/bin
    [root@localhost bin]# ls
    dump.rdb  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli  redis-config  redis-sentinel  redis-server
    [root@localhost bin]# redis-server redis-config/redis.conf
    [root@localhost bin]# ps -ef|grep redis
    root       9427      1  0 10:46 ?        00:00:00 redis-server 127.0.0.1:6379
    root       9433   7788  0 10:46 pts/0    00:00:00 grep --color=auto redis
    [root@localhost bin]# redis-cli -p 6379
    127.0.0.1:6379> ping
    PONG
    127.0.0.1:6379> 
    

    2、常用类型及命令

    string

    参考场景:各类热点数据(用户信息),限时业务(手机验证码,24小时优惠,订单未付款自动失效),计数器业务(秒杀,唯一序号自增,限制访问量)

    常用关键字:setgetsetnxkeys *msetmgetmsetnxexistsmoveexpirettltypeappendstrlenincrdecrincrbydecrbygetrangesetrangegetset

    127.0.0.1:6379> set name coin   # 存
    OK
    127.0.0.1:6379> get n           # 取不存在的key
    (nil)
    127.0.0.1:6379> get name        # 取
    "coin"
    
    127.0.0.1:6379> setnx name coin # 分布式锁中常用,不存在才设置,set if not exist
    (integer) 1
    127.0.0.1:6379> setnx name hello
    (integer) 0
    127.0.0.1:6379> get name
    "coin"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> flushdb
    OK
    127.0.0.1:6379> keys *
    (empty array)
    127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3  # 批量设置
    OK
    127.0.0.1:6379> mget k1 k2 k3           # 批量获取
    1) "v1"
    2) "v2"
    3) "v3"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> msetnx k1 new1 k4 v4    # 如果存在,直接失败,k1 覆盖失败,同时 k4 也未写入,这是一个原子性操作,要么一起成功,要么一起失败
    (integer) 0
    127.0.0.1:6379> mget k1 k2 k3 k4
    1) "v1"
    2) "v2"
    3) "v3"
    4) (nil)
    127.0.0.1:6379> msetnx k4 v4 
    (integer) 1
    127.0.0.1:6379> mget k1 k2 k3 k4
    1) "v1"
    2) "v2"
    3) "v3"
    4) "v4"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> keys *              # 查看所有key
    1) "name"
    
    127.0.0.1:6379> select 1            # 切换数据库
    OK
    127.0.0.1:6379[1]> set id 123
    OK
    
    
    127.0.0.1:6379[1]> flushdb          # 清空当前数据库
    OK
    127.0.0.1:6379[1]> keys *
    (empty array)
    127.0.0.1:6379[1]> flushall         # 清空所有数据库
    OK
    127.0.0.1:6379[1]> select 0
    OK
    127.0.0.1:6379> keys *
    (empty array)
    
    
    127.0.0.1:6379> set name coin
    OK
    127.0.0.1:6379> exists name         # 检查是否存在key
    (integer) 1
    127.0.0.1:6379> exists name1
    (integer) 0
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> move name 1         # 移除 key
    (integer) 1
    127.0.0.1:6379> exists name
    (integer) 0
    
    
    127.0.0.1:6379> set name coin
    OK
    127.0.0.1:6379> expire name 10      # 设置10秒过期方法一
    (integer) 1
    127.0.0.1:6379> keys *
    1) "name"
    127.0.0.1:6379> ttl name            # 查看剩余时间
    (integer) 7
    127.0.0.1:6379> ttl name
    (integer) 3
    127.0.0.1:6379> ttl name
    (integer) 0
    127.0.0.1:6379> ttl name
    (integer) -2
    127.0.0.1:6379> keys *              # 等待 10秒 再查询
    (empty array)
    127.0.0.1:6379> 
    
    127.0.0.1:6379> setex name 10 coin  # 设置10秒过期方法二,set with expire
    OK
    127.0.0.1:6379> ttl name
    (integer) 5
    127.0.0.1:6379> get name
    "coin"
    127.0.0.1:6379> get name
    (nil)
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> keys * 
    (empty array)
    127.0.0.1:6379> set name coin
    OK
    127.0.0.1:6379> set age 17
    OK
    127.0.0.1:6379> type name       # 查看key类型
    string
    127.0.0.1:6379> type age
    string
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> set key1 coin
    OK
    127.0.0.1:6379> append key1 hey     # 追加
    (integer) 7
    127.0.0.1:6379> get key1
    "coinhey"
    127.0.0.1:6379> strlen key1         # 查看长度
    (integer) 7
    127.0.0.1:6379> append key2 hello   # 追加如果不存在,等同于新建
    (integer) 5
    127.0.0.1:6379> get key2
    "hello"
    
    
    127.0.0.1:6379> set views 0     # 自增
    OK
    127.0.0.1:6379> get views
    "0"
    127.0.0.1:6379> incr views      # 加一
    (integer) 1
    127.0.0.1:6379> incr views
    (integer) 2
    127.0.0.1:6379> decr views      # 减一
    (integer) 1
    127.0.0.1:6379> incrby views 5  # 加特定步长
    (integer) 6
    127.0.0.1:6379> decrby views 5  # 减特定步长,也可以 incrby views -5
    (integer) 1
    
    
    127.0.0.1:6379> set name coin
    OK
    127.0.0.1:6379> get name
    "coin"
    127.0.0.1:6379> getrange name 1 2   # 字符串截取
    "oi"
    127.0.0.1:6379> getrange name 0 -1  # 所有字符
    "coin"
    
    
    127.0.0.1:6379> set name coin
    OK
    127.0.0.1:6379> setrange name 1 X   # 从 x 位开始替换字符
    (integer) 4
    127.0.0.1:6379> get name
    "cXin"
    
    
    127.0.0.1:6379> set user:1 {name:coin,age:17}   # 设置对象方式一
    OK
    127.0.0.1:6379> get user:1
    "{name:coin,age:17}"
    127.0.0.1:6379> mset user:1:name coin user:1:age 17 # 设置对象方式二【推荐】,便于程序使用动态变量,或对值直接处理,例如值自增,补充说明,设置
    OK
    127.0.0.1:6379> mget user:1:name user:1:age
    1) "coin"
    2) "17"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> get name
    (nil)
    127.0.0.1:6379> getset name coin    # 如果不存在,返回nil,如果存在,返回当前值,再赋值
    (nil)
    127.0.0.1:6379> get name
    "coin"
    127.0.0.1:6379> getset name hellocoin
    "coin"
    127.0.0.1:6379> get name
    "hellocoin"
    127.0.0.1:6379> 
    
    

    list

    特点:可重复,可设置读写规则

    参考场景:lpush + rpop,消息队列;lpush + lpop,栈

    常用关键字:lpushlrangerpushlpoprpoplindexllenlremltrimrpoplpushexistslsetlinsert

    127.0.0.1:6379> flushdb
    OK
    127.0.0.1:6379> lpush xList one     # 插入列表左侧,【one】
    (integer) 1
    127.0.0.1:6379> lpush xList two     #【two,one】
    (integer) 2
    127.0.0.1:6379> lpush xList three   #【three,two,one】
    (integer) 3
    127.0.0.1:6379> lrange xList 0 -1   # 命令不区分大小写,但 key 区分【大小写敏感】
    (empty array)
    127.0.0.1:6379> lrange xList 0 -1
    1) "three"
    2) "two"
    3) "one"
    
    
    127.0.0.1:6379> lrange xList 0 1    
    1) "three"
    2) "two"
    
    
    127.0.0.1:6379> rpush xList four    # 插入列表右侧,【three,two,one,four】
    (integer) 4
    127.0.0.1:6379> lrange xList 0 -1
    1) "three"
    2) "two"
    3) "one"
    4) "four"
    
    
    127.0.0.1:6379> lpop xList      # 左侧第一个移除,返回被移除值
    "three"
    127.0.0.1:6379> lrange xList 0 -1
    1) "two"
    2) "one"
    3) "four"
    
    
    127.0.0.1:6379> rpop xList      # 右侧第一个移除,返回被移除值
    "four"
    127.0.0.1:6379> lrange xList 0 -1
    1) "two"
    2) "one"
    
    
    127.0.0.1:6379> lindex xList 0      # 通过索引获取值
    "two"
    127.0.0.1:6379> lindex xList 1
    "one"
    127.0.0.1:6379> llen xList          # 获取list长度
    (integer) 2
    
    
    127.0.0.1:6379> lrange xList 0 -1
    1) "two"
    2) "one"
    127.0.0.1:6379> lpush xList two
    (integer) 3
    127.0.0.1:6379> lrange xList 0 -1
    1) "two"
    2) "two"
    3) "one"
    127.0.0.1:6379> lrem xList 1 one    # 移除制定值,移除一个 one
    (integer) 1
    127.0.0.1:6379> lrange xList 0 -1
    1) "two"
    2) "two"
    127.0.0.1:6379> lrem xList 2 two    # 移除制定值,移除两个 two
    (integer) 2
    127.0.0.1:6379> lrange xList 0 -1
    (empty array)
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> lrange xList 0 -1
    (empty array)
    127.0.0.1:6379> lpush xList 1
    (integer) 1
    127.0.0.1:6379> lpush xList 2
    (integer) 2
    127.0.0.1:6379> lpush xList 1
    (integer) 3
    127.0.0.1:6379> lpush xList 3
    (integer) 4
    127.0.0.1:6379> lrange xList 0 -1   # 插入无序重复值
    1) "3"
    2) "1"
    3) "2"
    4) "1"
    127.0.0.1:6379> lrem xList 1 1      # 只移除一个重复值,从左往右检查是否需要移除
    (integer) 1
    127.0.0.1:6379> lrange xList 0 -1
    1) "3"
    2) "2"
    3) "1"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> flushdb
    OK
    127.0.0.1:6379> lpush xList 1
    (integer) 1
    127.0.0.1:6379> lpush xList 2
    (integer) 2
    127.0.0.1:6379> lpush xList 3
    (integer) 3
    127.0.0.1:6379> lpush xList 4
    (integer) 4
    127.0.0.1:6379> lpush xList 5
    (integer) 5
    127.0.0.1:6379> lrange xList 0 -1
    1) "5"
    2) "4"
    3) "3"
    4) "2"
    5) "1"
    127.0.0.1:6379> ltrim xList 1 3     # 根据索引,截取,从 1 开始,截取 3 位
    OK
    127.0.0.1:6379> lrange xList 0 -1
    1) "4"
    2) "3"
    3) "2"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> lrange xList 0 -1
    1) "4"
    2) "3"
    3) "2"
    127.0.0.1:6379> rpoplpush xList yList   # 移除列表最后一个元素至新列表中
    "2"
    127.0.0.1:6379> lrange xList 0 -1
    1) "4"
    2) "3"
    127.0.0.1:6379> lrange yList 0 -1
    1) "2"
    127.0.0.1:6379> rpoplpush xList yList
    "3"
    127.0.0.1:6379> lrange xList 0 -1
    1) "4"
    127.0.0.1:6379> lrange yList 0 -1
    1) "3"
    2) "2"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> exists zList            # 判断列表是否存在
    (integer) 0
    127.0.0.1:6379> lset zList 0 coin       # 尝试修改索引为0的值,因不存在,无法修改
    (error) ERR no such key
    127.0.0.1:6379> lpush zList coin
    (integer) 1
    127.0.0.1:6379> lrange zList 0 -1
    1) "coin"
    127.0.0.1:6379> lset zList 0 hello      # 修改索引为0的值
    OK
    127.0.0.1:6379> lrange zList 0 -1
    1) "hello"
    127.0.0.1:6379> lset zList 1 world
    (error) ERR index out of range
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> flushdb
    OK
    127.0.0.1:6379> rpush xList 1
    (integer) 1
    127.0.0.1:6379> rpush xList 2
    (integer) 2
    127.0.0.1:6379> rpush xList 3
    (integer) 3
    127.0.0.1:6379> linsert xList before 2 before2      # 指定值前面插入
    (integer) 4
    127.0.0.1:6379> linsert xList after 2 after2        # 指定值后面插入
    (integer) 5
    127.0.0.1:6379> lrange xList 0 -1
    1) "1"
    2) "before2"
    3) "2"
    4) "after2"
    5) "3"
    127.0.0.1:6379> 
    

    set

    特点:无序、不重复

    参考场景:单点登录cookies,随机抽奖,点赞,共同好友

    常用关键字:saddsmemberssismemberscardsremsrandmemberspopsmovesdiffsintersunion

    127.0.0.1:6379> sadd xSet hello     # set 中添加数据
    (integer) 1
    127.0.0.1:6379> sadd xSet hi
    (integer) 1
    127.0.0.1:6379> sadd xSet hey
    (integer) 1
    127.0.0.1:6379> smembers xSet       # 获取 set 中所有值
    1) "hello"
    2) "hi"
    3) "hey"
    127.0.0.1:6379> sadd xSet hey       # set 中数据不可重复
    (integer) 0
    127.0.0.1:6379> smembers xSet
    1) "hello"
    2) "hi"
    3) "hey"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> sismember xSet hi   # 判断值在 set 中是否存在
    (integer) 1
    127.0.0.1:6379> sismember xSet hihi
    (integer) 0
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> scard xSet      # 获取 set 中元素个数
    (integer) 3
    
    
    127.0.0.1:6379> srem xSet hi    # 移除指定值
    (integer) 1
    127.0.0.1:6379> scard xSet
    (integer) 2
    127.0.0.1:6379> smembers xSet
    1) "hello"
    2) "hey"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> srandmember xSet    # 随机返回值
    "hey"
    127.0.0.1:6379> srandmember xSet
    "hello"
    127.0.0.1:6379> srandmember xSet
    "hello"
    
    
    127.0.0.1:6379> smembers xSet
    1) "hello"
    2) "hey"
    127.0.0.1:6379> sadd xSet hi
    (integer) 1
    127.0.0.1:6379> sadd xSet 1
    (integer) 1
    127.0.0.1:6379> sadd xSet 2
    (integer) 1
    127.0.0.1:6379> sadd xSet 3
    (integer) 1
    127.0.0.1:6379> smembers xSet
    1) "hi"
    2) "1"
    3) "hey"
    4) "3"
    5) "2"
    6) "hello"
    127.0.0.1:6379> spop xSet       # 随机删一个值
    "hey"
    127.0.0.1:6379> smembers xSet
    1) "3"
    2) "2"
    3) "hello"
    4) "hi"
    5) "1"
    127.0.0.1:6379> spop xSet
    "hello"
    127.0.0.1:6379> smembers xSet
    1) "3"
    2) "2"
    3) "hi"
    4) "1"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> smove xSet ySet hi  # 移动指定值去另一个set
    (integer) 1
    127.0.0.1:6379> smembers xSet
    1) "3"
    2) "2"
    3) "1"
    127.0.0.1:6379> smembers ySet
    1) "hi"
    
    
    127.0.0.1:6379> sadd ySet 1
    (integer) 1
    127.0.0.1:6379> smembers ySet
    1) "hi"
    2) "1"
    127.0.0.1:6379> smove xSet ySet 1   # 移动值重复,依旧提示成功,原set -1,新set 因不允许重复特性,不变动
    (integer) 1
    127.0.0.1:6379> smembers xSet
    1) "3"
    2) "2"
    127.0.0.1:6379> smembers ySet
    1) "hi"
    2) "1"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> sadd xSet 1
    (integer) 1
    127.0.0.1:6379> smembers xSet       # 检查xSet 值
    1) "3"
    2) "2"
    3) "1"
    127.0.0.1:6379> smembers ySet       # 检查ySet 值
    1) "hi"
    2) "1"
    127.0.0.1:6379> sdiff xSet ySet     # 检查 xSet 中不在 ySet 的值,差集
    1) "2"
    2) "3"
    127.0.0.1:6379> sdiff ySet xSet     # 检查 ySet 中不在 xSet 的值,差集
    1) "hi"
    127.0.0.1:6379> sinter xSet ySet    # 检查 xSet、ySet 中均包含的值,交集,【谁先谁后无所谓】
    1) "1"
    127.0.0.1:6379> sinter ySet xSet
    1) "1"
    127.0.0.1:6379> sunion xSet ySet    # 检查 xSet、ySet 所有值,并集,【谁先谁后无所谓】
    1) "3"
    2) "hi"
    3) "2"
    4) "1"
    127.0.0.1:6379> 
    

    hash

    特点:key-<key,value>,本质和string类似

    参考场景:存储对象数据,可模拟关系型数据库表结构

    常用关键字:hsethgethmsethmgethgetallhdelhlenhexistshkeyshvalshincrbyhsetnx

    127.0.0.1:6379> hset xhash name coin    # set 一个 值
    (integer) 1
    127.0.0.1:6379> hget xhash name         # 获取一个值
    "coin"
    127.0.0.1:6379> hmset xhash name yoga age 17    # 批量set
    OK
    127.0.0.1:6379> hmget xhash name age    # 批量获取
    1) "yoga"
    2) "17"
    127.0.0.1:6379> hgetall xhash           # 获取所有
    1) "name"
    2) "yoga"
    3) "age"
    4) "17"
    
    
    127.0.0.1:6379> hdel xhash age      # 删除指定属性、值
    (integer) 1
    127.0.0.1:6379> hgetall xhash
    1) "name"
    2) "yoga"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> hlen xhash          # 查看指定哈希里属性长度
    (integer) 1
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> hexists xhash name  # 判断指定哈希中属性是否存在
    (integer) 1
    127.0.0.1:6379> hexists xhash age
    (integer) 0
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> hkeys xhash     # 获取指定哈希中所有属性
    1) "name"
    127.0.0.1:6379> hvals xhash     # 获取指定哈希中所有值
    1) "yoga"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> hset xhash age 17
    (integer) 1
    127.0.0.1:6379> hgetall xhash
    1) "name"
    2) "yoga"
    3) "age"
    4) "17"
    127.0.0.1:6379> hincrby xhash age 5     # 增加
    (integer) 22
    127.0.0.1:6379> hgetall xhash
    1) "name"
    2) "yoga"
    3) "age"
    4) "22"
    127.0.0.1:6379> hincrby xhash age -5    # 减少,没有 hdecrby
    (integer) 17
    127.0.0.1:6379> hgetall xhash
    1) "name"
    2) "yoga"
    3) "age"
    4) "17"
    
    
    127.0.0.1:6379> hsetnx xhash age 110    # 存在,不修改
    (integer) 0
    127.0.0.1:6379> hsetnx xhash life 110   # 不存在,添加
    (integer) 1
    127.0.0.1:6379> hgetall xhash
    1) "name"
    2) "yoga"
    3) "age"
    4) "17"
    5) "life"
    6) "110"
    127.0.0.1:6379> 
    
    

    zset

    有序的set

    参考场景:快排(热点榜单排序)

    常用关键字:zaddzrangezrangebyscorezrevrangezrevrangebylexzcardzcountzremzincrby

    127.0.0.1:6379> zadd xSet 1 one     # 添加一个值
    (integer) 1
    127.0.0.1:6379> zadd xSet 2 two
    (integer) 1
    127.0.0.1:6379> zadd xSet 3 three 4 four    # 添加多个值
    (integer) 2
    127.0.0.1:6379> zrange xSet 0 -1
    1) "one"
    2) "two"
    3) "three"
    4) "four"
    127.0.0.1:6379> zadd xSet 0 zero
    (integer) 1
    127.0.0.1:6379> zrangebyscore xSet -inf +inf    # 排序,负无穷到正无穷
    1) "zero"
    2) "one"
    3) "two"
    4) "three"
    5) "four"
    127.0.0.1:6379> zrangebyscore xSet -inf +inf withscores # 排序,带上值,升序
     1) "zero"
     2) "0"
     3) "one"
     4) "1"
     5) "two"
     6) "2"
     7) "three"
     8) "3"
     9) "four"
    10) "4"
    127.0.0.1:6379> zrevrangebylex xSet + -     # 降序
    1) "four"
    2) "three"
    3) "two"
    4) "one"
    5) "zero"
    127.0.0.1:6379> zrevrange xSet 0 -1     # 降序
    1) "four"
    2) "three"
    3) "two"
    4) "zero"
    
    
    127.0.0.1:6379> zcard xSet              # 获取个数
    (integer) 4
    
    
    127.0.0.1:6379> zcount xSet -inf +inf   # 查找个数
    (integer) 4
    127.0.0.1:6379> zcount xSet 2 3         # 查找指定范围内个数
    (integer) 2
    
    
    127.0.0.1:6379> zrem xSet three zero    # 移除
    (integer) 2
    127.0.0.1:6379> zrange xSet 0 -1
    1) "two"
    2) "four"
    
    
    127.0.0.1:6379> zadd xSet 200 22222     # 添加
    (integer) 1
    127.0.0.1:6379> zrange xSet 0 -1
    1) "two"
    2) "four"
    3) "22222"
    127.0.0.1:6379> zrange xSet 0 -1 withscores
    1) "two"
    2) "2"
    3) "four"
    4) "4"
    5) "22222"
    6) "200"
    127.0.0.1:6379> zadd xSet 10 two        # 修改(覆盖)
    (integer) 0
    127.0.0.1:6379> zrange xSet 0 -1 withscores
    1) "four"
    2) "4"
    3) "two"
    4) "10"
    5) "22222"
    6) "200"
    127.0.0.1:6379> zincrby xSet 10 two     # 修改(累加)
    "20"
    127.0.0.1:6379> zrange xSet 0 -1 withscores
    1) "four"
    2) "4"
    3) "two"
    4) "20"
    5) "22222"
    6) "200"
    127.0.0.1:6379> zincrby xSet -10 two
    "10"
    127.0.0.1:6379> zrange xSet 0 -1 withscores
    1) "four"
    2) "4"
    3) "two"
    4) "10"
    5) "22222"
    6) "200"
    127.0.0.1:6379> 
    

    geospatial

    特殊的zset

    参考场景:涉及空间数据相关操作(附近的人,两地间距离)

    常用关键字:geoaddgeoposgeodistgeoradiusgeoradiusbymemberzrange

    127.0.0.1:6379> clear
    127.0.0.1:6379> geoadd city 116.413384 39.910925 beijing 117.209523 39.093668 tianjin 114.521532 38.048312 shijiazhuang                 # 添加坐标,geoadd 集合名 经度 纬度 值 经度 纬度 值
    (integer) 3
    127.0.0.1:6379> geopos city beijing     # 获取坐标
    1) 1) "116.41338318586349487"
       2) "39.9109247398676743"
    127.0.0.1:6379> geopos city beijing tianjin
    1) 1) "116.41338318586349487"
       2) "39.9109247398676743"
    2) 1) "117.2095218300819397"
       2) "39.09366727006780451"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> geodist city beijing tianjin    # 获取两点间直线距离,默认单位 m,可设置 m(密) km(千米)  mi(英里)  ft(英尺)
    "113715.0724"
    127.0.0.1:6379> geodist city beijing tianjin km # 返回距离单位设置为千米
    "113.7151"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> georadius city 117 39 100 km    # 查找指定中心点指定半径内数据,georadius 集合名 经度 纬度 半径 单位
    1) "tianjin"
    127.0.0.1:6379> georadius city 117 39 200 km withdist   # 带上直线距离
    1) 1) "tianjin"
       2) "20.8832"
    2) 1) "beijing"
       2) "113.1518"
    127.0.0.1:6379> georadius city 117 39 200 km withcoord  # 带上坐标
    1) 1) "tianjin"
       2) 1) "117.2095218300819397"
          2) "39.09366727006780451"
    2) 1) "beijing"
       2) 1) "116.41338318586349487"
          2) "39.9109247398676743"
    127.0.0.1:6379> georadius city 117 39 200 km withcoord withdist withhash
    1) 1) "tianjin"
       2) "20.8832"
       3) (integer) 4069186120131577
       4) 1) "117.2095218300819397"
          2) "39.09366727006780451"
    2) 1) "beijing"
       2) "113.1518"
       3) (integer) 4069885642983710
       4) 1) "116.41338318586349487"
          2) "39.9109247398676743"
    127.0.0.1:6379> georadius city 117 39 200 km withcoord withdist count 1 # 限制返回个数
    1) 1) "tianjin"
       2) "20.8832"
       3) 1) "117.2095218300819397"
          2) "39.09366727006780451"
    
    
    127.0.0.1:6379> georadiusbymember city beijing 200 km   # 根据值找指定半径内数据
    1) "beijing"
    2) "tianjin"
    127.0.0.1:6379> georadiusbymember city beijing 500 km
    1) "shijiazhuang"
    2) "tianjin"
    3) "beijing"
    127.0.0.1:6379> 
    
    
    127.0.0.1:6379> zrange city 0 -1        # 获取所有数据,geo底层使用zset,所以可以使用zset相关命令操作geo
    1) "shijiazhuang"
    2) "tianjin"
    3) "beijing"
    127.0.0.1:6379> zrem city shijiazhuang  # 移除特定数据
    (integer) 1
    127.0.0.1:6379> zrange city 0 -1
    1) "tianjin"
    2) "beijing"
    127.0.0.1:6379> 
    

    hyperloglog

    用来做基数统计的算法,优点在输入元素的数量或体积非常大时,计算基数所需的空间总是固定的、且很小。

    缺点在只会根据输入元素来计算基数,而不会储存输入元素本身,不能像集合那样,返回输入的各个元素,大数据量存在0.81%错误率,即具备自带数据脱敏。

    参考场景:计算访客价值(UV)、访问量价值(PV

    常用关键字:pfaddpfcountpfmerge

    127.0.0.1:6379> pfadd uv:001 A B C D    # 页面一,访问情况
    (integer) 1
    127.0.0.1:6379> pfadd uv:002 C D E F G  # 页面二,访问情况
    (integer) 1
    127.0.0.1:6379> pfcount uv:001          # 统计个数
    (integer) 4
    127.0.0.1:6379> pfcount uv:002
    (integer) 5
    127.0.0.1:6379> pfcount uv:all
    (integer) 0
    127.0.0.1:6379> pfmerge uv:all uv:001 uv:002    # 合并,去重取并集,所以实际只有 A - G
    OK
    127.0.0.1:6379> pfcount uv:all
    (integer) 7
    127.0.0.1:6379> pfadd uv:001 C D E      # 页面一,增加 C、D、E访问,重复不累计,所以实际只有 A - E
    (integer) 1
    127.0.0.1:6379> pfcount uv:001
    (integer) 5
    127.0.0.1:6379> pfcount uv:all          # 之前合并过的,不受影响
    (integer) 7
    127.0.0.1:6379> 
    

    bitmap

    用来做布尔数据统计,优点,操作二进制进行记录,只有 01 两种状态,节省内存空间

    缺点,不会储存输入元素本身

    参考场景:是否活跃 / 在线,是否打卡

    常用关键字:setbitgetbitbitcount

    127.0.0.1:6379> setbit isonline 1 1     # 设置 1号用户 在线,语法,setbit 变量名 数字 1或0
    (integer) 0
    127.0.0.1:6379> setbit isonline 2 1
    (integer) 0
    127.0.0.1:6379> setbit isonline 1000 1
    (integer) 0
    127.0.0.1:6379> getbit isonline 1000    # 取出 1000号用户 状态
    (integer) 1
    127.0.0.1:6379> getbit isonline 999     # 取出不存在用户,状态默认 0 
    (integer) 0
    127.0.0.1:6379> bitcount isonline       # 统计在线人数
    (integer) 3
    127.0.0.1:6379> setbit isonline 1 0     # 修改 1号用户 状态
    (integer) 1
    127.0.0.1:6379> bitcount isonline
    (integer) 2
    127.0.0.1:6379> 
    

    3、异常

    127.0.0.1:6379[1]> set id 123
    (error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
    
    [root@localhost redis-config]# vim redis.conf
    
    # stop-writes-on-bgsave-error yes
    stop-writes-on-bgsave-error no
    
    # 重启服务
    

    相关文章

      网友评论

          本文标题:02-Redis 常用命令

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