Skip to content

Commit

Permalink
feat:support for loading lua modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Apr 1, 2024
1 parent ddc7814 commit 06d3f17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion fakeredis/_fakesocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
from .commands_mixins.scripting_mixin import ScriptingCommandsMixin
except ImportError:
class ScriptingCommandsMixin: # type: ignore # noqa: E303
pass
def __init__(self, *args, **kwargs) -> None:
kwargs.pop("lua_modules", None)
super(ScriptingCommandsMixin, self).__init__(*args, **kwargs)

from .commands_mixins.server_mixin import ServerCommandsMixin
from .commands_mixins.set_mixin import SetCommandsMixin
Expand Down
12 changes: 11 additions & 1 deletion fakeredis/commands_mixins/scripting_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ class ScriptingCommandsMixin:
def __init__(self, *args: Any, **kwargs: Any):
self.script_cache: Dict[bytes, bytes] = dict() # Maps SHA1 to the script source
self.version: Tuple[int]
self.load_lua_modules: Set[str] = kwargs.pop("lua_modules", None) or set()
self.load_lua_modules = set()
lua_modules_set: Set[str] = kwargs.pop("lua_modules", None) or set()
if len(lua_modules_set) > 0:
lua_runtime: LUA_MODULE.LuaRuntime = LUA_MODULE.LuaRuntime(encoding=None, unpack_returned_tuples=True)
for module in lua_modules_set:
try:
lua_runtime.require(module.encode())
self.load_lua_modules.add(module)
except LUA_MODULE.LuaError as ex:
LOGGER.error(f'Failed to load LUA module "{module}", make sure it is installed: {ex}')

super(ScriptingCommandsMixin, self).__init__(*args, **kwargs)

def _convert_redis_arg(self, lua_runtime: LUA_MODULE.LuaRuntime, value: Any) -> bytes:
Expand Down

0 comments on commit 06d3f17

Please sign in to comment.