首页 > 解决方案 > 如何从 Redis INFO 获取特定值

问题描述

遵循来自https://redis.io/commands/info的文档

我想从返回的特定键/值bulk string

例如:

# Rails Controller Code
redis = Redis.new(host: 'localhost', port: 6379)
render json: redis.INFO

... returns ....

# Server
redis_version:999.999.999
redis_git_sha1:3c968ff0
redis_git_dirty:0
......
......

# Memory
used_memory:167560008
used_memory_human:159.80M
used_memory_rss:174358528
used_memory_rss_human:166.28M
.....

我只想得到used_memory等等。

是否可以不使用 Ruby 调整?

MEMORY DOCTOR另外,鉴于我已经在控制器中初始化了 Redis,我该如何使用它来获取信息?(例如redis = Redis.new(host: 'localhost', port: 6379)

https://redis.io/commands/memory-doctor

提前致谢!

标签: ruby-on-railsredis

解决方案


用于Hash#slice获取相关键值对

redis = Redis.new(host: 'localhost', port: 6379)
redis.info #=> returns hash
result = redis.info.slice('used_memory_human', 'used_memory_rss')
#=>  {"used_memory_human"=>"1.10M", "used_memory_rss"=>"700416"}
render json: result

推荐阅读