影落离风

The shadow falls away from the wind

0%

常用redis-lua脚本

REDIS控制台

1. 进入控制台

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的数量

1
2
3
local prefix = ARGV[1]
local keys = redis.call('KEYS', prefix .. '*')
return #keys

对应EVAL脚本,可直接在控制台执行

1
EVAL "local prefix = ARGV[1] local keys = redis.call('KEYS', prefix .. '*') return #keys" 0 "xxx"

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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脚本,可直接在控制台执行

1
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- 要查询的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脚本,可直接在控制台执行

1
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

4.EVAL脚本用于删除以test为前缀的key

1
EVAL "local keys = redis.call('KEYS', ARGV[1]) for i, key in ipairs(keys) do redis.call('DEL', key) end return keys" 0 "test*"

5.EVAL脚本用于删除以test为前缀的key,在0-15db中

1
EVAL "for db=0, 15 do redis.call('SELECT', db) for _, key in ipairs(redis.call('KEYS', ARGV[1])) do redis.call('DEL', key) end end" 0 "rta*"

6.EVAL脚本用于删除过期时间为 -1 dekey

确认要删除的数据无误或已备份,并谨慎使用

1
EVAL "local keys = redis.call('KEYS', '*')\nfor i, key in ipairs(keys) do\n    local ttl = redis.call('TTL', key)\n    if ttl == -1 then\n        redis.call('DEL', key)\n    end\nend\nreturn 'Deleted expired keys'" 0