This commit is contained in:
pei711 2026-10-17 13:13:46 +00:00 • committed by GitHub
commit 46c57eeb46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 38 additions and 3 deletions

View file

@ -556,7 +556,7 @@ class Cache:
Returns:
str: The final hashed cache key with the redis namespace.
"""
dynamic_cache_control: Final[DynamicCacheControl] = kwargs.get("cache", {})
dynamic_cache_control: Final[DynamicCacheControl] = kwargs.get("cache") or {}
metadata: Final = kwargs.get("metadata") or {}
namespace: Final = dynamic_cache_control.get("namespace") or metadata.get("redis_namespace") or self.namespace
if namespace:
@ -661,7 +661,7 @@ class Cache:
else:
cache_key = self.get_cache_key(**kwargs)
if cache_key is not None:
cache_control_args: Final[DynamicCacheControl] = kwargs.get("cache", {})
cache_control_args: Final[DynamicCacheControl] = kwargs.get("cache") or {}
max_age = cache_control_args.get("s-maxage") or cache_control_args.get("s-max-age") or float("inf")
cache_lookup_kwargs: Final = self._get_safe_cache_lookup_kwargs(kwargs)
if dynamic_cache_object is not None:
@ -694,7 +694,7 @@ class Cache:
else:
cache_key = self.get_cache_key(**kwargs)
if cache_key is not None:
cache_control_args: Final = kwargs.get("cache", {})
cache_control_args: Final = kwargs.get("cache") or {}
max_age: Final = cache_control_args.get(
"s-max-age", cache_control_args.get("s-maxage", float("inf"))
)

View file

@ -0,0 +1,35 @@
from typing import Final
from litellm.caching.caching import Cache
from litellm.types.caching import LiteLLMCacheType
def test_explicit_null_cache_control_uses_cache_namespace():
cache: Final = Cache(type=LiteLLMCacheType.LOCAL, namespace="instance")
request: Final = {"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hello"}]}
default_key: Final = cache.get_cache_key(**request, metadata=None)
null_cache_key: Final = cache.get_cache_key(**request, metadata=None, cache=None)
request_namespace_key: Final = cache.get_cache_key(
**request,
metadata=None,
cache={"namespace": "request"},
)
assert null_cache_key == default_key
assert request_namespace_key.startswith("request:")
assert request_namespace_key != default_key
def test_explicit_null_cache_control_allows_sync_cache_lookup():
cache: Final = Cache(type=LiteLLMCacheType.LOCAL)
request: Final = {"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hello"}]}
assert cache.get_cache(**request, cache=None) is None
async def test_explicit_null_cache_control_allows_async_cache_lookup():
cache: Final = Cache(type=LiteLLMCacheType.LOCAL)
request: Final = {"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hello"}]}
assert await cache.async_get_cache(**request, cache=None) is None