Skip to content

Commit 8d2549e

Browse files
Copilotabrookins
andcommitted
Use flush() API to delete all entries instead of delete_query with empty attributes
- Changed delete() to use self._client.flush() instead of raising NotImplementedError - Changed adelete() to use self._client.flush_async() - Updated error messages in delete_by_attributes to suggest using delete()/clear() - Updated all tests to verify flush() is called - This fixes the 400 error by using the proper API endpoint for clearing all entries Co-authored-by: abrookins <[email protected]>
1 parent 9d33cd0 commit 8d2549e

File tree

2 files changed

+38
-46
lines changed

2 files changed

+38
-46
lines changed

redisvl/extensions/cache/llm/langcache.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -536,34 +536,16 @@ async def aupdate(self, key: str, **kwargs) -> None:
536536
def delete(self) -> None:
537537
"""Delete the entire cache.
538538
539-
Note: LangCache API does not support deleting all entries without
540-
specific attributes. Use delete_by_attributes() to delete entries
541-
matching specific criteria, or delete_by_id() to delete individual entries.
542-
543-
Raises:
544-
NotImplementedError: LangCache does not support clearing all entries.
539+
This deletes all entries in the cache by calling the flush API.
545540
"""
546-
raise NotImplementedError(
547-
"LangCache API does not support deleting all entries without attributes. "
548-
"Use delete_by_attributes(attributes) to delete entries matching specific "
549-
"criteria, or delete_by_id(entry_id) to delete individual entries."
550-
)
541+
self._client.flush()
551542

552543
async def adelete(self) -> None:
553544
"""Async delete the entire cache.
554545
555-
Note: LangCache API does not support deleting all entries without
556-
specific attributes. Use adelete_by_attributes() to delete entries
557-
matching specific criteria, or adelete_by_id() to delete individual entries.
558-
559-
Raises:
560-
NotImplementedError: LangCache does not support clearing all entries.
546+
This deletes all entries in the cache by calling the flush API.
561547
"""
562-
raise NotImplementedError(
563-
"LangCache API does not support deleting all entries without attributes. "
564-
"Use adelete_by_attributes(attributes) to delete entries matching specific "
565-
"criteria, or adelete_by_id(entry_id) to delete individual entries."
566-
)
548+
await self._client.flush_async()
567549

568550
def clear(self) -> None:
569551
"""Clear the cache of all entries.
@@ -611,6 +593,7 @@ def delete_by_attributes(self, attributes: Dict[str, Any]) -> Dict[str, Any]:
611593
if not attributes:
612594
raise ValueError(
613595
"attributes cannot be empty. Provide specific attributes to match, "
596+
"use delete() or clear() to delete all entries, "
614597
"or use delete_by_id(entry_id) to delete individual entries."
615598
)
616599
result = self._client.delete_query(attributes=attributes)
@@ -633,6 +616,7 @@ async def adelete_by_attributes(self, attributes: Dict[str, Any]) -> Dict[str, A
633616
if not attributes:
634617
raise ValueError(
635618
"attributes cannot be empty. Provide specific attributes to match, "
619+
"use adelete() or aclear() to delete all entries, "
636620
"or use adelete_by_id(entry_id) to delete individual entries."
637621
)
638622
result = await self._client.delete_query_async(attributes=attributes)

tests/unit/test_langcache_semantic_cache.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -354,63 +354,71 @@ def test_check_with_empty_attributes_does_not_send_attributes(
354354
_, call_kwargs = mock_client.search.call_args
355355
assert "attributes" not in call_kwargs
356356

357-
def test_delete_not_supported(self, mock_langcache_client):
358-
"""Test that delete() raises NotImplementedError."""
357+
def test_delete(self, mock_langcache_client):
358+
"""Test deleting the entire cache using flush()."""
359+
_, mock_client = mock_langcache_client
360+
359361
cache = LangCacheSemanticCache(
360362
name="test",
361363
server_url="https://api.example.com",
362364
cache_id="test-cache",
363365
api_key="test-key",
364366
)
365367

366-
with pytest.raises(
367-
NotImplementedError, match="does not support deleting all entries"
368-
):
369-
cache.delete()
368+
cache.delete()
369+
370+
mock_client.flush.assert_called_once()
370371

371372
@pytest.mark.asyncio
372-
async def test_adelete_not_supported(self, mock_langcache_client):
373-
"""Test that async delete() raises NotImplementedError."""
373+
async def test_adelete(self, mock_langcache_client):
374+
"""Test async deleting the entire cache using flush()."""
375+
_, mock_client = mock_langcache_client
376+
377+
mock_client.flush_async = AsyncMock()
378+
374379
cache = LangCacheSemanticCache(
375380
name="test",
376381
server_url="https://api.example.com",
377382
cache_id="test-cache",
378383
api_key="test-key",
379384
)
380385

381-
with pytest.raises(
382-
NotImplementedError, match="does not support deleting all entries"
383-
):
384-
await cache.adelete()
386+
await cache.adelete()
387+
388+
mock_client.flush_async.assert_called_once()
389+
390+
def test_clear(self, mock_langcache_client):
391+
"""Test that clear() calls delete() which uses flush()."""
392+
_, mock_client = mock_langcache_client
385393

386-
def test_clear_not_supported(self, mock_langcache_client):
387-
"""Test that clear() raises NotImplementedError."""
388394
cache = LangCacheSemanticCache(
389395
name="test",
390396
server_url="https://api.example.com",
391397
cache_id="test-cache",
392398
api_key="test-key",
393399
)
394400

395-
with pytest.raises(
396-
NotImplementedError, match="does not support deleting all entries"
397-
):
398-
cache.clear()
401+
cache.clear()
402+
403+
mock_client.flush.assert_called_once()
399404

400405
@pytest.mark.asyncio
401-
async def test_aclear_not_supported(self, mock_langcache_client):
402-
"""Test that async clear() raises NotImplementedError."""
406+
async def test_aclear(self, mock_langcache_client):
407+
"""Test that async clear() calls adelete() which uses flush()."""
408+
_, mock_client = mock_langcache_client
409+
410+
mock_client.flush_async = AsyncMock()
411+
403412
cache = LangCacheSemanticCache(
404413
name="test",
405414
server_url="https://api.example.com",
406415
cache_id="test-cache",
407416
api_key="test-key",
408417
)
409418

410-
with pytest.raises(
411-
NotImplementedError, match="does not support deleting all entries"
412-
):
413-
await cache.aclear()
419+
await cache.aclear()
420+
421+
mock_client.flush_async.assert_called_once()
414422

415423
def test_delete_by_id(self, mock_langcache_client):
416424
"""Test deleting a single entry by ID."""

0 commit comments

Comments
 (0)