From 84645fbc07a2f1dd2725002045a554b89598d99a Mon Sep 17 00:00:00 2001 From: wjchi Date: Tue, 8 Oct 2024 11:37:23 +0800 Subject: [PATCH] fix: LRUCache.set update cache value when key exists --- tests/test_utils.py | 8 ++++++++ tinydb/utils.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index b7472413..031e086f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -29,6 +29,14 @@ def test_lru_cache_set_multiple(): assert cache.lru == ["a"] +def test_lru_cache_set_update(): + cache = LRUCache(capacity=3) + cache["a"] = 1 + cache["a"] = 2 + + assert cache["a"] == 2 + + def test_lru_cache_get(): cache = LRUCache(capacity=3) cache["a"] = 1 diff --git a/tinydb/utils.py b/tinydb/utils.py index 08430ba1..e1f01fbc 100644 --- a/tinydb/utils.py +++ b/tinydb/utils.py @@ -99,8 +99,8 @@ def get(self, key: K, default: Optional[D] = None) -> Optional[Union[V, D]]: def set(self, key: K, value: V): if self.cache.get(key): + self.cache[key] = value self.cache.move_to_end(key, last=True) - else: self.cache[key] = value