Skip to content

Commit 572aa94

Browse files
astutejoecarltongibson
authored andcommitted
Moving LUA arguments to ARGV to optimize Redis script caching
1 parent 4c88088 commit 572aa94

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

CHANGELOG.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Unreleased
55

66
* Ensured channel names are unique using UUIDs.
77

8+
* Ensured messages are expired even when channel is in constant activity.
9+
10+
* Optimized Redis script caching
11+
812

913
2.4.2 (2020-02-19)
1014
------------------

channels_redis/core.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -652,20 +652,20 @@ async def group_send(self, group, message):
652652
# stored in channel_to_message dict and contains the
653653
# __asgi_channel__ key.
654654

655-
group_send_lua = """ local over_capacity = 0
656-
for i=1,#KEYS do
657-
if redis.call('ZCOUNT', KEYS[i], '-inf', '+inf') < tonumber(ARGV[i + #KEYS]) then
658-
redis.call('ZADD', KEYS[i], %f, ARGV[i])
659-
redis.call('EXPIRE', KEYS[i], %d)
660-
else
661-
over_capacity = over_capacity + 1
662-
end
655+
group_send_lua = """
656+
local over_capacity = 0
657+
local current_time = ARGV[#ARGV - 1]
658+
local expiry = ARGV[#ARGV]
659+
for i=1,#KEYS do
660+
if redis.call('ZCOUNT', KEYS[i], '-inf', '+inf') < tonumber(ARGV[i + #KEYS]) then
661+
redis.call('ZADD', KEYS[i], current_time, ARGV[i])
662+
redis.call('EXPIRE', KEYS[i], expiry)
663+
else
664+
over_capacity = over_capacity + 1
663665
end
664-
return over_capacity
665-
""" % (
666-
time.time(),
667-
self.expiry,
668-
)
666+
end
667+
return over_capacity
668+
"""
669669

670670
# We need to filter the messages to keep those related to the connection
671671
args = [
@@ -679,6 +679,8 @@ async def group_send(self, group, message):
679679
for channel_key in channel_redis_keys
680680
]
681681

682+
args += [time.time(), self.expiry]
683+
682684
# channel_keys does not contain a single redis key more than once
683685
async with self.connection(connection_index) as connection:
684686
channels_over_capacity = await connection.eval(

0 commit comments

Comments
 (0)