-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a basic caching system for some datanommer values
Signed-off-by: Aurélien Bompard <[email protected]>
- Loading branch information
Showing
7 changed files
with
157 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
from contextlib import suppress | ||
|
||
import datanommer.models | ||
from dogpile.cache import make_region | ||
from dogpile.cache.api import NO_VALUE | ||
from dogpile.cache.util import kwarg_function_key_generator | ||
from fedora_messaging.message import Message | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
cache = make_region() | ||
|
||
|
||
class CachedValue: | ||
|
||
def __init__(self): | ||
self._key_generator = kwarg_function_key_generator(None, self.compute) | ||
|
||
def _get_key(self, **kwargs): | ||
return self._key_generator(**kwargs).replace(" ", "|") | ||
|
||
def get(self, **kwargs): | ||
key = self._get_key(**kwargs) | ||
return cache.get_or_create(key, creator=self.compute, creator_args=((), kwargs)) | ||
|
||
def set(self, *, value, **kwargs): | ||
key = self._get_key(**kwargs) | ||
log.debug("Updating cached value %s with key %s", self.__class__.__name__, key) | ||
cache.set(key, value) | ||
|
||
def compute(self, **kwargs): | ||
raise NotImplementedError | ||
|
||
def on_message(self, message: Message): | ||
raise NotImplementedError | ||
|
||
|
||
class TopicUserCount(CachedValue): | ||
|
||
def compute(self, *, topic, username): | ||
total, pages, query = datanommer.models.Message.grep( | ||
topics=[topic], users=[username], defer=True | ||
) | ||
return total | ||
|
||
def on_message(self, message: Message): | ||
for username in message.usernames: | ||
current_value = self.get(topic=message.topic, username=username) | ||
if current_value == NO_VALUE: | ||
continue # Don't update the value if no one has ever requested it | ||
self.set(topic=message.topic, username=username, value=current_value + 1) | ||
|
||
@classmethod | ||
def is_applicable(cls, search_kwargs): | ||
"""Return whether we can use this cached value for this datanommer query""" | ||
query_keys = set(search_kwargs.keys()) | ||
with suppress(KeyError): | ||
query_keys.remove("rows_per_page") | ||
if query_keys != {"topics", "users"}: | ||
return False | ||
return len(search_kwargs["topics"]) == 1 and len(search_kwargs["users"]) == 1 | ||
|
||
|
||
def on_message(message: Message): | ||
TopicUserCount().on_message(message) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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