Skip to content

Commit ea3d254

Browse files
committed
perf(redis): use UNLINK instead of DEL and optimize cache eviction
- Replace DEL commands with UNLINK for better performance - Optimize cache eviction logic in FIFO, LFU, LRU, and RR put operations - Use batch operations and reduce the number of individual key deletions - Improve code readability and maintainability in cache management scripts
1 parent 7fd1a4b commit ea3d254

File tree

5 files changed

+75
-40
lines changed

5 files changed

+75
-40
lines changed

src/redis_func_cache/lua/fifo_put.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ local hmap_exists = redis.call('EXISTS', hmap_key)
2929
-- If either zset or hash doesn't exist, clean up the other one
3030
if zset_exists == 0 or hmap_exists == 0 then
3131
if zset_exists == 1 then
32-
redis.call('DEL', zset_key)
32+
redis.call('UNLINK', zset_key)
3333
end
3434
if hmap_exists == 1 then
35-
redis.call('DEL', hmap_key)
35+
redis.call('UNLINK', hmap_key)
3636
end
3737
-- Reset existence flags since we just deleted them
3838
zset_exists = 0
@@ -57,11 +57,18 @@ else
5757
-- Hash does not exist in zset
5858
if maxsize > 0 then
5959
local n = redis.call('ZCARD', zset_key) - maxsize
60-
while n >= 0 do
61-
local popped = redis.call('ZPOPMIN', zset_key) -- evict oldest
62-
redis.call('HDEL', hmap_key, popped[1])
63-
n = n - 1
64-
c = c + 1
60+
if n >= 0 then
61+
local popped_keys = {}
62+
for i = 1, n + 1 do
63+
local popped = redis.call('ZPOPMIN', zset_key) -- evict oldest
64+
if popped[1] then
65+
table.insert(popped_keys, popped[1])
66+
end
67+
end
68+
if #popped_keys > 0 then
69+
redis.call('HDEL', hmap_key, unpack(popped_keys))
70+
c = #popped_keys
71+
end
6572
end
6673
end
6774

src/redis_func_cache/lua/lfu_put.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ local hmap_exists = redis.call('EXISTS', hmap_key)
2929
-- If either zset or hash doesn't exist, clean up the other one
3030
if zset_exists == 0 or hmap_exists == 0 then
3131
if zset_exists == 1 then
32-
redis.call('DEL', zset_key)
32+
redis.call('UNLINK', zset_key)
3333
end
3434
if hmap_exists == 1 then
35-
redis.call('DEL', hmap_key)
35+
redis.call('UNLINK', hmap_key)
3636
end
3737
-- Reset existence flags since we just deleted them
3838
zset_exists = 0
@@ -57,11 +57,18 @@ else
5757
-- Hash does not exist in zset
5858
if maxsize > 0 then
5959
local n = redis.call('ZCARD', zset_key) - maxsize
60-
while n >= 0 do
61-
local popped = redis.call('ZPOPMIN', zset_key) -- evict least frequently used
62-
redis.call('HDEL', hmap_key, popped[1])
63-
n = n - 1
64-
c = c + 1
60+
if n >= 0 then
61+
local popped_keys = {}
62+
for i = 1, n + 1 do
63+
local popped = redis.call('ZPOPMIN', zset_key) -- evict least frequently used
64+
if popped[1] then
65+
table.insert(popped_keys, popped[1])
66+
end
67+
end
68+
if #popped_keys > 0 then
69+
redis.call('HDEL', hmap_key, unpack(popped_keys))
70+
c = #popped_keys
71+
end
6572
end
6673
end
6774
redis.call('ZADD', zset_key, 1, hash) -- initialize frequency to 1

src/redis_func_cache/lua/lru_put.lua

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ local hmap_exists = redis.call('EXISTS', hmap_key)
3535
-- If either zset or hash doesn't exist, clean up the other one
3636
if zset_exists == 0 or hmap_exists == 0 then
3737
if zset_exists == 1 then
38-
redis.call('DEL', zset_key)
38+
redis.call('UNLINK', zset_key)
3939
end
4040
if hmap_exists == 1 then
41-
redis.call('DEL', hmap_key)
41+
redis.call('UNLINK', hmap_key)
4242
end
4343
-- Reset existence flags since we just deleted them
4444
zset_exists = 0
@@ -63,16 +63,23 @@ else
6363
-- Hash does not exist in zset
6464
if maxsize > 0 then
6565
local n = redis.call('ZCARD', zset_key) - maxsize
66-
while n >= 0 do
67-
local popped
66+
if n >= 0 then
67+
local count
6868
if is_mru then
69-
popped = redis.call('ZPOPMAX', zset_key) -- MRU eviction
69+
-- MRU eviction: remove highest scores (most recently used)
70+
count = redis.call('ZREMRANGEBYSCORE', zset_key, '+inf', '-inf', 'LIMIT', 0, n + 1)
7071
else
71-
popped = redis.call('ZPOPMIN', zset_key) -- LRU eviction
72+
-- LRU eviction: remove lowest scores (least recently used)
73+
count = redis.call('ZREMRANGEBYRANK', zset_key, 0, n)
74+
end
75+
if count > 0 then
76+
-- Get the evicted keys
77+
local evicted_keys = redis.call('ZRANGE', zset_key, 0, count - 1)
78+
if #evicted_keys > 0 then
79+
redis.call('HDEL', hmap_key, unpack(evicted_keys))
80+
end
81+
c = count
7282
end
73-
redis.call('HDEL', hmap_key, popped[1])
74-
n = n - 1
75-
c = c + 1
7683
end
7784
end
7885

src/redis_func_cache/lua/lru_t_put.lua

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ local hmap_exists = redis.call('EXISTS', hmap_key)
3434

3535
-- If either zset or hash doesn't exist, clean up the other one
3636
if zset_exists == 0 or hmap_exists == 0 then
37-
if zset_exists == 1 then
38-
redis.call('DEL', zset_key)
37+
if zset_exists == 1 then
38+
redis.call('UNLINK', zset_key)
3939
end
4040
if hmap_exists == 1 then
41-
redis.call('DEL', hmap_key)
41+
redis.call('UNLINK', hmap_key)
4242
end
4343
-- Reset existence flags since we just deleted them
4444
zset_exists = 0
@@ -66,16 +66,23 @@ else
6666
-- Hash does not exist in zset
6767
if maxsize > 0 then
6868
local n = redis.call('ZCARD', zset_key) - maxsize
69-
while n >= 0 do
70-
local popped
69+
if n >= 0 then
70+
local count = n + 1
71+
local popped_keys
7172
if is_mru then
72-
popped = redis.call('ZPOPMAX', zset_key) -- MRU eviction
73+
popped_keys = redis.call('ZPOPMAX', zset_key, count) -- MRU eviction with batch pop
7374
else
74-
popped = redis.call('ZPOPMIN', zset_key) -- LRU eviction
75+
popped_keys = redis.call('ZPOPMIN', zset_key, count) -- LRU eviction with batch pop
76+
end
77+
78+
if #popped_keys > 0 then
79+
local keys = {}
80+
for i = 1, #popped_keys, 2 do
81+
table.insert(keys, popped_keys[i])
82+
end
83+
redis.call('HDEL', hmap_key, unpack(keys))
84+
c = #keys
7585
end
76-
redis.call('HDEL', hmap_key, popped[1])
77-
n = n - 1
78-
c = c + 1
7986
end
8087
end
8188
local time = redis.call('TIME')

src/redis_func_cache/lua/rr_put.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ local hmap_exists = redis.call('EXISTS', hmap_key)
2929
-- If either set or hash doesn't exist, clean up the other one
3030
if set_exists == 0 or hmap_exists == 0 then
3131
if set_exists == 1 then
32-
redis.call('DEL', set_key)
32+
redis.call('UNLINK', set_key)
3333
end
3434
if hmap_exists == 1 then
35-
redis.call('DEL', hmap_key)
35+
redis.call('UNLINK', hmap_key)
3636
end
3737
-- Reset existence flags since we just deleted them
3838
set_exists = 0
@@ -57,11 +57,18 @@ else
5757
-- Hash does not exist in set
5858
if maxsize > 0 then
5959
local n = redis.call('SCARD', set_key) - maxsize
60-
while n >= 0 do
61-
local popped = redis.call('SPOP', set_key) -- evict random member
62-
redis.call('HDEL', hmap_key, popped)
63-
n = n - 1
64-
c = c + 1
60+
if n >= 0 then
61+
local popped_keys = {}
62+
for i = 1, n + 1 do
63+
local popped = redis.call('SPOP', set_key) -- evict random member
64+
if popped then
65+
table.insert(popped_keys, popped)
66+
end
67+
end
68+
if #popped_keys > 0 then
69+
redis.call('HDEL', hmap_key, unpack(popped_keys))
70+
c = #popped_keys
71+
end
6572
end
6673
end
6774
redis.call('SADD', set_key, hash)

0 commit comments

Comments
 (0)