1.批量删除匹配的key

redis.log(redis.LOG_NOTICE, "call keys "..KEYS[1]);
local keys=redis.call("keys", KEYS[1]);
local count = 0;
local arr={};
if(keys and (table.maxn(keys) > 0)) then
    for index, key in ipairs(keys) do
        redis.log(redis.LOG_NOTICE, "del "..key);
        redis.call("del",key);
        count = count +1;
    end
end
return arr;
1
2
3
4
5
6
7
8
9
10
11
12

调用方法,保存为脚本执行,例如

redis-cli -a 'ebiz!2016' -n 7 --eval batchdel.lua "ebiz:stock:WH3400*"
1

2.批量修改key值

redis.log(redis.LOG_NOTICE, "call keys "..KEYS[1]);
redis.log(redis.LOG_NOTICE, "call args "..ARGV[1]..ARGV[2]);
local keys=redis.call("keys", KEYS[1]);
local count = 0;
local pat = ARGV[1];
local replace = ARGV[2];
if(keys and (table.maxn(keys) > 0)) then
    for index, key in ipairs(keys) do
        redis.log(redis.LOG_NOTICE, "rename "..key);
        local newkey = string.gsub(key,pat,replace);
        redis.call("RENAME",key,newkey);
        count = count +1;
    end
end
return count;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

调用方法,保存为脚本执行,例如

redis-cli -a 'ebiz!2016' -n 7 --eval batchRename.lua "ebiz:stock:WH3100*" , "WH3100" "310100"
1
  1. 遍历key,找出匹配property.
redis.log(redis.LOG_NOTICE, "call keys "..KEYS[1]);
local keys=redis.call("keys", KEYS[1]);
local count = 0;
local arr = "";
if(keys and (table.maxn(keys) > 0)) then
    for index, key in ipairs(keys) do
        redis.log(redis.LOG_NOTICE, "get "..key);
        local value = redis.call("get",key);
        local version = cjson.decode(value).versionRelease;
        if(version == '8.0.0' or version == '8.1.0') then
            arr = arr..cjson.decode(value).userID..",";
        end
        count = count +1;
    end
end
return arr;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16