Handle cache for header and footer
authorThierry Florac <thierry.florac@onf.fr>
Thu, 05 Jul 2018 10:04:14 +0200
changeset 801 c7585a24ca8b
parent 800 2b5460ecb254
child 802 3c71b1d0a370
Handle cache for header and footer
src/pyams_content/features/footer/skin/__init__.py
src/pyams_content/features/header/skin/__init__.py
--- a/src/pyams_content/features/footer/skin/__init__.py	Thu Jul 05 08:53:43 2018 +0200
+++ b/src/pyams_content/features/footer/skin/__init__.py	Thu Jul 05 10:04:14 2018 +0200
@@ -14,15 +14,21 @@
 
 
 # import standard library
+import logging
+logger = logging.getLogger('PyAMS (content)')
 
 # import interfaces
 from pyams_content.features.footer.interfaces import IFooterTarget, IFooterRenderer, IFooterSettings
 from pyams_content.features.renderer.interfaces import HIDDEN_RENDERER_NAME
+from pyams_portal.interfaces import PREVIEW_MODE
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import ICacheKeyValue
 from pyams_utils.interfaces.url import DISPLAY_CONTEXT
 
 # import packages
+from pyams_cache.beaker import get_cache
 from pyams_content.features.renderer.skin import BaseContentRenderer
+from pyams_portal.portlet import PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME, PORTLETS_CACHE_KEY
 from pyams_utils.adapter import adapter_config
 from pyams_utils.traversing import get_parent
 from pyramid.decorator import reify
@@ -46,6 +52,23 @@
             settings = IFooterSettings(settings.parent)
         return settings.settings
 
+    def render(self):
+        preview_mode = self.request.annotations.get(PREVIEW_MODE, False)
+        if preview_mode:
+            return super(BaseFooterRenderer, self).render()
+        else:
+            portlets_cache = get_cache(PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME)
+            cache_key = PORTLETS_CACHE_KEY.format(portlet=self.name,
+                                                  context=ICacheKeyValue(self.settings))
+            try:
+                result = portlets_cache.get_value(cache_key)
+                logger.debug("Retrieving header content from cache key {0}".format(cache_key))
+            except KeyError:
+                result = super(BaseFooterRenderer, self).render()
+                portlets_cache.set_value(cache_key, result)
+                logger.debug("Storing header content for cache key {0}".format(cache_key))
+            return result
+
 
 @adapter_config(name=HIDDEN_RENDERER_NAME, context=(IFooterTarget, IPyAMSLayer), provides=IFooterRenderer)
 class HiddenFooterRenderer(BaseFooterRenderer):
--- a/src/pyams_content/features/header/skin/__init__.py	Thu Jul 05 08:53:43 2018 +0200
+++ b/src/pyams_content/features/header/skin/__init__.py	Thu Jul 05 10:04:14 2018 +0200
@@ -14,15 +14,21 @@
 
 
 # import standard library
+import logging
+logger = logging.getLogger('PyAMS (content)')
 
 # import interfaces
 from pyams_content.features.header.interfaces import IHeaderTarget, IHeaderRenderer, IHeaderSettings
 from pyams_content.features.renderer.interfaces import HIDDEN_RENDERER_NAME
+from pyams_portal.interfaces import PREVIEW_MODE
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import ICacheKeyValue
 from pyams_utils.interfaces.url import DISPLAY_CONTEXT
 
 # import packages
+from pyams_cache.beaker import get_cache
 from pyams_content.features.renderer.skin import BaseContentRenderer
+from pyams_portal.portlet import PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME, PORTLETS_CACHE_KEY
 from pyams_utils.adapter import adapter_config
 from pyams_utils.traversing import get_parent
 from pyramid.decorator import reify
@@ -46,6 +52,23 @@
             settings = IHeaderSettings(settings.parent)
         return settings.settings
 
+    def render(self):
+        preview_mode = self.request.annotations.get(PREVIEW_MODE, False)
+        if preview_mode:
+            return super(BaseHeaderRenderer, self).render()
+        else:
+            portlets_cache = get_cache(PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME)
+            cache_key = PORTLETS_CACHE_KEY.format(portlet=self.name,
+                                                  context=ICacheKeyValue(self.settings))
+            try:
+                result = portlets_cache.get_value(cache_key)
+                logger.debug("Retrieving header content from cache key {0}".format(cache_key))
+            except KeyError:
+                result = super(BaseHeaderRenderer, self).render()
+                portlets_cache.set_value(cache_key, result)
+                logger.debug("Storing header content for cache key {0}".format(cache_key))
+            return result
+
 
 @adapter_config(name=HIDDEN_RENDERER_NAME, context=(IHeaderTarget, IPyAMSLayer), provides=IHeaderRenderer)
 class HiddenHeaderRenderer(BaseHeaderRenderer):