Skip to content

Commit

Permalink
fix: LRUCache.set update cache value when key exists
Browse files Browse the repository at this point in the history
  • Loading branch information
wjchi committed Oct 8, 2024
1 parent 87e7ed3 commit 84645fb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tinydb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 84645fb

Please sign in to comment.