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 @@ -7,6 +7,11 @@ Changelog
- Added css_modifier to extend css class of a filter item
[agitator]

Bug fixes:

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


3.2.1 (2019-08-07)
------------------
Expand Down
26 changes: 19 additions & 7 deletions src/collective/collectionfilter/vocabularies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collective.collectionfilter.interfaces import IGroupByCriteria
from collective.collectionfilter.interfaces import IGroupByModifier
from collective.collectionfilter.utils import safe_encode
from plone.memoize import ram
from zope.component import getAdapters
from zope.component import getUtility
from zope.interface import implementer
Expand Down Expand Up @@ -53,6 +54,22 @@
LIST_SCALING = ['No Scaling', 'Linear', 'Logarithmic']


# Function for determining the cache key used by GroupByCreteria.
# There should be a separate cache for each site and the cache should be
# invalidated by modification to portal_catalog (changes to the indexes rather
# than changes to cataloged items).
def _groupby_cache_key(method, self):
portal = plone.api.portal.get()
site_path = '/'.join(portal.getPhysicalPath())
cache_key = site_path
# TODO: clear cache if portal_catalog is modified
# cat = portal.portal_catalog
# cat_changed = cat.undoable_transactions()[:1]
# cat_changed = len(cat_changed) > 0 and cat_changed[0]['time'] or ''
# cache_key = site_path + str(cat_changed)
instification marked this conversation as resolved.
Show resolved Hide resolved
return cache_key


@implementer(IGroupByCriteria)
class GroupByCriteria():
"""Global utility for retrieving and manipulating groupby criterias.
Expand All @@ -63,18 +80,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