Skip to content

Commit 396e732

Browse files
authored
Exposing the module version in loaded_modules (#1648)
This is useful for the case where one wants to instantiate a module, knowing the back end version. The reason: behaviour may differ based on redis module versions.
1 parent 866ac00 commit 396e732

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

redis/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,8 @@ def loaded_modules(self):
930930
return mods
931931

932932
try:
933-
mods = [f.get('name').lower() for f in self.info().get('modules')]
933+
mods = {f.get('name').lower(): f.get('ver')
934+
for f in self.info().get('modules')}
934935
except TypeError:
935936
mods = []
936937
setattr(self, key, mods)

redis/commands/json/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class JSON(JSONCommands):
2323
def __init__(
2424
self,
2525
client,
26+
version=None,
2627
decoder=JSONDecoder(),
2728
encoder=JSONEncoder(),
2829
):
@@ -62,6 +63,7 @@ def __init__(
6263

6364
self.client = client
6465
self.execute_command = client.execute_command
66+
self.MODULE_VERSION = version
6567

6668
for key, value in self.MODULE_CALLBACKS.items():
6769
self.client.set_response_callback(key, value)

redis/commands/redismodules.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,26 @@ class RedisModuleCommands:
99

1010
def json(self, encoder=JSONEncoder(), decoder=JSONDecoder()):
1111
"""Access the json namespace, providing support for redis json."""
12-
if 'rejson' not in self.loaded_modules:
12+
try:
13+
modversion = self.loaded_modules['rejson']
14+
except IndexError:
1315
raise ModuleError("rejson is not a loaded in the redis instance.")
1416

1517
from .json import JSON
16-
jj = JSON(client=self, encoder=encoder, decoder=decoder)
18+
jj = JSON(
19+
client=self,
20+
version=modversion,
21+
encoder=encoder,
22+
decoder=decoder)
1723
return jj
1824

1925
def ft(self, index_name="idx"):
2026
"""Access the search namespace, providing support for redis search."""
21-
if 'search' not in self.loaded_modules:
22-
raise ModuleError("search is not a loaded in the redis instance.")
27+
try:
28+
modversion = self.loaded_modules['search']
29+
except IndexError:
30+
raise ModuleError("rejson is not a loaded in the redis instance.")
2331

2432
from .search import Search
25-
s = Search(client=self, index_name=index_name)
33+
s = Search(client=self, version=modversion, index_name=index_name)
2634
return s

redis/commands/search/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ def commit(self):
8383
self.pipeline.execute()
8484
self.current_chunk = 0
8585

86-
def __init__(self, client, index_name="idx"):
86+
def __init__(self, client, version=None, index_name="idx"):
8787
"""
8888
Create a new Client for the given index_name.
8989
The default name is `idx`
9090
9191
If conn is not None, we employ an already existing redis connection
9292
"""
9393
self.client = client
94+
self.MODULE_VERSION = version
9495
self.index_name = index_name
9596
self.execute_command = client.execute_command
9697
self.pipeline = client.pipeline

tests/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_invalid_response(r):
2020
@skip_if_server_version_lt('4.0.0')
2121
def test_loaded_modules(r, modclient):
2222
assert r.loaded_modules == []
23-
assert 'rejson' in modclient.loaded_modules
23+
assert 'rejson' in modclient.loaded_modules.keys()
2424

2525

2626
@skip_if_server_version_lt('4.0.0')

0 commit comments

Comments
 (0)