Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use plone.memoize to cache groupby criteria #88

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ Breaking Change:
- Add idx parameter to display_modifier call, so that we can use the index name to resolve the correct translated taxonomy titles in collective.taxonomy. This means that the display_modifier method in the groupby_modifier adapters needs to expect this parameter too!
[MrTango]

Bug fixes:

- Use plone.memoize to cache groupby criteria
[instification]


3.5.1 (2021-05-26)
----------------
Expand Down
22 changes: 15 additions & 7 deletions src/collective/collectionfilter/vocabularies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from Products.CMFCore.interfaces import ICatalogTool
from collective.collectionfilter import _
from collective.collectionfilter.interfaces import IGroupByCriteria
from collective.collectionfilter.interfaces import IGroupByModifier
Expand All @@ -8,9 +9,11 @@
ReallyUserFriendlyTypesVocabularyFactory,
)
from plone.registry.interfaces import IRegistry
from plone.memoize import ram
from zope.component import getAdapters
from zope.component import getMultiAdapter
from zope.component import getUtility
from zope.component import queryUtility
from zope.globalrequest import getRequest
from zope.i18n import translate
from zope.interface import implementer
Expand Down Expand Up @@ -109,6 +112,16 @@ def translate_portal_type(value, *args, **kwargs):
return term.title if term else value


def _groupby_cache_key(method, self):
portal = plone.api.portal.get()
# cache key is shared across sites, so path is included
site_path = '/'.join(portal.getPhysicalPath())
catalog = queryUtility(ICatalogTool)
if catalog is None:
return site_path
return '%s-%s' % (site_path, str(catalog.getCounter()))
instification marked this conversation as resolved.
Show resolved Hide resolved


@implementer(IGroupByCriteria)
class GroupByCriteria:
"""Global utility for retrieving and manipulating groupby criterias.
Expand All @@ -119,18 +132,13 @@ class GroupByCriteria:

"""

_groupby = None
_groupby = {}
groupby_modify = {}

@property
@ram.cache(_groupby_cache_key)
def groupby(self):

if self._groupby is not None:
# The groupby criteria are used at each IBeforeTraverseEvent - so
# on each request. This has to be fast, so exit early.
return self._groupby
self._groupby = {}

cat = plone.api.portal.get_tool("portal_catalog")
# get catalog metadata schema, but filter out items which cannot be
# used for grouping
Expand Down