From 5bd1b3968e6cc071c64eb919a9c7b60d4f1f6ee0 Mon Sep 17 00:00:00 2001 From: ishaan-jaff Date: Wed, 25 Oct 2023 11:29:47 -0700 Subject: [PATCH] (feat) Redis caching print exception statements when it fails --- litellm/caching.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/litellm/caching.py b/litellm/caching.py index 0a11bec21..0e508e37e 100644 --- a/litellm/caching.py +++ b/litellm/caching.py @@ -30,17 +30,25 @@ class RedisCache(): self.redis_client = redis.Redis(host=host, port=port, password=password) def set_cache(self, key, value): - self.redis_client.set(key, str(value)) + try: + self.redis_client.set(key, str(value)) + except Exception as e: + # NON blocking - notify users Redis is throwing an exception + print("LiteLLM Caching: Got exception from REDIS: ", e) def get_cache(self, key): - # TODO convert this to a ModelResponse object - cached_response = self.redis_client.get(key) - if cached_response!=None: - # cached_response is in `b{} convert it to ModelResponse - cached_response = cached_response.decode("utf-8") # Convert bytes to string - cached_response = json.loads(cached_response) # Convert string to dictionary - cached_response['cache'] = True # set cache-hit flag to True - return cached_response + try: + # TODO convert this to a ModelResponse object + cached_response = self.redis_client.get(key) + if cached_response!=None: + # cached_response is in `b{} convert it to ModelResponse + cached_response = cached_response.decode("utf-8") # Convert bytes to string + cached_response = json.loads(cached_response) # Convert string to dictionary + cached_response['cache'] = True # set cache-hit flag to True + return cached_response + except Exception as e: + # NON blocking - notify users Redis is throwing an exception + print("LiteLLM Caching: Got exception from REDIS: ", e) class HostedCache(): def set_cache(self, key, value):