Skip to content

Instantly share code, notes, and snippets.

@iBaozi
Last active June 27, 2021 03:36
Show Gist options
  • Save iBaozi/460fa9c5d0fd90a0bfc7 to your computer and use it in GitHub Desktop.
Save iBaozi/460fa9c5d0fd90a0bfc7 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
@I321065
Copy link

I321065 commented Apr 16, 2020

单位是什么?byte ?
是的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment