From 2e939a21b388c88fc793a4e04fec783a46720776 Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Wed, 2 Apr 2025 14:37:39 -0700 Subject: [PATCH] refactor pod lock manager to use redis --- litellm/caching/redis_cache.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/litellm/caching/redis_cache.py b/litellm/caching/redis_cache.py index 63cd4d09598..1d553c9c80d 100644 --- a/litellm/caching/redis_cache.py +++ b/litellm/caching/redis_cache.py @@ -303,13 +303,19 @@ class RedisCache(BaseCache): raise e key = self.check_and_fix_namespace(key=key) - ttl = self.get_ttl(**kwargs) + ttl = self.get_ttl(**kwargs) or kwargs.get("ex", None) + nx = kwargs.get("nx", False) print_verbose(f"Set ASYNC Redis Cache: key: {key}\nValue {value}\nttl={ttl}") try: if not hasattr(_redis_client, "set"): raise Exception("Redis client cannot set cache. Attribute not found.") - await _redis_client.set(name=key, value=json.dumps(value), ex=ttl) + result = await _redis_client.set( + name=key, + value=json.dumps(value), + nx=nx, + ex=ttl, + ) print_verbose( f"Successfully Set ASYNC Redis Cache: key: {key}\nValue {value}\nttl={ttl}" ) @@ -326,6 +332,7 @@ class RedisCache(BaseCache): event_metadata={"key": key}, ) ) + return result except Exception as e: end_time = time.time() _duration = end_time - start_time @@ -931,7 +938,7 @@ class RedisCache(BaseCache): # typed as Any, redis python lib has incomplete type stubs for RedisCluster and does not include `delete` _redis_client: Any = self.init_async_client() # keys is str - await _redis_client.delete(key) + return await _redis_client.delete(key) def delete_cache(self, key): self.redis_client.delete(key)