From 168fcf097569c73b1c181dc327406bcf13429bff Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Tue, 23 Aug 2022 19:09:43 +0530 Subject: [PATCH 1/2] build(deps): bump redis to latest version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 9bcf519b40..8844a507bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,7 +54,7 @@ dependencies = [ "python-dateutil~=2.8.1", "pytz==2022.1", "rauth~=0.7.3", - "redis~=3.5.3", + "redis~=4.3.4", "hiredis~=2.0.0", "requests-oauthlib~=1.3.0", "requests~=2.27.1", From 1f089f44f60796e8cceb648f9344fb87deb33b2f Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Thu, 25 Aug 2022 15:57:35 +0530 Subject: [PATCH 2/2] feat: RedisearchWrapper Like rediswrapper but for redisearch to allow multitenancy. --- frappe/utils/redis_wrapper.py | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/frappe/utils/redis_wrapper.py b/frappe/utils/redis_wrapper.py index a41e47b0c6..915f792180 100644 --- a/frappe/utils/redis_wrapper.py +++ b/frappe/utils/redis_wrapper.py @@ -4,11 +4,26 @@ import pickle import re import redis +from redis.commands.search import Search import frappe from frappe.utils import cstr +class RedisearchWrapper(Search): + def sugadd(self, key, *suggestions, **kwargs): + return super().sugadd(self.client.make_key(key), *suggestions, **kwargs) + + def suglen(self, key): + return super().suglen(self.client.make_key(key)) + + def sugdel(self, key, string): + return super().sugdel(self.client.make_key(key), string) + + def sugget(self, key, *args, **kwargs): + return super().sugget(self.client.make_key(key), *args, **kwargs) + + class RedisWrapper(redis.Redis): """Redis client that will automatically prefix conf.db_name""" @@ -148,7 +163,16 @@ class RedisWrapper(redis.Redis): def ltrim(self, key, start, stop): return super().ltrim(self.make_key(key), start, stop) - def hset(self, name: str, key: str, value, shared: bool = False, cache_locally: bool = True): + def hset( + self, + name: str, + key: str, + value, + shared: bool = False, + cache_locally: bool = True, + *args, + **kwargs, + ): if key is None: return @@ -160,7 +184,7 @@ class RedisWrapper(redis.Redis): # set in redis try: - super().hset(_name, key, pickle.dumps(value)) + super().hset(_name, key, pickle.dumps(value), *args, **kwargs) except redis.exceptions.ConnectionError: pass @@ -248,3 +272,6 @@ class RedisWrapper(redis.Redis): def smembers(self, name): """Return all members of the set""" return super().smembers(self.make_key(name)) + + def ft(self, index_name="idx"): + return RedisearchWrapper(client=self, index_name=self.make_key(index_name))