Skip to content

Commit

Permalink
NEW: add clear method to caches
Browse files Browse the repository at this point in the history
closes #135
  • Loading branch information
Sergio Castillo authored and eigenein committed Aug 14, 2024
1 parent 63d5ed7 commit 7ed954a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cachetory/caches/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions cachetory/caches/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions tests/caches/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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](
Expand Down
10 changes: 10 additions & 0 deletions tests/caches/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 7ed954a

Please sign in to comment.