From 278d6c05274288fa964bc52fb656a0f5a26a453f Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 23 Feb 2016 11:54:48 +0000 Subject: [PATCH 1/3] Report size of ExpiringCache --- synapse/util/caches/expiringcache.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/synapse/util/caches/expiringcache.py b/synapse/util/caches/expiringcache.py index 62cae9964..138cbefff 100644 --- a/synapse/util/caches/expiringcache.py +++ b/synapse/util/caches/expiringcache.py @@ -13,6 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from synapse.util.caches import cache_counter, caches_by_name + import logging @@ -47,6 +49,8 @@ class ExpiringCache(object): self._cache = {} + caches_by_name[cache_name] = self._cache + def start(self): if not self._expiry_ms: # Don't bother starting the loop if things never expire @@ -72,7 +76,11 @@ class ExpiringCache(object): self._cache.pop(k) def __getitem__(self, key): - entry = self._cache[key] + try: + entry = self._cache[key] + cache_counter.inc_hits(self._cache_name) + finally: + cache_counter.inc_misses(self._cache_name) if self._reset_expiry_on_get: entry.time = self._clock.time_msec() @@ -105,9 +113,12 @@ class ExpiringCache(object): logger.debug( "[%s] _prune_cache before: %d, after len: %d", - self._cache_name, begin_length, len(self._cache.keys()) + self._cache_name, begin_length, len(self._cache) ) + def __len__(self): + return len(self._cache) + class _CacheEntry(object): def __init__(self, time, value): From ff2d7551c738ea6138b7bcd245620d91e80841cf Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 1 Mar 2016 10:59:17 +0000 Subject: [PATCH 2/3] Correct cache miss detection --- synapse/util/caches/expiringcache.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/util/caches/expiringcache.py b/synapse/util/caches/expiringcache.py index 138cbefff..55ba1c08c 100644 --- a/synapse/util/caches/expiringcache.py +++ b/synapse/util/caches/expiringcache.py @@ -79,7 +79,7 @@ class ExpiringCache(object): try: entry = self._cache[key] cache_counter.inc_hits(self._cache_name) - finally: + except KeyError: cache_counter.inc_misses(self._cache_name) if self._reset_expiry_on_get: From 72165e5b779709ea6724f6dd2081027c0818276b Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 1 Mar 2016 11:00:10 +0000 Subject: [PATCH 3/3] Reraise exception --- synapse/util/caches/expiringcache.py | 1 + 1 file changed, 1 insertion(+) diff --git a/synapse/util/caches/expiringcache.py b/synapse/util/caches/expiringcache.py index 55ba1c08c..e863a8f8a 100644 --- a/synapse/util/caches/expiringcache.py +++ b/synapse/util/caches/expiringcache.py @@ -81,6 +81,7 @@ class ExpiringCache(object): cache_counter.inc_hits(self._cache_name) except KeyError: cache_counter.inc_misses(self._cache_name) + raise if self._reset_expiry_on_get: entry.time = self._clock.time_msec()