1. 创建一个 size.lua 脚本文件
Lua
local total = 0
local cursor = "0"
repeat
local reply = redis.call("SCAN", cursor, "COUNT", 1000)
cursor = reply[1]
for _, key in ipairs(reply[2]) do
total = total + redis.call("MEMORY", "USAGE", key)
end
until cursor == "0"
return string.format("Total memory: %.2f MB", total / (1024 * 1024))
2. 命令行调用
bash
# 例如,计算 db15 的内存使用情况
$redis-cli -n 15 --eval size.lua
"Total memory: 2.99 MB"