Merge pull request #3483 from emanuelschuetze/issue3427

Adds elements to restricted data cache only if cache already exists
This commit is contained in:
Emanuel Schütze 2017-11-15 12:05:59 +01:00 committed by GitHub
commit b515e68068
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 2 deletions

View File

@ -99,7 +99,7 @@ def ws_add_site(message: Any) -> None:
output.append(formatted_data)
# Cache restricted data for user
restricted_data_cache.add_element(
restricted_data_cache.update_element(
user_id,
collection.collection_string,
data['id'],
@ -293,7 +293,7 @@ def send_data(message: ChannelMessageFormat) -> None:
for collection_element in collection_elements:
formatted_data = collection_element.as_autoupdate_for_user(user)
if formatted_data['action'] == 'changed':
restricted_data_cache.add_element(
restricted_data_cache.update_element(
user_id or 0,
collection_element.collection_string,
collection_element.id,

View File

@ -363,6 +363,17 @@ class RestrictedDataCache:
base_cache_key = 'restricted_user_cache'
def update_element(self, user_id: int, collection_string: str, id: int, data: object) -> None:
"""
Adds on element to the cache only if the cache exists for the user.
Note: This method is not atomic. So in very rare cases it is possible
that the restricted date cache can become corrupt. The best solution would be to
use a lua script instead. See also #3427.
"""
if self.exists_for_user(user_id):
self.add_element(user_id, collection_string, id, data)
def add_element(self, user_id: int, collection_string: str, id: int, data: object) -> None:
"""
Adds one element to the cache. If the cache does not exists for the user,
@ -413,6 +424,9 @@ class DummyRestrictedDataCache:
Dummy RestrictedDataCache that does nothing.
"""
def update_element(self, user_id: int, collection_string: str, id: int, data: object) -> None:
pass
def add_element(self, user_id: int, collection_string: str, id: int, data: object) -> None:
pass