This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Use stream cache in get_linearized_receipts_for_room #3505
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Reduce database consumption when processing large numbers of receipts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,9 @@ def get_linearized_receipts_for_rooms(self, room_ids, to_key, from_key=None): | |
""" | ||
room_ids = set(room_ids) | ||
|
||
if from_key: | ||
if from_key is not None: | ||
# Only ask the database about rooms where there have been new | ||
# receipts added since `from_key` | ||
room_ids = yield self._receipts_stream_cache.get_entities_changed( | ||
room_ids, from_key | ||
) | ||
|
@@ -151,7 +153,6 @@ def get_linearized_receipts_for_rooms(self, room_ids, to_key, from_key=None): | |
|
||
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 +165,18 @@ def get_linearized_receipts_for_room(self, room_id, to_key, from_key=None): | |
Returns: | ||
list: A list of receipts. | ||
""" | ||
if from_key is not None: | ||
# Check the cache first to see if any new receipts have been added | ||
# since`from_key`. If not we can no-op. | ||
if not self._receipts_stream_cache.has_entity_changed(room_id, from_key): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we have a comment here please? why is this a sensible thing to do? |
||
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 +224,7 @@ def f(txn): | |
"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 +386,7 @@ def insert_linearized_receipt_txn(self, txn, room_id, receipt_type, | |
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 +506,7 @@ def insert_graph_receipt_txn(self, txn, room_id, receipt_type, | |
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, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you clarify that this is a Deferred[list] ?