E:\soft\Redis-x64-5.0.14.1\redis-cli.exe -h 127.0.0.1 -p 6379 -a password
在REDIS控制台输入
local prefix = ARGV[1]
local keys = redis.call('KEYS', prefix .. '*')
return #keys
EVAL "local prefix = ARGV[1] local keys = redis.call('KEYS', prefix .. '*') return #keys" 0 "xxx"
local cursor = '0'
local allKeys = {}
repeat
local result = redis.call('SCAN', cursor, 'MATCH', '*', 'COUNT', '1000')
cursor = result[1]
local keys = result[2]
for _, key in ipairs(keys) do
table.insert(allKeys, key)
end
until cursor == '0'
local groups = {}
for _, key in ipairs(allKeys) do
-- 前6个字符
local prefix = string.sub(key, 1, 6)
if not groups[prefix] then
groups[prefix] = 1
else
groups[prefix] = groups[prefix] + 1
end
end
local result = {}
for prefix, count in pairs(groups) do
table.insert(result, { prefix = prefix, count = count })
end
table.sort(result, function(a, b)
return a.count > b.count
end)
local sortedResult = {}
for _, item in ipairs(result) do
table.insert(sortedResult, item.prefix .. ':' .. item.count)
end
return sortedResult
EVAL "local cursor='0';local allKeys={};repeat local result=redis.call('SCAN',cursor,'MATCH','*','COUNT','1000');cursor=result[1];local keys=result[2];for _,key in ipairs(keys) do table.insert(allKeys,key) end;until cursor=='0';local groups={};for _,key in ipairs(allKeys) do local prefix=string.sub(key,1,6);if not groups[prefix] then groups[prefix]=1 else groups[prefix]=groups[prefix]+1 end end;local result={};for prefix,count in pairs(groups) do table.insert(result,{prefix=prefix,count=count}) end;table.sort(result,function(a,b) return a.count>b.count end);local sortedResult={};for _,item in ipairs(result) do table.insert(sortedResult,item.prefix..':'..item.count) end;return sortedResult;" 0
-- 要查询的db数
local numDbs = 16
local result = {}
-- 查询db起
for db = 0, numDbs - 1 do
redis.call('SELECT', db)
local cursor = '0'
local groups = {}
repeat
local scanResult = redis.call('SCAN', cursor, 'MATCH', '*', 'COUNT', '1000')
cursor = scanResult[1]
local keys = scanResult[2]
for _, key in ipairs(keys) do
-- 前6个字符
local prefix = string.sub(key, 1, 6)
if not groups[prefix] then
groups[prefix] = { count = 1, db = db }
else
groups[prefix].count = groups[prefix].count + 1
end
end
until cursor == '0'
local tempResult = {}
-- 将每个前缀的信息存储到临时表中
for prefix, data in pairs(groups) do
table.insert(tempResult, { prefix = prefix, count = data.count, db = data.db })
end
-- 按照数量从大到小排序
table.sort(tempResult, function(a, b) return a.count > b.count end)
-- 将排序后的结果插入到最终结果中
for _, data in ipairs(tempResult) do
table.insert(result, data.prefix .. ': ' .. data.count .. ' (DB: ' .. data.db .. ')')
end
end
return result
EVAL "local numDbs=16 local result={} for db=0,numDbs-1 do redis.call('SELECT',db) local cursor='0' local groups={} repeat local scanResult=redis.call('SCAN',cursor,'MATCH','*','COUNT','1000') cursor=scanResult[1] local keys=scanResult[2] for _,key in ipairs(keys)do local prefix=string.sub(key,1,6) if not groups[prefix]then groups[prefix]={count=1,db=db}else groups[prefix].count=groups[prefix].count+1 end end until cursor=='0' local tempResult={} for prefix,data in pairs(groups)do table.insert(tempResult,{prefix=prefix,count=data.count,db=data.db})end table.sort(tempResult,function(a,b)return a.count>b.count end)for _,data in ipairs(tempResult)do table.insert(result,data.prefix..': '..data.count..' (DB: '..data.db..')')end end return result" 0