常用redis-lua脚本

REDIS控制台

1. 进入控制台

E:\soft\Redis-x64-5.0.14.1\redis-cli.exe -h 127.0.0.1 -p 6379 -a password

常用redis-lua脚本

在REDIS控制台输入

1. 查询以xxx为前缀的key的数量

local prefix = ARGV[1]
local keys = redis.call('KEYS', prefix .. '*')
return #keys
对应EVAL脚本,可直接在控制台执行
EVAL "local prefix = ARGV[1] local keys = redis.call('KEYS', prefix .. '*') return #keys" 0 "xxx"

2. 用于在 Redis 中统计具有相同前缀(前n个字符)的键名的数量

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脚本,可直接在控制台执行
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

3. 用于在 Redis 中统计具有相同前缀(前n个字符)的键名的数量,可控制查询db,目前0-15

-- 要查询的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脚本,可直接在控制台执行
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

你可能感兴趣的:(工具,脚本,redis,lua)