summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorEric Eastwood <erice@element.io>2022-09-22 17:18:34 -0500
committerEric Eastwood <erice@element.io>2022-09-22 17:18:34 -0500
commit2162ab5607082f030ea9a4cd2cbcf78854e6f724 (patch)
tree6118c58bf5f0f2a0e7ec948764be6f5a45b4f7e6 /synapse
parentMerge branch 'develop' into madlittlemods/13856-fix-have-seen-events-not-bein... (diff)
downloadsynapse-2162ab5607082f030ea9a4cd2cbcf78854e6f724.tar.xz
Invalidate cache like #13796
Copying what https://github.com/matrix-org/synapse/pull/13796
is doing
Diffstat (limited to 'synapse')
-rw-r--r--synapse/storage/controllers/persist_events.py34
-rw-r--r--synapse/storage/databases/main/events.py25
2 files changed, 26 insertions, 33 deletions
diff --git a/synapse/storage/controllers/persist_events.py b/synapse/storage/controllers/persist_events.py
index 73380e295d..501dbbc990 100644
--- a/synapse/storage/controllers/persist_events.py
+++ b/synapse/storage/controllers/persist_events.py
@@ -43,7 +43,7 @@ from prometheus_client import Counter, Histogram
 from twisted.internet import defer
 
 from synapse.api.constants import EventTypes, Membership
-from synapse.events import EventBase, relation_from_event
+from synapse.events import EventBase
 from synapse.events.snapshot import EventContext
 from synapse.logging.context import PreserveLoggingContext, make_deferred_yieldable
 from synapse.logging.opentracing import (
@@ -431,22 +431,6 @@ class EventsPersistenceStorageController:
             else:
                 events.append(event)
 
-                # We expect events to be persisted by this point and this makes
-                # mypy happy about `stream_ordering` not being optional below
-                assert event.internal_metadata.stream_ordering is not None
-                # Invalidate related caches after we persist a new event
-                relation = relation_from_event(event)
-                self.main_store._invalidate_caches_for_event(
-                    stream_ordering=event.internal_metadata.stream_ordering,
-                    event_id=event.event_id,
-                    room_id=event.room_id,
-                    etype=event.type,
-                    state_key=event.state_key if hasattr(event, "state_key") else None,
-                    redacts=event.redacts,
-                    relates_to=relation.parent_id if relation else None,
-                    backfilled=backfilled,
-                )
-
         return (
             events,
             self.main_store.get_room_max_token(),
@@ -479,22 +463,6 @@ class EventsPersistenceStorageController:
         replaced_event = replaced_events.get(event.event_id)
         if replaced_event:
             event = await self.main_store.get_event(replaced_event)
-        else:
-            # We expect events to be persisted by this point and this makes
-            # mypy happy about `stream_ordering` not being optional below
-            assert event.internal_metadata.stream_ordering is not None
-            # Invalidate related caches after we persist a new event
-            relation = relation_from_event(event)
-            self.main_store._invalidate_caches_for_event(
-                stream_ordering=event.internal_metadata.stream_ordering,
-                event_id=event.event_id,
-                room_id=event.room_id,
-                etype=event.type,
-                state_key=event.state_key if hasattr(event, "state_key") else None,
-                redacts=event.redacts,
-                relates_to=relation.parent_id if relation else None,
-                backfilled=backfilled,
-            )
 
         event_stream_id = event.internal_metadata.stream_ordering
         # stream ordering should have been assigned by now
diff --git a/synapse/storage/databases/main/events.py b/synapse/storage/databases/main/events.py
index 1b54a2eb57..1f1a7b7545 100644
--- a/synapse/storage/databases/main/events.py
+++ b/synapse/storage/databases/main/events.py
@@ -410,6 +410,31 @@ class PersistEventsStore:
         assert min_stream_order
         assert max_stream_order
 
+        # Once the txn completes, invalidate all of the relevant caches. Note that we do this
+        # up here because it captures all the events_and_contexts before any are removed.
+        for event, _ in events_and_contexts:
+            self.store.invalidate_get_event_cache_after_txn(txn, event.event_id)
+            if event.redacts:
+                self.store.invalidate_get_event_cache_after_txn(txn, event.redacts)
+
+            relates_to = None
+            relation = relation_from_event(event)
+            if relation:
+                relates_to = relation.parent_id
+
+            assert event.internal_metadata.stream_ordering is not None
+            txn.call_after(
+                self.store._invalidate_caches_for_event,
+                event.internal_metadata.stream_ordering,
+                event.event_id,
+                event.room_id,
+                event.type,
+                getattr(event, "state_key", None),
+                event.redacts,
+                relates_to,
+                backfilled=False,
+            )
+
         self._update_forward_extremities_txn(
             txn,
             new_forward_extremities=new_forward_extremities,