Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions litellm/caching/redis_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,10 +1259,11 @@ async def test_connection(self) -> dict:
async def async_delete_cache(self, key: str):
# 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
key = self.check_and_fix_namespace(key=key)
return await _redis_client.delete(key)

def delete_cache(self, key):
def delete_cache(self, key: str):
key = self.check_and_fix_namespace(key=key)
self.redis_client.delete(key)

async def _pipeline_increment_helper(
Expand Down
27 changes: 26 additions & 1 deletion tests/test_litellm/caching/test_redis_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient

sys.path.insert(
0, os.path.abspath("../../..")
Expand Down Expand Up @@ -473,3 +472,29 @@ async def test_async_lpop_with_float_redis_version(

# Verify the method completed without error
assert result is not None


@pytest.mark.parametrize("namespace", [None, "myns"])
@pytest.mark.asyncio
async def test_delete_cache_applies_namespace(namespace, monkeypatch, redis_no_ping):
"""async_delete_cache and delete_cache must apply the namespace prefix so that
DEL targets the same key that SET/GET use."""
monkeypatch.setenv("REDIS_HOST", "https://my-test-host")
redis_cache = RedisCache(namespace=namespace)

raw_key = "cronjob_lock:db_spend_update_job"
expected_key = f"{namespace}:{raw_key}" if namespace else raw_key

# --- async path ---
mock_async_client = AsyncMock()
with patch.object(
redis_cache, "init_async_client", return_value=mock_async_client
):
await redis_cache.async_delete_cache(key=raw_key)
mock_async_client.delete.assert_called_once_with(expected_key)

# --- sync path ---
mock_sync_client = MagicMock()
redis_cache.redis_client = mock_sync_client
redis_cache.delete_cache(key=raw_key)
mock_sync_client.delete.assert_called_once_with(expected_key)
Loading