redis-cli的使用

  • 两种交互模式:

    • 互动模式 REPL(read eval print loop),用户在其中键入命令并获得恢复

    • 命令行模式:作为redis-cli的参数执行打印标准输出,例如:redis-cli incr mycounter

  • 删除结果到文件

    [root@VM-4-16-centos data]# redis-cli incr mycounter > output.txt
    
    [root@VM-4-16-centos data]# cat output.txt 
    2
    
  • 主机、端口、数据库

    redis-cli -h 127.0.0.1 -p 6390 ping
    
  • 使用-x读取标准输入作为redis-cli的负载参数

    [root@VM-4-16-centos data]# redis-cli -x set foo < input.txt 
    OK
    [root@VM-4-16-centos data]# redis-cli get foo
    "\"input content!====\"\n"
    
  • 逐行读取redis命令

    [root@VM-4-16-centos data]# cat linecommand.txt 
    set num 100
    incr num
    [root@VM-4-16-centos data]# cat linecommand.txt | redis-cli
    OK
    (integer) 101
    
  • 双引号也可以像命令行交互的方式一样使用

    [root@VM-4-16-centos data]# cat linecommand2.txt 
    set key "123456"
    strlen key
    [root@VM-4-16-centos data]# cat linecommand2.txt | redis-cli
    OK
    (integer) 6
    
  • 连续运行相同的命令

    -r:配置执行次数; -i:配置执行间隔

    [root@VM-4-16-centos data]# redis-cli -r 5 -i 1 incr num
    (integer) 107
    (integer) 108
    (integer) 109
    (integer) 110
    (integer) 111
    
  • 持续监控RSS-RAM常驻内存,每秒一次

    [root@VM-4-16-centos data]# redis-cli -r -1 -i 1 info | grep rss_human
    used_memory_rss_human:4.43M
    used_memory_rss_human:4.43M
    used_memory_rss_human:4.43M
    used_memory_rss_human:4.43M
    
  • ‘,’分割导出到外部程序

    [root@VM-4-16-centos data]# redis-cli lpush mylist a b c d
    (integer) 4
    [root@VM-4-16-centos data]# redis-cli --csv lrange mylist 0 -1 > list.txt
    [root@VM-4-16-centos data]# cat list.txt 
    "d","c","b","a"
    
  • 运行lua脚本

    –eval将脚本执行的键列表和非键参数作为不同的数组,调用时可以指定键的数量为一个数字,但是可以不用指定,因为它使用eval分割键和参数,对应起来填充KEYS数组和ARGV数组

    [root@VM-4-16-centos redis-tmp]# cat script.lua 
    return redis.call('set',KEYS[1],ARGV[1])
    [root@VM-4-16-centos redis-tmp]# redis-cli --eval script.lua foo , bar
    OK
    
  • 连续统计模式

    [root@VM-4-16-centos redis-tmp]# redis-cli --stat
    ------- data ------ --------------------- load -------------------- - child -
    keys       mem      clients blocked requests            connections          
    10         851.35K  1       0       1704616 (+0)        129         
    10         851.35K  1       0       1704617 (+1)        129         
    10         851.35K  1       0       1704618 (+1)        129    
    
  • 扫描大键

    [root@VM-4-16-centos redis-tmp]# redis-cli --bigkeys
    
    # Scanning the entire keyspace to find biggest keys as well as
    # average sizes per key type.  You can use -i 0.1 to sleep 0.1 sec
    # per 100 SCAN commands (not usually needed).
    
    [00.00%] Biggest string found so far 'backup1' with 100 bytes
    [00.00%] Biggest list   found so far 'mylist' with 4 items
    [00.00%] Biggest string found so far 'backup4' with 130 bytes
    
    -------- summary -------
    
    Sampled 10 keys in the keyspace!
    Total key length in bytes is 53 (avg len 5.30)
    
    Biggest string found 'backup4' has 130 bytes
    Biggest   list found 'mylist' has 4 items
    
    8 strings with 453 bytes (80.00% of keys, avg size 56.62)
    2 lists with 7 items (20.00% of keys, avg size 3.50)
    0 sets with 0 members (00.00% of keys, avg size 0.00)
    0 hashs with 0 fields (00.00% of keys, avg size 0.00)
    0 zsets with 0 members (00.00% of keys, avg size 0.00)
    
  • 获取键列表

    [root@VM-4-16-centos redis-tmp]# redis-cli --scan | head -10
    backup1
    mylist
    backup4
    num
    key
    a
    foo
    backup3
    backup2
    mycounter
    

    使用模式匹配并计数

    [root@VM-4-16-centos redis-tmp]# redis-cli --scan --pattern 'back*' | wc -l
    4
    
  • 订阅发布消息

    订阅
    redis-cli psubscribe '*'
    
    发布
    redis-cli PUBLISH mychannel mymessage
    
  • 监控正在执行的命令

    打印redis实例收到的所有命令

    redis-cli monitor
    
  • 监控redis时延

    每秒发送100次ping,统计平均值

    [root@VM-4-16-centos ~]# redis-cli --latency
    min: 0, max: 41, avg: 0.45 (276 samples)
    
  • RDB文件的远程备份

    redis主从第一次复制过程中,master和slave以RDB文件的形式交换整个数据集。

    redis-cli --rdb /data/redis-tmp/dump.rdb
    
  • 从模式

    从模式是一项对redis开发人员调试操作很有用的功能。它允许检查主服务器在复制流中发送给其从服务器的内容,以便将其写入传播其副本。

    redis-cli --slave
    
  • 执行LRU模拟

    redis经常使用LRU驱逐算法,根据键的数量和为缓存分配的内存量(指定maxmemory,在redis.conf文件中配置),缓存和命中的数量将发生变化。

    测试redis的LRU实现的质量,测试给定版本怎么对部署进行设置。

    root@VM-4-16-centos bin]# redis-cli --lru-test 100000
    5250 Gets/sec | Hits: 467 (8.90%) | Misses: 4783 (91.10%)
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值