Fix bug in in-memory data cache

Previously, if we didn't find a key, we'd empty the entire cache, making it essentially a single-key cache. We skip clearing now, although this does mean we won't GC expired entries (not a problem for tests, though)
This commit is contained in:
Joseph Schorr 2018-05-01 13:25:46 +03:00
parent 23c19bcbc1
commit 178c8e7cb0
2 changed files with 6 additions and 3 deletions

2
data/cache/impl.py vendored
View file

@ -40,7 +40,7 @@ class NoopDataModelCache(DataModelCache):
class InMemoryDataModelCache(DataModelCache): class InMemoryDataModelCache(DataModelCache):
""" Implementation of the data model cache backed by an in-memory dictionary. """ """ Implementation of the data model cache backed by an in-memory dictionary. """
def __init__(self): def __init__(self):
self.cache = ExpiresDict(rebuilder=lambda: {}) self.cache = ExpiresDict()
def retrieve(self, cache_key, loader, should_cache=is_not_none): def retrieve(self, cache_key, loader, should_cache=is_not_none):
not_found = [None] not_found = [None]

View file

@ -17,9 +17,9 @@ class ExpiresEntry(object):
class ExpiresDict(object): class ExpiresDict(object):
""" ExpiresDict defines a dictionary-like class whose keys have expiration. The rebuilder is """ ExpiresDict defines a dictionary-like class whose keys have expiration. The rebuilder is
a function that returns the full contents of the cached dictionary as a dict of the keys a function that returns the full contents of the cached dictionary as a dict of the keys
and whose values are TTLEntry's. and whose values are TTLEntry's. If the rebuilder is None, then no rebuilding is performed.
""" """
def __init__(self, rebuilder): def __init__(self, rebuilder=None):
self._rebuilder = rebuilder self._rebuilder = rebuilder
self._items = {} self._items = {}
@ -49,6 +49,9 @@ class ExpiresDict(object):
return self.get(key) is not None return self.get(key) is not None
def _rebuild(self): def _rebuild(self):
if self._rebuilder is None:
return self._items
items = self._rebuilder() items = self._rebuilder()
self._items = items self._items = items
return items return items