From b9c0662de6b44781a68351eeb81f83a892d748e3 Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Tue, 7 Jan 2025 13:46:47 +0530 Subject: [PATCH] fix: Make client cache work without redis too --- frappe/utils/redis_wrapper.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/frappe/utils/redis_wrapper.py b/frappe/utils/redis_wrapper.py index 24160183a2..bc14834de3 100644 --- a/frappe/utils/redis_wrapper.py +++ b/frappe/utils/redis_wrapper.py @@ -454,7 +454,25 @@ class ClientCache: self.cache: dict[bytes, CachedValue] = {} self.invalidator = frappe.cache - self.invalidator_id = self.invalidator.client_id() + self.healthy = True + self.connection_retries = 0 + self.invalidator_id = None + + try: + self.invalidator_id = self.invalidator.client_id() + except redis.exceptions.ConnectionError: + # Redis not available, this can happen during setup/startup time + self.redis = frappe.cache + self.healthy = False + + # These are local hits and misses, *not* global. + # - Local miss = not found in worker memory + # - Global miss = not found in Redis too + # These stats can be *slightly* off, these aren't guarded by a mutex. + self.hits = self.misses = 0 + + if not self.invalidator_id: + return self.redis: RedisWrapper = RedisWrapper.from_url( frappe.conf.get("redis_cache"), @@ -463,14 +481,6 @@ class ClientCache: protocol=2, ) self.invalidator_thread = self.run_invalidator_thread() - self.healthy = True - self.connection_retries = 0 - - # These are local hits and misses, *not* global. - # - Local miss = not found in worker memory - # - Global miss = not found in Redis too - # These stats can be *slightly* off, these aren't guarded by a mutex. - self.hits = self.misses = 0 def get_value(self, key): key = self.redis.make_key(key)