This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
Cache check to see if device exists #2201
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.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
from twisted.internet import defer | ||
|
||
from synapse.api.errors import StoreError | ||
from ._base import SQLBaseStore | ||
from ._base import SQLBaseStore, Cache | ||
from synapse.util.caches.descriptors import cached, cachedList, cachedInlineCallbacks | ||
|
||
|
||
|
@@ -29,6 +29,14 @@ class DeviceStore(SQLBaseStore): | |
def __init__(self, hs): | ||
super(DeviceStore, self).__init__(hs) | ||
|
||
# Map of (user_id, device_id) -> bool. If there is an entry that implies | ||
# the device exists. | ||
self.device_id_exists_cache = Cache( | ||
name="device_id_exists", | ||
keylen=2, | ||
max_entries=10000, | ||
) | ||
|
||
self._clock.looping_call( | ||
self._prune_old_outbound_device_pokes, 60 * 60 * 1000 | ||
) | ||
|
@@ -54,6 +62,10 @@ def store_device(self, user_id, device_id, | |
defer.Deferred: boolean whether the device was inserted or an | ||
existing device existed with that ID. | ||
""" | ||
key = (user_id, device_id) | ||
if self.device_id_exists_cache.get(key, None): | ||
defer.returnValue(False) | ||
|
||
try: | ||
inserted = yield self._simple_insert( | ||
"devices", | ||
|
@@ -65,6 +77,7 @@ def store_device(self, user_id, device_id, | |
desc="store_device", | ||
or_ignore=True, | ||
) | ||
self.device_id_exists_cache.prefill(key, True) | ||
defer.returnValue(inserted) | ||
except Exception as e: | ||
logger.error("store_device with device_id=%s(%r) user_id=%s(%r)" | ||
|
@@ -102,12 +115,14 @@ def delete_device(self, user_id, device_id): | |
Returns: | ||
defer.Deferred | ||
""" | ||
return self._simple_delete_one( | ||
self._simple_delete_one( | ||
table="devices", | ||
keyvalues={"user_id": user_id, "device_id": device_id}, | ||
desc="delete_device", | ||
) | ||
|
||
self.device_id_exists_cache.invalidate((user_id, device_id)) | ||
|
||
def delete_devices(self, user_id, device_ids): | ||
"""Deletes several devices. | ||
|
||
|
@@ -117,13 +132,15 @@ def delete_devices(self, user_id, device_ids): | |
Returns: | ||
defer.Deferred | ||
""" | ||
return self._simple_delete_many( | ||
self._simple_delete_many( | ||
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. missing |
||
table="devices", | ||
column="device_id", | ||
iterable=device_ids, | ||
keyvalues={"user_id": user_id}, | ||
desc="delete_devices", | ||
) | ||
for device_id in device_ids: | ||
self.device_id_exists_cache.invalidate((user_id, device_id)) | ||
|
||
def update_device(self, user_id, device_id, new_display_name=None): | ||
"""Update a device. | ||
|
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.
missing
yield