#步骤(How to use it)
- 保存脚本到(Save script as ) redis_size.lua
- 注册脚本 (Load script)
redis-cli -h 192.168.1.1 -p 6371 script load "$(cat redis_size.lua)"
- 调用(Call it)
evalsha 78331e1d4741cad34084d357e19ce6dbca8e202e 0 test_key // 注意与上步返回的一致
local key = ARGV[1] | |
local test_dump = redis.call('dump', key) | |
if (test_dump == false) then | |
return 0 | |
end | |
local copy_key = key .. '_size_test_copy_' | |
local mem_info = redis.call('info', 'memory') | |
local before = string.sub(mem_info, string.find(mem_info, 'used_memory:') + 12, string.find(mem_info, 'used_memory_human:') - 3) | |
redis.call('restore', copy_key, 0, test_dump) | |
mem_info = redis.call('info', 'memory') | |
redis.call('del', copy_key) | |
local incr = tonumber(string.sub(mem_info, string.find(mem_info, 'used_memory:') + 12, string.find(mem_info, 'used_memory_human:') - 3)) - tonumber(before) - 16 | |
return incr |