in my case i have alot of data in redis like
User_DB_1:<user_id> , User_Main:<user_id>, Skills_DB_1:<user_id>
I set the maxmemory to 50mb and write a lua script and run it in the redis that lua file should remove data that has pattern User_DB_1:<user_id> and shouldn’t remove any data for Skills pattern
I have tried alot and I see that each time remove data matches Skills pattern
and i run it uses
redis-cli --eval /data/redis_evication.lua
and the output is (integer) 1000
I am tring to know how can i write a lua file and excute it with the expected results
local cursor = "0"
local patterns = {"User_DB_1:*", "User_Main:*"}
local total_evicted = 0
local limit = 1000 -- Limit of keys to evict per script run
repeat
-- Scan for the first pattern
local res = redis.call("SCAN", cursor, "MATCH", patterns[1], "COUNT", limit)
cursor = res[1]
local keys = res[2]
for _, key in ipairs(keys) do
if string.match(key, "^User_DB_1:%d+$") then
redis.call("DEL", key)
redis.log(redis.LOG_NOTICE, "Deleted key: " .. key)
total_evicted = total_evicted + 1
if total_evicted >= limit then
return total_evicted
end
end
end
-- Scan for the second pattern
res = redis.call("SCAN", cursor, "MATCH", patterns[2], "COUNT", limit)
cursor = res[1]
keys = res[2]
for _, key in ipairs(keys) do
if string.match(key, "^User_Main:%d+$") then
redis.call("DEL", key)
redis.log(redis.LOG_NOTICE, "Deleted key: " .. key)
total_evicted = total_evicted + 1
if total_evicted >= limit then
return total_evicted
end
end
end
until cursor == "0"
return total_evicted```