summaryrefslogtreecommitdiff
path: root/searx/redislib.py
diff options
context:
space:
mode:
authorMarkus Heiser <markus.heiser@darmarit.de>2022-05-30 11:05:24 +0200
committerMarkus Heiser <markus.heiser@darmarit.de>2022-05-30 11:21:09 +0200
commit1a8ff10e300f60e4221a33caf2018a636e1b2f09 (patch)
tree36adb1decc1e31a5dacda990573d9dc6daa3d68c /searx/redislib.py
parent4b185f0e116861d84dd3afa2d84ec9757af56a69 (diff)
downloadsearxng-1a8ff10e300f60e4221a33caf2018a636e1b2f09.tar.gz
searxng-1a8ff10e300f60e4221a33caf2018a636e1b2f09.zip
[mod] redislib - optimize LUA script registration.
Instead of register the LUA script on each call, the LUA script is registered once and the `Script` object is cached in global storage. [1] https://github.com/searxng/searxng/pull/1047#discussion_r884143305 Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/redislib.py')
-rw-r--r--searx/redislib.py34
1 files changed, 31 insertions, 3 deletions
diff --git a/searx/redislib.py b/searx/redislib.py
index 0314cf832..a90e15bc2 100644
--- a/searx/redislib.py
+++ b/searx/redislib.py
@@ -14,6 +14,34 @@ import hmac
from searx import get_setting
+LUA_SCRIPT_STORAGE = {}
+"""A global dictionary to cache client's ``Script`` objects, used by
+:py:obj:`lua_script_storage`"""
+
+
+def lua_script_storage(client, script):
+ """Returns a redis :py:obj:`Script
+ <redis.commands.core.CoreCommands.register_script>` instance.
+
+ Due to performance reason the ``Script`` object is instantiated only once
+ for a client (``client.register_script(..)``) and is cached in
+ :py:obj:`LUA_SCRIPT_STORAGE`.
+
+ """
+
+ # redis connection can be closed, lets use the id() of the redis connector
+ # as key in the script-storage:
+ client_id = id(client)
+
+ if LUA_SCRIPT_STORAGE.get(client_id) is None:
+ LUA_SCRIPT_STORAGE[client_id] = {}
+
+ if LUA_SCRIPT_STORAGE[client_id].get(script) is None:
+ LUA_SCRIPT_STORAGE[client_id][script] = client.register_script(script)
+
+ return LUA_SCRIPT_STORAGE[client_id][script]
+
+
PURGE_BY_PREFIX = """
local prefix = tostring(ARGV[1])
for i, name in ipairs(redis.call('KEYS', prefix .. '*')) do
@@ -41,7 +69,7 @@ def purge_by_prefix(client, prefix: str = "SearXNG_"):
.. _DEL: https://redis.io/commands/del/
"""
- script = client.register_script(PURGE_BY_PREFIX)
+ script = lua_script_storage(client, PURGE_BY_PREFIX)
script(args=[prefix])
@@ -123,7 +151,7 @@ def incr_counter(client, name: str, limit: int = 0, expire: int = 0):
(5, 1)
"""
- script = client.register_script(INCR_COUNTER)
+ script = lua_script_storage(client, INCR_COUNTER)
name = "SearXNG_counter_" + secret_hash(name)
c = script(args=[limit, expire], keys=[name])
return c
@@ -207,7 +235,7 @@ def incr_sliding_window(client, name: str, duration: int):
1
"""
- script = client.register_script(INCR_SLIDING_WINDOW)
+ script = lua_script_storage(client, INCR_SLIDING_WINDOW)
name = "SearXNG_counter_" + secret_hash(name)
c = script(args=[duration], keys=[name])
return c