diff --git a/cachetory/caches/async_.py b/cachetory/caches/async_.py index 4844857..cef42d1 100644 --- a/cachetory/caches/async_.py +++ b/cachetory/caches/async_.py @@ -139,6 +139,10 @@ async def delete(self, key: str) -> bool: """ return await self._backend.delete(f"{self._prefix}{key}") + async def clear(self) -> None: + """Delete all cache items.""" + return await self._backend.clear() + async def __aexit__( self, exc_type: type[BaseException] | None, diff --git a/cachetory/caches/sync.py b/cachetory/caches/sync.py index cfb1da2..14e93a6 100644 --- a/cachetory/caches/sync.py +++ b/cachetory/caches/sync.py @@ -138,6 +138,10 @@ def delete(self, key: str) -> bool: """ return self._backend.delete(f"{self._prefix}{key}") + def clear(self) -> None: + """Delete all cache items.""" + return self._backend.clear() + def __delitem__(self, key: str) -> None: """ Delete the cache item. diff --git a/tests/caches/test_async.py b/tests/caches/test_async.py index 2a07947..317807e 100644 --- a/tests/caches/test_async.py +++ b/tests/caches/test_async.py @@ -46,6 +46,17 @@ async def test_delete(memory_cache: Cache[int, bytes]) -> None: assert await memory_cache.get("foo") is None +@mark.asyncio +async def test_clear(memory_cache: Cache[int, bytes]) -> None: + await memory_cache.set("foo", 42) + await memory_cache.set("bar", 42) + + await memory_cache.clear() + + assert await memory_cache.get("foo") is None + assert await memory_cache.get("bar") is None + + @mark.asyncio async def test_serialize_executor() -> None: cache = Cache[int, bytes]( diff --git a/tests/caches/test_sync.py b/tests/caches/test_sync.py index 589db41..4489e70 100644 --- a/tests/caches/test_sync.py +++ b/tests/caches/test_sync.py @@ -46,6 +46,16 @@ def test_delete(memory_cache: Cache[int, bytes]) -> None: assert memory_cache.get("foo") is None +def test_clear_cache(memory_cache: Cache[int, bytes]) -> None: + memory_cache.set("foo", 42) + memory_cache.set("bar", 42) + + memory_cache.clear() + + assert memory_cache.get("foo") is None + assert memory_cache.get("bar") is None + + def test_del_item(memory_cache: Cache[int, bytes]) -> None: memory_cache.set("foo", 42) del memory_cache["foo"]