Skip to content

Commit

Permalink
Cache the events from the 'Upcoming Events' portlet. refs gh-351
Browse files Browse the repository at this point in the history
  • Loading branch information
frapell committed Mar 25, 2022
1 parent c5217c9 commit 15696d9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions news/351.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cache the events from the 'Upcoming Events' portlet
[frapell]
42 changes: 42 additions & 0 deletions plone/app/event/portlets/portlet_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from plone.app.portlets.portlets import base
from plone.app.querystring import queryparser
from plone.app.uuid.utils import uuidToObject
from plone.memoize import ram
from plone.memoize.compress import xhtml_compress
from plone.portlets.interfaces import IPortletDataProvider
from plone.registry.interfaces import IRegistry
Expand All @@ -28,6 +29,46 @@
from zope.interface import implementer


def _render_events_cachekey(method, self):
# XXX: Since the events portlet shows the upcoming events, I will simply get
# the most recently modified event using the same logic, and return its uuid
# and last modified date as key. If there is a change, then the most expensive
# computation can be performed

site = getSite()
pc = site.portal_catalog

query = {}
data = self.data
if data.state:
query['review_state'] = data.state

query.update(self.request.get('contentFilter', {}))
if ICollection and ICollection.providedBy(self.search_base):
# Whatever sorting is defined, we're overriding it.
query = queryparser.parseFormquery(
self.search_base, self.search_base.query,
sort_on='start', sort_order=None
)
else:
if self.search_base_path:
query['path'] = {'query': self.search_base_path}

query['portal_type'] = 'Event'
query['sort_on'] = 'start'
query['sort_order'] = 'asc'
query['sort_limit'] = data.count

events = pc(**query)
uuid = ""
modified = 0
for event in events:
uuid += event.UID
modified += event.modified.asdatetime().timestamp()

return (uuid, modified)


class IEventsPortlet(IPortletDataProvider):

count = schema.Int(
Expand Down Expand Up @@ -141,6 +182,7 @@ def available(self):
return self.data.count > 0 and len(self.events)

@property
@ram.cache(_render_events_cachekey)
def events(self):
context = aq_inner(self.context)
data = self.data
Expand Down

0 comments on commit 15696d9

Please sign in to comment.