Skip to content

Commit 9d2d04b

Browse files
committed
perf(lua): optimize cache eviction logic in fifo_put, lfu_put, and rr_put
- Replace looping calls with batch ZPOPMIN in fifo_put and lfu_put - Use batch SPOP in rr_put - Improve performance by reducing the number of Redis calls
1 parent ea3d254 commit 9d2d04b

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

src/redis_func_cache/lua/fifo_put.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ else
5858
if maxsize > 0 then
5959
local n = redis.call('ZCARD', zset_key) - maxsize
6060
if n >= 0 then
61+
-- Use batch ZPOPMIN instead of looping calls
62+
local popped_keys_data = redis.call('ZPOPMIN', zset_key, n + 1) -- evict oldest
63+
64+
-- Extract keys from returned data (ZPOPMIN returns [key,score,key,score,...] format)
6165
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
66+
for i = 1, #popped_keys_data, 2 do
67+
table.insert(popped_keys, popped_keys_data[i])
6768
end
69+
6870
if #popped_keys > 0 then
6971
redis.call('HDEL', hmap_key, unpack(popped_keys))
7072
c = #popped_keys

src/redis_func_cache/lua/lfu_put.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ else
5858
if maxsize > 0 then
5959
local n = redis.call('ZCARD', zset_key) - maxsize
6060
if n >= 0 then
61+
-- Use batch ZPOPMIN instead of looping calls
62+
local popped_keys_data = redis.call('ZPOPMIN', zset_key, n + 1) -- evict least frequently used
63+
64+
-- Extract keys from returned data (ZPOPMIN returns [key,score,key,score,...] format)
6165
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
66+
for i = 1, #popped_keys_data, 2 do
67+
table.insert(popped_keys, popped_keys_data[i])
6768
end
69+
6870
if #popped_keys > 0 then
6971
redis.call('HDEL', hmap_key, unpack(popped_keys))
7072
c = #popped_keys

src/redis_func_cache/lua/rr_put.lua

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ else
5858
if maxsize > 0 then
5959
local n = redis.call('SCARD', set_key) - maxsize
6060
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
61+
-- SPOP: Removes and returns one or more random members from the set value store at key
62+
local popped_keys = redis.call('SPOP', set_key, n + 1) -- evict random members
63+
64+
if type(popped_keys) == "string" then
65+
-- What returned by SPOP is a string when only one element is popped
66+
redis.call('HDEL', hmap_key, popped_keys)
67+
c = 1
68+
elseif type(popped_keys) == "table" and #popped_keys > 0 then
69+
-- what returned by SPOP is a table when multiple elements are popped
6970
redis.call('HDEL', hmap_key, unpack(popped_keys))
7071
c = #popped_keys
7172
end

0 commit comments

Comments
 (0)