summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2016-06-06 14:17:35 +0100
committerErik Johnston <erik@matrix.org>2016-06-06 14:17:35 +0100
commit778c1fea8b90768d14905fbbf6a24215e69e4b13 (patch)
tree8a6ea1d5ad385edaf9beaa10d17b538f9e45e296 /synapse
parentMerge pull request #836 from matrix-org/erikj/change_event_cache (diff)
parentAdd events to cache when we persist them (diff)
downloadsynapse-778c1fea8b90768d14905fbbf6a24215e69e4b13.tar.xz
Merge pull request #840 from matrix-org/erikj/event_write_through
Add events to cache when we persist them
Diffstat (limited to 'synapse')
-rw-r--r--synapse/storage/events.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/synapse/storage/events.py b/synapse/storage/events.py
index 5db24e86f9..16398dc0a8 100644
--- a/synapse/storage/events.py
+++ b/synapse/storage/events.py
@@ -635,6 +635,8 @@ class EventsStore(SQLBaseStore):
             ],
         )
 
+        self._add_to_cache(txn, events_and_contexts)
+
         if backfilled:
             # Backfilled events come before the current state so we don't need
             # to update the current state table
@@ -676,6 +678,45 @@ class EventsStore(SQLBaseStore):
 
         return
 
+    def _add_to_cache(self, txn, events_and_contexts):
+        to_prefill = []
+
+        rows = []
+        N = 200
+        for i in range(0, len(events_and_contexts), N):
+            ev_map = {
+                e[0].event_id: e[0]
+                for e in events_and_contexts[i:i + N]
+            }
+            if not ev_map:
+                break
+
+            sql = (
+                "SELECT "
+                " e.event_id as event_id, "
+                " r.redacts as redacts,"
+                " rej.event_id as rejects "
+                " FROM events as e"
+                " LEFT JOIN rejections as rej USING (event_id)"
+                " LEFT JOIN redactions as r ON e.event_id = r.redacts"
+                " WHERE e.event_id IN (%s)"
+            ) % (",".join(["?"] * len(ev_map)),)
+
+            txn.execute(sql, ev_map.keys())
+            rows = self.cursor_to_dict(txn)
+            for row in rows:
+                event = ev_map[row["event_id"]]
+                if not row["rejects"] and not row["redacts"]:
+                    to_prefill.append(_EventCacheEntry(
+                        event=event,
+                        redacted_event=None,
+                    ))
+
+        def prefill():
+            for cache_entry in to_prefill:
+                self._get_event_cache.prefill((cache_entry[0].event_id,), cache_entry)
+        txn.call_after(prefill)
+
     def _store_redaction(self, txn, event):
         # invalidate the cache for the redacted event
         txn.call_after(self._invalidate_get_event_cache, event.redacts)