Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dargueta-mru-cache' into dev/4.2.0
- Loading branch information
Showing
5 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import collections | ||
|
||
from cachetools.cache import Cache | ||
|
||
|
||
class MRUCache(Cache): | ||
"""Most Recently Used (MRU) cache implementation.""" | ||
|
||
def __init__(self, maxsize, getsizeof=None): | ||
super().__init__(maxsize, getsizeof) | ||
self.__order = collections.OrderedDict() | ||
|
||
def __getitem__(self, key): | ||
value = super().__getitem__(key) | ||
self.__update(key) | ||
return value | ||
|
||
def __setitem__(self, key, value): | ||
super().__setitem__(key, value) | ||
self.__update(key) | ||
|
||
def __delitem__(self, key): | ||
super().__delitem__(key) | ||
del self.__order[key] | ||
|
||
def popitem(self): | ||
"""Remove and return the `(key, value)` pair most recently used.""" | ||
if not self.__order: | ||
raise KeyError(type(self).__name__ + ' cache is empty') from None | ||
|
||
key = next(iter(self.__order)) | ||
return (key, self.pop(key)) | ||
|
||
def __update(self, key): | ||
try: | ||
self.__order.move_to_end(key, last=False) | ||
except KeyError: | ||
self.__order[key] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
|
||
from cachetools import MRUCache | ||
|
||
from . import CacheTestMixin | ||
|
||
|
||
class MRUCacheTest(unittest.TestCase, CacheTestMixin): | ||
|
||
Cache = MRUCache | ||
|
||
def test_evict__writes_only(self): | ||
cache = MRUCache(maxsize=2) | ||
|
||
cache[1] = 1 | ||
cache[2] = 2 | ||
cache[3] = 3 # Evicts 1 because nothing's been used yet | ||
|
||
assert len(cache) == 2 | ||
assert 1 not in cache, 'Wrong key was evicted. Should have been `1`.' | ||
assert 2 in cache | ||
assert 3 in cache | ||
|
||
def test_evict__with_access(self): | ||
cache = MRUCache(maxsize=2) | ||
|
||
cache[1] = 1 | ||
cache[2] = 2 | ||
cache[1] | ||
cache[2] | ||
cache[3] = 3 # Evicts 2 | ||
assert 2 not in cache, 'Wrong key was evicted. Should have been `2`.' | ||
assert 1 in cache | ||
assert 3 in cache | ||
|
||
def test_evict__with_delete(self): | ||
cache = MRUCache(maxsize=2) | ||
|
||
cache[1] = 1 | ||
cache[2] = 2 | ||
del cache[2] | ||
cache[3] = 3 # Doesn't evict anything because we just deleted 2 | ||
|
||
assert 2 not in cache | ||
assert 1 in cache | ||
|
||
cache[4] = 4 # Should evict 1 as we just accessed it with __contains__ | ||
assert 1 not in cache | ||
assert 3 in cache | ||
assert 4 in cache |