Skip to content

Commit

Permalink
Ignore KeyError in LRUCache when moving key to end when default value…
Browse files Browse the repository at this point in the history
… used.

Fixes #4
  • Loading branch information
dgilland committed Jul 31, 2018
1 parent c7e873b commit 937d2df
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Changelog
=========


- Fix bug in ``LRUCache.get()`` where supplying a ``default`` value would result in a ``KeyError``.


v0.10.1 (2018-07-15)
--------------------

Expand Down
9 changes: 8 additions & 1 deletion src/cacheout/lru.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ class LRUCache(Cache):
"""
def _get(self, key, default=None):
value = super()._get(key, default=default)
self._cache.move_to_end(key)

try:
self._cache.move_to_end(key)
except KeyError:
# KeyError occurs when cache key doesn't exist which happens when
# default value is used.
pass

return value
6 changes: 6 additions & 0 deletions tests/test_lru.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ def test_lru_get(cache):
"""Test that LRUCache.get() returns cached value."""
for key, value in cache.items():
assert cache.get(key) == value


def test_lru_get_default(cache):
"""Test that LRUCache.get() returns a default value."""
default = 'bar'
assert cache.get('foo', default=default) == default

0 comments on commit 937d2df

Please sign in to comment.