From 1b32ccbdbf4b2ce1f51e0d02e3b411e533ff6e84 Mon Sep 17 00:00:00 2001 From: Vitaliy <160263432+vitalivo@users.noreply.github.com> Date: Sat, 12 Sep 2026 18:20:53 +0300 Subject: [PATCH] Apply cache replacement policy hooks during bulk writes --- AUTHORS.rst | 1 + src/cacheout/cache.py | 2 +- tests/test_lfu.py | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index b60f702..1fcf812 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -14,3 +14,4 @@ Contributors - John Bergvall , `johnbergvall@github `_ - AllinolCP, `AllinolCP@github `_ - uncle-lv, `uncle-lv@github `_ +- Vitaliy, `vitalivo@github `_ diff --git a/src/cacheout/cache.py b/src/cacheout/cache.py index 3aae3ac..506003e 100644 --- a/src/cacheout/cache.py +++ b/src/cacheout/cache.py @@ -419,7 +419,7 @@ def set_many(self, items: t.Mapping, ttl: t.Optional[T_TTL] = None) -> None: def _set_many(self, items: t.Mapping, ttl: t.Optional[T_TTL] = None) -> None: for key, value in items.items(): - self._set(key, value, ttl=ttl) + self.set(key, value, ttl=ttl) def delete(self, key: t.Hashable) -> int: """ diff --git a/tests/test_lfu.py b/tests/test_lfu.py index 6d68dc8..9b1a877 100644 --- a/tests/test_lfu.py +++ b/tests/test_lfu.py @@ -118,3 +118,22 @@ def test_lfu_access_count_using_default_callable(): assert value is False assert cache._access_counts["a"] == -1 + + +def test_lfu_set_many_respects_maxsize(): + cache = LFUCache(maxsize=2) + cache.set_many({"a": 1, "b": 2, "c": 3}) + assert list(cache.keys()) == ["b", "c"] + assert cache.popitem() == ("b", 2) + assert cache.popitem() == ("c", 3) + + +def test_lfu_set_many_preserves_eviction_order(): + cache = LFUCache(maxsize=2) + cache.set("hot", 1) + cache.get("hot") + cache.set_many({"cold": 2}) + cache.set("next", 3) + assert "hot" in cache + assert "cold" not in cache + assert "next" in cache