Use page display context as portlet's cache key
authorThierry Florac <thierry.florac@onf.fr>
Fri, 22 Jun 2018 13:00:07 +0200
changeset 135 3bca72c8574d
parent 134 1cee610aecce
child 136 a1aaac7979e9
Use page display context as portlet's cache key
src/pyams_portal/interfaces/__init__.py
src/pyams_portal/portlet.py
src/pyams_portal/views.py
--- a/src/pyams_portal/interfaces/__init__.py	Thu Jun 21 16:38:12 2018 +0200
+++ b/src/pyams_portal/interfaces/__init__.py	Fri Jun 22 13:00:07 2018 +0200
@@ -25,7 +25,7 @@
 from pyams_security.schema import PermissionField
 from pyams_utils.schema import PersistentDict, PersistentList
 from zope.container.constraints import contains
-from zope.interface import invariant, Interface, Attribute, Invalid
+from zope.interface import Interface, Attribute
 from zope.schema import List, TextLine, Object, Int, Bool, Choice
 
 from pyams_portal import _
@@ -158,6 +158,8 @@
 
     target_interface = Attribute("Target interface provided by this renderer")
 
+    use_portlets_cache = Attribute("Can renderer use rendering cache?")
+
 
 PORTLET_RENDERER_SETTINGS_KEY = 'pyams_portal.renderer.settings::{0}'
 
@@ -391,6 +393,9 @@
     """
 
 
+PREVIEW_MODE = 'PREVIEW_MODE'
+
+
 class IPortalPage(Interface):
     """Portal page interface
 
--- a/src/pyams_portal/portlet.py	Thu Jun 21 16:38:12 2018 +0200
+++ b/src/pyams_portal/portlet.py	Fri Jun 22 13:00:07 2018 +0200
@@ -9,7 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_portal.views import PREVIEW_MODE
 
 __docformat__ = 'restructuredtext'
 
@@ -24,8 +23,10 @@
 from pyams_form.interfaces.form import IFormContextPermissionChecker
 from pyams_portal.interfaces import IPortlet, IPortletSettings, IPortletConfiguration, IPortletPreviewer, \
     IPortletRenderer, IPortalPortletsConfiguration, IPortalTemplate, IPortalContext, IPortalPage, \
-    IPortalTemplateConfiguration, MANAGE_TEMPLATE_PERMISSION, IPortletRendererSettings, PORTLET_RENDERER_SETTINGS_KEY
+    IPortalTemplateConfiguration, MANAGE_TEMPLATE_PERMISSION, IPortletRendererSettings, PORTLET_RENDERER_SETTINGS_KEY, \
+    PREVIEW_MODE
 from pyams_utils.interfaces import ICacheKeyValue
+from pyams_utils.interfaces.url import DISPLAY_CONTEXT
 from zope.traversing.interfaces import ITraversable
 
 # import packages
@@ -166,10 +167,12 @@
             return super(PortletPreviewer, self).render()
 
 
-PORTLETS_CACHE_KEY = 'portlet::{portlet}::{context}'
 PORTLETS_CACHE_REGION = 'portlets'
 PORTLETS_CACHE_NAME = 'PyAMS::portlet'
 
+PORTLETS_CACHE_KEY = 'portlet::{portlet}::{context}'
+PORTLETS_CACHE_DISPLAY_CONTEXT_KEY = 'portlet::{portlet}::{context}::{display}'
+
 
 @implementer(IPortletRenderer)
 class PortletRenderer(PortletContentProvider):
@@ -182,6 +185,7 @@
         return PORTLET_RENDERER_SETTINGS_KEY.format(self.settings.renderer)
 
     target_interface = None
+    use_portlets_cache = True
 
     weight = 0
 
@@ -198,12 +202,20 @@
 
     def render(self):
         preview_mode = self.request.annotations.get(PREVIEW_MODE, False)
-        if preview_mode:
+        if preview_mode or not self.use_portlets_cache:
             return super(PortletRenderer, self).render()
         else:
             portlets_cache = get_cache(PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME)
-            cache_key = PORTLETS_CACHE_KEY.format(portlet=ICacheKeyValue(self.settings),
-                                                  context=ICacheKeyValue(self.context))
+            # check for display context
+            display_context = self.request.annotations.get(DISPLAY_CONTEXT)
+            if display_context is None:
+                cache_key = PORTLETS_CACHE_KEY.format(portlet=ICacheKeyValue(self.settings),
+                                                      context=ICacheKeyValue(self.context))
+            else:
+                cache_key = PORTLETS_CACHE_DISPLAY_CONTEXT_KEY.format(portlet=ICacheKeyValue(self.settings),
+                                                                      context=ICacheKeyValue(self.context),
+                                                                      display=ICacheKeyValue(display_context))
+            # load rendered content from cache, or create output and store it in cache
             try:
                 result = portlets_cache.get_value(cache_key)
                 logger.debug("Retrieving portlet content for cache key {0}".format(cache_key))
--- a/src/pyams_portal/views.py	Thu Jun 21 16:38:12 2018 +0200
+++ b/src/pyams_portal/views.py	Fri Jun 22 13:00:07 2018 +0200
@@ -17,7 +17,7 @@
 
 # import interfaces
 from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalTemplateConfiguration, \
-    IPortalPortletsConfiguration, IPortletRenderer, IPortlet
+    IPortalPortletsConfiguration, IPortletRenderer, IPortlet, PREVIEW_MODE
 from pyams_skin.layer import IPyAMSUserLayer
 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
 from pyams_workflow.interfaces import IWorkflowPublicationInfo
@@ -87,9 +87,6 @@
         super(PortalContextIndexPage, self).update()
 
 
-PREVIEW_MODE = 'PREVIEW_MODE'
-
-
 @pagelet_config(name='preview.html', context=IPortalContext, layer=IPyAMSUserLayer,
                 permission=VIEW_SYSTEM_PERMISSION)
 class PortalContextPreviewPage(PortalContextIndexPage):