summary refs log tree commit diff
path: root/synapse/storage/receipts.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2018-07-10 17:21:17 +0100
committerErik Johnston <erik@matrix.org>2018-07-10 17:22:42 +0100
commit05f5dabc10f9d7a4403c9571c12371b2b6dd93f7 (patch)
tree09e09e6ae992cee67f65c7f4d288c72f3dc7be01 /synapse/storage/receipts.py
parentAttempt to include db threads in cpu usage stats (#3496) (diff)
downloadsynapse-05f5dabc10f9d7a4403c9571c12371b2b6dd93f7.tar.xz
Use stream cache in get_linearized_receipts_for_room
This avoids us from uncessarily hitting the database when there has been
no change for the room
Diffstat (limited to '')
-rw-r--r--synapse/storage/receipts.py17
1 files changed, 13 insertions, 4 deletions
diff --git a/synapse/storage/receipts.py b/synapse/storage/receipts.py
index 3738901ea4..401400d927 100644
--- a/synapse/storage/receipts.py
+++ b/synapse/storage/receipts.py
@@ -151,7 +151,6 @@ class ReceiptsWorkerStore(SQLBaseStore):
 
         defer.returnValue([ev for res in results.values() for ev in res])
 
-    @cachedInlineCallbacks(num_args=3, tree=True)
     def get_linearized_receipts_for_room(self, room_id, to_key, from_key=None):
         """Get receipts for a single room for sending to clients.
 
@@ -164,6 +163,16 @@ class ReceiptsWorkerStore(SQLBaseStore):
         Returns:
             list: A list of receipts.
         """
+        if from_key:
+            if not self._receipts_stream_cache.has_entity_changed(room_id, from_key):
+                defer.succeed([])
+
+        return self._get_linearized_receipts_for_room(room_id, to_key, from_key)
+
+    @cachedInlineCallbacks(num_args=3, tree=True)
+    def _get_linearized_receipts_for_room(self, room_id, to_key, from_key=None):
+        """See get_linearized_receipts_for_room
+        """
         def f(txn):
             if from_key:
                 sql = (
@@ -211,7 +220,7 @@ class ReceiptsWorkerStore(SQLBaseStore):
             "content": content,
         }])
 
-    @cachedList(cached_method_name="get_linearized_receipts_for_room",
+    @cachedList(cached_method_name="_get_linearized_receipts_for_room",
                 list_name="room_ids", num_args=3, inlineCallbacks=True)
     def _get_linearized_receipts_for_rooms(self, room_ids, to_key, from_key=None):
         if not room_ids:
@@ -373,7 +382,7 @@ class ReceiptsStore(ReceiptsWorkerStore):
             self.get_receipts_for_user.invalidate, (user_id, receipt_type)
         )
         # FIXME: This shouldn't invalidate the whole cache
-        txn.call_after(self.get_linearized_receipts_for_room.invalidate_many, (room_id,))
+        txn.call_after(self._get_linearized_receipts_for_room.invalidate_many, (room_id,))
 
         txn.call_after(
             self._receipts_stream_cache.entity_has_changed,
@@ -493,7 +502,7 @@ class ReceiptsStore(ReceiptsWorkerStore):
             self.get_receipts_for_user.invalidate, (user_id, receipt_type)
         )
         # FIXME: This shouldn't invalidate the whole cache
-        txn.call_after(self.get_linearized_receipts_for_room.invalidate_many, (room_id,))
+        txn.call_after(self._get_linearized_receipts_for_room.invalidate_many, (room_id,))
 
         self._simple_delete_txn(
             txn,