Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(proxy_server.py): fix setting router redis cache, if cache enable… (#8859)

Krish Dholakia54b7f17ca675
diff --git a/litellm/caching/redis_cache.py b/litellm/caching/redis_cache.py
index 960d19c3f81..66245e7476d 100644
--- a/litellm/caching/redis_cache.py
+++ b/litellm/caching/redis_cache.py
@@ -543,6 +543,7 @@ class RedisCache(BaseCache):
_redis_client: Redis = self.init_async_client() # type: ignore
start_time = time.time()
_used_ttl = self.get_ttl(ttl=ttl)
+ key = self.check_and_fix_namespace(key=key)
try:
result = await _redis_client.incrbyfloat(name=key, amount=value)
if _used_ttl is not None:
diff --git a/litellm/proxy/_new_secret_config.yaml b/litellm/proxy/_new_secret_config.yaml
index c3be5aa1d99..649d5a25d0a 100644
--- a/litellm/proxy/_new_secret_config.yaml
+++ b/litellm/proxy/_new_secret_config.yaml
@@ -27,3 +27,8 @@ model_list:
litellm_params:
model: openai/gpt-4o
+litellm_settings:
+ cache: true
+ cache_params: # set cache params for redis
+ type: redis
+ namespace: "litellm.caching"
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py
index 68fee62c760..19d0ebe9ea4 100644
--- a/litellm/proxy/proxy_server.py
+++ b/litellm/proxy/proxy_server.py
@@ -1653,10 +1653,6 @@ class ProxyConfig:
## INIT PROXY REDIS USAGE CLIENT ##
redis_usage_cache = litellm.cache.cache
- ## INIT ROUTER REDIS CACHE ##
- if llm_router is not None:
- llm_router._update_redis_cache(cache=redis_usage_cache)
-
async def get_config(self, config_file_path: Optional[str] = None) -> dict:
"""
Load config file
@@ -2183,6 +2179,9 @@ class ProxyConfig:
),
) # type:ignore
+ if redis_usage_cache is not None and router.cache.redis_cache is None:
+ router._update_redis_cache(cache=redis_usage_cache)
+
# Guardrail settings
guardrails_v2: Optional[List[Dict]] = None
diff --git a/tests/litellm/caching/test_redis_cache.py b/tests/litellm/caching/test_redis_cache.py
new file mode 100644
index 00000000000..c2549d7fce7
--- /dev/null
+++ b/tests/litellm/caching/test_redis_cache.py
@@ -0,0 +1,41 @@
+import json
+import os
+import sys
+from unittest.mock import MagicMock, patch
+
+import pytest
+from fastapi.testclient import TestClient
+
+sys.path.insert(
+ 0, os.path.abspath("../../..")
+) # Adds the parent directory to the system path
+from unittest.mock import AsyncMock
+
+from litellm.caching.redis_cache import RedisCache
+
+
[email protected]("namespace", [None, "test"])
+async def test_redis_cache_async_increment(namespace):
+ redis_cache = RedisCache(namespace=namespace)
+ # Create an AsyncMock for the Redis client
+ mock_redis_instance = AsyncMock()
+
+ # Make sure the mock can be used as an async context manager
+ mock_redis_instance.__aenter__.return_value = mock_redis_instance
+ mock_redis_instance.__aexit__.return_value = None
+
+ assert redis_cache is not None
+
+ expected_key = "test:test" if namespace else "test"
+
+ with patch.object(
+ redis_cache, "init_async_client", return_value=mock_redis_instance
+ ):
+ # Call async_set_cache
+ await redis_cache.async_increment(key=expected_key, value=1)
+
+ # Verify that the set method was called on the mock Redis instance
+ mock_redis_instance.incrbyfloat.assert_called_once_with(
+ name=expected_key, amount=1
+ )