Skip to content

Instantly share code, notes, and snippets.

@zhongql
Forked from iBaozi/ReadMe.md
Created May 22, 2017 10:02
Show Gist options
  • Save zhongql/d28710863fe20d28b7323e771e1f728a to your computer and use it in GitHub Desktop.
Save zhongql/d28710863fe20d28b7323e771e1f728a to your computer and use it in GitHub Desktop.
redis 查看指定key 占用大小。用复制,查看增量方法,注意不要用于过大的key

#步骤(How to use it)

  1. 保存脚本到(Save script as ) redis_size.lua
  2. 注册脚本 (Load script)
    redis-cli -h 192.168.1.1 -p 6371 script load "$(cat redis_size.lua)"
  3. 调用(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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment