Skip to content

Commit f3b1672

Browse files
authored
add clear method (#7)
1 parent e5d1cae commit f3b1672

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

atomic_lru/_storage/storage.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ def delete(self, key: str) -> bool:
348348
self.__delete(key)
349349
return True
350350

351+
def clear(self) -> None:
352+
"""Clear all items from the cache.
353+
354+
Removes all key-value pairs from the cache and resets the size tracking
355+
to the initial empty state. This operation is thread-safe.
356+
357+
Raises:
358+
RuntimeError: If the storage has been closed.
359+
"""
360+
with self.__lock:
361+
self._assert_not_closed()
362+
self._data.clear()
363+
self._size_in_bytes = sys.getsizeof(self._data)
364+
351365
def _clean_expired(
352366
self, start: int | None = None, stop: int | None = None
353367
) -> tuple[int, int]:

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ license = "MIT"
1010
dependencies = []
1111

1212
[project.urls]
13-
Homepage = "https://github.com/fabien-marty/atomic-lru"
13+
Homepage = "https://fabien-marty.github.io/atomic-lru"
1414
Repository = "https://github.com/fabien-marty/atomic-lru.git"
15+
Documentation = "https://fabien-marty.github.io/atomic-lru"
1516

1617
[tool.uv]
1718
required-version = ">=0.9.0"

tests/test_storage.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,62 @@ def test_overwrite_existing_key_size_tracking():
298298
assert storage.get("key1") == medium_value
299299

300300
storage.close()
301+
302+
303+
def test_clear():
304+
"""Test that clear() removes all items and resets size tracking."""
305+
storage = Storage[bytes](
306+
size_limit_in_bytes=4096,
307+
max_items=100,
308+
expiration_thread_max_checks_per_iteration=0,
309+
)
310+
initial_size = storage.size_in_bytes
311+
assert storage.number_of_items == 0
312+
313+
# Clear empty cache should work
314+
storage.clear()
315+
assert storage.number_of_items == 0
316+
assert storage.size_in_bytes == initial_size
317+
318+
# Add some items
319+
storage.set("key1", b"value1")
320+
storage.set("key2", b"value2")
321+
storage.set("key3", b"value3")
322+
assert storage.number_of_items == 3
323+
assert storage.size_in_bytes > initial_size
324+
325+
# Verify items exist
326+
assert storage.get("key1") == b"value1"
327+
assert storage.get("key2") == b"value2"
328+
assert storage.get("key3") == b"value3"
329+
330+
# Clear the cache
331+
storage.clear()
332+
assert storage.number_of_items == 0
333+
assert storage.size_in_bytes == initial_size
334+
335+
# Verify items are gone
336+
assert storage.get("key1") is CACHE_MISS
337+
assert storage.get("key2") is CACHE_MISS
338+
assert storage.get("key3") is CACHE_MISS
339+
340+
# Can add items after clearing
341+
storage.set("key4", b"value4")
342+
assert storage.number_of_items == 1
343+
assert storage.get("key4") == b"value4"
344+
345+
storage.close()
346+
347+
348+
def test_clear_closed_storage():
349+
"""Test that clear() raises RuntimeError when storage is closed."""
350+
storage = Storage[bytes](
351+
size_limit_in_bytes=4096,
352+
max_items=100,
353+
expiration_thread_max_checks_per_iteration=0,
354+
)
355+
storage.set("key1", b"value1")
356+
storage.close()
357+
358+
with pytest.raises(RuntimeError, match="Storage is closed"):
359+
storage.clear()

0 commit comments

Comments
 (0)