当前位置: 首页 > 科技观察

Redis-Cli这么强大,你真的了解吗?

时间:2023-03-13 03:49:50 科技观察

玩过redis的朋友应该都知道,有一个客户端小工具,叫redis-cli。在实际开发中,相信很多人只知道使用redis-cli进行REPL式的交互。其实这只是一个很小的工具。Set,例如,它可以用于在测试和开发环境中进行高效的模拟测试。接下来,我将一一展示给大家。一:Non-REPL(ReadEvalPrintLoop)模式通常我们使用的是REPL模式,即连接到端口后,以这样的循环形式发送请求并等待响应,如下图:[root@localhostDesktop]#redis-cli-h192.168.1.216192.168.1.216:6379>setusernamejackOK192.168.1.216:6379>setpassword12345OK192.168.1.216:6379>其实我也可以直接在命令行使用redis-cli加上各种附加参数,效果同上完全一样,比如下面这样:@localhostDesktop]#看看是不是有点意思~~~二:从本地文件执行命令导入的功能挺爽的。可以直接将本地文件中的一组redis命令导入到redis-cli中执行,免去逐行敲打的痛苦。对了,工作量大大减少了,请参考下面的流程。1、新建目录在/usr/下新建一个txt文件,进行两次set操作。然后使用<命令导入它。这里216的ip是局域网中的虚拟机。是不是有点像管道操作,🐂👃?~~~如下图:[root@localhostDesktop]#redis-cli-h192.168.1.216and-icommand其中-r是重复次数,-i是delaysecond的秒数,接下来我演示一下,调用info命令10次,每次延时1s,如下:[root@localhostDesktop]#redis-cli-h192.168.1.216-r10-i1INFO#Serverredis_version:3.2。4redis_git_sha1:00000000redis_git_dirty:0redis_build_id:fc9ad9a14d3a0fb5redis_mode:standaloneos:Linux3.10.0-327.el7.x86_64x86_64arch_bits:64multiplexing_api:epollgcc_version:4.8.5process_id:6171run_id:8d1d5cffbf81e31c6c6e0bd144186e9df9fea482tcp_port:6379uptime_in_seconds:3536932uptime_in_days:40hz:10lru_clock:5049094executable:/etc/redis/redis-serverconfig_file:/etc/redis/6379.conf#Clientsconnected_clients:7client_longest_output_list:0client_biggest_input_buf:0blocked_clients:0#Memoryused_memory:1295512used_memory_human:1.24Mused_memory_rss:10395648used_memory_rss_human:9.91Mused_memory_peak:35199336used_memory_peak_human:33.57Mtotal_system_memory:2099109888total_system_memory_human:1.95Gused_memory_lua:37888used_memory_lua_human:37.00Kmaxmemory:0maxmemory_human:0Bmaxmemory_policy:noevictionmem_fragmentation_ratio:8.02mem_allocator:jemalloc-4.0.3#Persistenceloading:0rdb_changes_since_last_save:0rdb_bgsave_in_progress:0rdb_last_save_time:1481443658rdb_last_bgsave_status:okrdb_last_bgsave_time_sec:0rdb_current_bgsave_time_sec:-1aof_enabled:0aof_rewrite_in_progress:0aof_rewrite_scheduled:0aof_last_rewrite_time_sec:-1aof_current_rewrite_time_sec:-1aof_last_bgrewrite_status:okaof_last_write_status:ok...这么多输出,有点眼花缭乱,大多时候我只关注used_memory_human字段,看看当前redis占用了多少内存,hereyoucanusegreptofilter:[root@localhostDesktop]#redis-cli-h192.168.1.216-r10-i1INFO|grepused_memory_humanused_memory_human:1.24Mused_memory_human:1.24Mused_memory_human:1.24Mused_memory_human:1.24Mused_memory_human:1.24Mused_memory_human:1.24Mused_memory_human:1.24Mused_memory_human:1.24Mused_memory_human:1.24Mused_memory_human:1.24M[root@localhostDesktop]#可以明显看出当前memory_human占用1.24M:sparrow监控用的一个stat--stat版本监控如果内存使用指标只有一个,肯定不能满足你的实际需求。比如你不会看到有多少个key,多少个client,多少个被blocked,多少个requests等等信息,如果这些都有,是不是有点像mongodb里面的mongostats?哈哈,迫不及待的给大家演示一下,很简单。.[root@localhostDesktop]#redis-cli-h192.168.1.216--stat------数据---------------------------加载----------------子密钥smemclientsblockedrequestsconnections271.24M701198768(+0)2206271.24M701198769(+1)2206271.24M701198770(+1)2206271.24M701198171.24)24M701198171.24M701198772(+1)2206271.24M701198773(+1)2206271.24M701198774(+1)2206271.24M701198775(+1)2206271.24M701198776(+1)2206271.24M701198777(+1)2206271.24M701198778(+1)2206271.24M701198779(+1)2206271.24M701198780(+1)2206271.27M701198782(+2)2206271.24M701198783(+1)2206271.24M701198784(+1)2206271.24M701198785(+2)2206二维码关注。转载本文,请联系一线码农,聊聊技术公众号。