From d9cffe7e2e742375c200897896ea1c6a9fd11834 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Aug 25 2017 08:50:55 +0000 Subject: [PATCH 1/3] Set some defaults for the autodoc Sphinx extension --- diff --git a/docs/api/utils.rst b/docs/api/utils.rst index 773eb40..5338291 100644 --- a/docs/api/utils.rst +++ b/docs/api/utils.rst @@ -5,23 +5,18 @@ View utils ---------- .. automodule:: hubs.utils.views - :members: Caching utils ------------- .. automodule:: hubs.utils.cache - :members: Package database utils ---------------------- .. automodule:: hubs.utils.packages - :members: Datagrepper utils ----------------- .. automodule:: hubs.utils.datagrepper - :members: - diff --git a/docs/api/widgets.rst b/docs/api/widgets.rst index 021bf49..f828673 100644 --- a/docs/api/widgets.rst +++ b/docs/api/widgets.rst @@ -5,40 +5,31 @@ Widgets ======= .. automodule:: hubs.widgets - :members: - :show-inheritance: Widgets registry ---------------- .. automodule:: hubs.widgets.registry - :members: - :show-inheritance: Widget class ------------ .. automodule:: hubs.widgets.base - :members: - :show-inheritance: + :private-members: Widget parameter validators --------------------------- .. automodule:: hubs.widgets.validators - :members: - :show-inheritance: Widget view ----------- .. automodule:: hubs.widgets.view - :members: - :show-inheritance: + :private-members: Caching ------- .. automodule:: hubs.widgets.caching - :members: - :show-inheritance: + :private-members: diff --git a/docs/conf.py b/docs/conf.py index 30c5281..194929b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -119,6 +119,9 @@ pygments_style = 'sphinx' # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = True +# Default flags for the autodoc extension: show public members (some private members need to be overriiden +autodoc_default_flags = ['members', 'show-inheritance'] + # -- Options for HTML output ---------------------------------------------- From 45084e03264d0cbd773eb8cbb1795459b7d2deff Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Aug 25 2017 08:50:55 +0000 Subject: [PATCH 2/3] Add a switch for config-independant cached functions --- diff --git a/hubs/widgets/caching.py b/hubs/widgets/caching.py index c531cc0..2476ca8 100644 --- a/hubs/widgets/caching.py +++ b/hubs/widgets/caching.py @@ -34,16 +34,22 @@ class CachedFunction(object): To call the function, instantiate the class and execute it. You may also call the :py:meth:`.get_data` method. - Args: + Attributes: instance (hubs.models.Widget): The widget instance. + invalidate_on_config_change (bool): ``True`` if the cached result + depends on the widget configuration, ``False`` otherwise. Defaults + to ``True``. E.g: the function retrieves a lot of raw data from an + external service, and config-dependant filtering is done in the + view calling the function. """ + invalidate_on_config_change = True + def __init__(self, instance): self.instance = instance def execute(self): - """ - The function to cache. + """The function to cache. This is the main method, it must be implemented. @@ -53,11 +59,10 @@ class CachedFunction(object): raise NotImplementedError def get_cache_key(self): - return "|".join([ - str(self.instance.idx), - self.__class__.__name__, - json.dumps(self.instance.config), - ]).encode('utf-8') + key_elements = [str(self.instance.idx), self.__class__.__name__] + if self.invalidate_on_config_change: + key_elements.append(json.dumps(self.instance.config)) + return "|".join(key_elements).encode('utf-8') def get_data(self): key = self.get_cache_key() diff --git a/hubs/widgets/halp/functions.py b/hubs/widgets/halp/functions.py index 662081d..0191710 100644 --- a/hubs/widgets/halp/functions.py +++ b/hubs/widgets/halp/functions.py @@ -25,6 +25,7 @@ class GetRequests(CachedFunction): them. """ + invalidate_on_config_change = False TOPIC = "org.fedoraproject.prod.meetbot.meeting.item.help" def execute(self): From d2a300e741de607b5be91bfd547e8a7f10d84966 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Aug 25 2017 08:50:55 +0000 Subject: [PATCH 3/3] Add a subclassable function to signal a value should not be cached --- diff --git a/hubs/widgets/caching.py b/hubs/widgets/caching.py index 2476ca8..6a499a5 100644 --- a/hubs/widgets/caching.py +++ b/hubs/widgets/caching.py @@ -68,10 +68,24 @@ class CachedFunction(object): key = self.get_cache_key() log.debug("Accessing cache key %s", key) return cache.get_or_create( - key, self.execute) + key, self.execute, should_cache_fn=self._should_cache) __call__ = get_data + def _should_cache(self, value): + """Indicates if the result should be cached. + + This function which will receive the value returned by + :py:meth:`.execute`, and will then return ``True`` or ``False``, + indicating if the value should actually be cached or not. If it returns + ``False``, the value is still returned, but isn't cached. E.g.:: + + return (value is not None) + + Defaults to ``True``. + """ + return True + def should_invalidate(self, message): """ Tell the cache invalidator if the received message should invalidate