From 178c8e7cb07a78d2c55de751cb91dee5ae1f30cc Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 1 May 2018 13:25:46 +0300 Subject: [PATCH] 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) --- data/cache/impl.py | 2 +- util/expiresdict.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/data/cache/impl.py b/data/cache/impl.py index a851dbe07..982e950e9 100644 --- a/data/cache/impl.py +++ b/data/cache/impl.py @@ -40,7 +40,7 @@ class NoopDataModelCache(DataModelCache): class InMemoryDataModelCache(DataModelCache): """ Implementation of the data model cache backed by an in-memory dictionary. """ def __init__(self): - self.cache = ExpiresDict(rebuilder=lambda: {}) + self.cache = ExpiresDict() def retrieve(self, cache_key, loader, should_cache=is_not_none): not_found = [None] diff --git a/util/expiresdict.py b/util/expiresdict.py index 952cad26b..62c87b9d9 100644 --- a/util/expiresdict.py +++ b/util/expiresdict.py @@ -17,9 +17,9 @@ class ExpiresEntry(object): class ExpiresDict(object): """ 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 - 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._items = {} @@ -49,6 +49,9 @@ class ExpiresDict(object): return self.get(key) is not None def _rebuild(self): + if self._rebuilder is None: + return self._items + items = self._rebuilder() self._items = items return items