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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
verify expires field prior to serving cache entry
Michal Otmianowskic444263e7dae
diff --git a/litellm/caching/s3_cache.py b/litellm/caching/s3_cache.pyindex 15f7a5c1e16..e3142ea1359 100644--- a/litellm/caching/s3_cache.py+++ b/litellm/caching/s3_cache.py@@ -13,6 +13,7 @@ import asyncioimport jsonfrom functools import partialfrom typing import Optional+from datetime import datetimefrom litellm._logging import print_verbose, verbose_logger@@ -72,8 +73,7 @@ class S3Cache(BaseCache):import datetime# Calculate expiration time- expiration_time = datetime.datetime.now() + datetime.timedelta(seconds=ttl)-+ expiration_time = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=ttl)# Upload the data to S3 with the calculated expiration timeself.s3_client.put_object(Bucket=self.bucket_name,@@ -126,6 +126,13 @@ class S3Cache(BaseCache):)if cached_response is not None:+ if "Expires" in cached_response:+ expires_time = cached_response['Expires']+ current_time = datetime.now(expires_time.tzinfo)++ if current_time > expires_time:+ return None+# cached_response is in `b{} convert it to ModelResponsecached_response = (cached_response["Body"].read().decode("utf-8")diff --git a/tests/test_litellm/caching/test_s3_cache.py b/tests/test_litellm/caching/test_s3_cache.pyindex dce3f7d585d..9c902768bfc 100644--- a/tests/test_litellm/caching/test_s3_cache.py+++ b/tests/test_litellm/caching/test_s3_cache.py@@ -56,7 +56,7 @@ def test_s3_cache_set_cache_with_ttl(mock_s3_dependencies):assert "max-age=3600" in call_args[1]["CacheControl"]-def test_s3_cache_get_cache(mock_s3_dependencies):+def test_s3_cache_get_cache_no_expires_info_in_response(mock_s3_dependencies):"""Test basic get_cache functionality"""cache = S3Cache("test-bucket")@@ -75,6 +75,54 @@ def test_s3_cache_get_cache(mock_s3_dependencies):assert result == {"key": "value", "number": 42}+def test_s3_cache_get_cache_with_expires_valid(mock_s3_dependencies):+ """Test get_cache when response contains Expires and cache entry is still valid"""+ cache = S3Cache("test-bucket")++ # Create a future expiration time (1 hour from now)+ future_time = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(hours=1)++ mock_response = {+ "Body": MagicMock(),+ "Expires": future_time+ }+ mock_response["Body"].read.return_value = b'{"key": "value", "number": 42}'+ cache.s3_client.get_object.return_value = mock_response++ result = cache.get_cache("test_key")++ cache.s3_client.get_object.assert_called_once_with(+ Bucket="test-bucket",+ Key="test_key"+ )++ # Should return the cached value since it's not expired+ assert result == {"key": "value", "number": 42}+++def test_s3_cache_get_cache_with_expires_expired(mock_s3_dependencies):+ """Test get_cache when response contains Expires and cache entry is no longer valid"""+ cache = S3Cache("test-bucket")++ # Create a past expiration time (1 hour ago)+ past_time = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(hours=1)++ mock_response = {+ "Body": MagicMock(),+ "Expires": past_time+ }+ mock_response["Body"].read.return_value = b'{"key": "value", "number": 42}'+ cache.s3_client.get_object.return_value = mock_response++ result = cache.get_cache("test_key")++ cache.s3_client.get_object.assert_called_once_with(+ Bucket="test-bucket",+ Key="test_key"+ )++ # Should return None since the cache entry is expired+ assert result is Nonedef test_s3_cache_get_cache_not_found(mock_s3_dependencies):"""Test get_cache when key is not found"""@@ -126,7 +174,6 @@ def test_s3_cache_initialization():cache_with_path = S3Cache("test-bucket", s3_path="my/cache/path")assert cache_with_path.key_prefix == "my/cache/path/"-# ============================================================================# ASYNC TESTS# ============================================================================