summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaul "LeoNerd" Evans <paul@matrix.org>2015-03-25 19:12:16 +0000
committerPaul "LeoNerd" Evans <paul@matrix.org>2015-03-25 19:12:16 +0000
commit953e40f9dc086a47d811d1fe029734b3178266f3 (patch)
tree09c82e2db2e052f9daee0cfcb6d05081e5edfbdc
parentUse FrozenEvent's reject_reason to decide whether to return it; don't include... (diff)
downloadsynapse-953e40f9dc086a47d811d1fe029734b3178266f3.tar.xz
Implement the main getEvent cache using Cache() instead of a custom application of LruCache; also unify its two-level structure into just one
-rw-r--r--synapse/storage/_base.py19
1 files changed, 7 insertions, 12 deletions
diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py
index 919295eabb..5c7bd22e64 100644
--- a/synapse/storage/_base.py
+++ b/synapse/storage/_base.py
@@ -241,10 +241,8 @@ class SQLBaseStore(object):
         self._txn_perf_counters = PerformanceCounters()
         self._get_event_counters = PerformanceCounters()
 
-        self._get_event_cache = LruCache(hs.config.event_cache_size)
-
-        # Pretend the getEventCache is just another named cache
-        caches_by_name["*getEvent*"] = self._get_event_cache
+        self._get_event_cache = Cache("*getEvent*", keylen=3, lru=True,
+                                      max_entries=hs.config.event_cache_size)
 
     def start_profiling(self):
         self._previous_loop_ts = self._clock.time_msec()
@@ -733,7 +731,9 @@ class SQLBaseStore(object):
         return [e for e in events if e]
 
     def _invalidate_get_event_cache(self, event_id):
-        self._get_event_cache.pop(event_id)
+        for check_redacted in (False, True):
+            for get_prev_content in (False, True):
+                self._get_event_cache.invalidate(event_id, check_redacted, get_prev_content)
 
     def _get_event_txn(self, txn, event_id, check_redacted=True,
                        get_prev_content=False, allow_rejected=False):
@@ -745,19 +745,14 @@ class SQLBaseStore(object):
             sql_getevents_timer.inc_by(curr_time - last_time, desc)
             return curr_time
 
-        cache = self._get_event_cache.setdefault(event_id, {})
-
         try:
-            # Separate cache entries for each way to invoke _get_event_txn
-            ret = cache[(check_redacted, get_prev_content)]
-            cache_counter.inc_hits("*getEvent*")
+            ret = self._get_event_cache.get(event_id, check_redacted, get_prev_content)
 
             if allow_rejected or not ret.rejected_reason:
                 return ret
             else:
                 return None
         except KeyError:
-            cache_counter.inc_misses("*getEvent*")
             pass
         finally:
             start_time = update_counter("event_cache", start_time)
@@ -788,7 +783,7 @@ class SQLBaseStore(object):
             get_prev_content=get_prev_content,
             rejected_reason=rejected_reason,
         )
-        cache[(check_redacted, get_prev_content)] = result
+        self._get_event_cache.prefill(event_id, check_redacted, get_prev_content, result)
 
         if allow_rejected or not rejected_reason:
             return result