summaryrefslogtreecommitdiff
path: root/redis/client.py
diff options
context:
space:
mode:
Diffstat (limited to 'redis/client.py')
-rwxr-xr-xredis/client.py48
1 files changed, 43 insertions, 5 deletions
diff --git a/redis/client.py b/redis/client.py
index a6bd183..4db9887 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -6,10 +6,7 @@ import re
import threading
import time
import warnings
-from redis.commands import (
- list_or_args,
- Commands
-)
+from redis.commands import CoreCommands, RedisModuleCommands, list_or_args
from redis.connection import (ConnectionPool, UnixDomainSocketConnection,
SSLConnection)
from redis.lock import Lock
@@ -609,7 +606,7 @@ def parse_set_result(response, **options):
return response and str_if_bytes(response) == 'OK'
-class Redis(Commands, object):
+class Redis(RedisModuleCommands, CoreCommands, object):
"""
Implementation of the Redis protocol.
@@ -898,6 +895,47 @@ class Redis(Commands, object):
"Set a custom Response Callback"
self.response_callbacks[command] = callback
+ def load_external_module(self, modname, funcname, func):
+ """
+ This function can be used to add externally defined redis modules,
+ and their namespaces to the redis client.
+ modname - A string containing the name of the redis module to look for
+ in the redis info block.
+ funcname - A string containing the name of the function to create
+ func - The function, being added to this class.
+
+ ex: Assume that one has a custom redis module named foomod that
+ creates command named 'foo.dothing' and 'foo.anotherthing' in redis.
+ To load function functions into this namespace:
+
+ from redis import Redis
+ from foomodule import F
+ r = Redis()
+ r.load_external_module("foomod", "foo", F)
+ r.foo().dothing('your', 'arguments')
+
+ For a concrete example see the reimport of the redisjson module in
+ tests/test_connection.py::test_loading_external_modules
+ """
+ mods = self.loaded_modules
+ if modname.lower() not in mods:
+ raise ModuleError("{} is not loaded in redis.".format(modname))
+ setattr(self, funcname, func)
+
+ @property
+ def loaded_modules(self):
+ key = '__redis_modules__'
+ mods = getattr(self, key, None)
+ if mods is not None:
+ return mods
+
+ try:
+ mods = [f.get('name').lower() for f in self.info().get('modules')]
+ except TypeError:
+ mods = []
+ setattr(self, key, mods)
+ return mods
+
def pipeline(self, transaction=True, shard_hint=None):
"""
Return a new pipeline object that can queue multiple commands for