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

Possibility to configure RestAPI caching operations. #183

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Internal:
- Update configuration files.
[plone devs] (93e1ab65)

New features:

- Add possibily to configure caching operations for RestAPI endpoints. @folix-01


4.1.2 (2023-10-28)
------------------
Expand Down
59 changes: 59 additions & 0 deletions src/plone/rest/caching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Customized implementation of plone.app.caching.lookup.ContentItemLookup for plone.restapi Services"""

from plone.app.caching.interfaces import IPloneCacheSettings
from plone.app.caching.lookup import ContentItemLookup
from plone.caching.interfaces import IRulesetLookup
from plone.registry.interfaces import IRegistry
from zope.component import queryUtility
from zope.interface import implementer


@implementer(IRulesetLookup)
class RestContentItemLookup(ContentItemLookup):
"""General lookup for RestAPI Services.

1. Look up the published name in the page template mapping (as
PageTemplateLookup does now) and return that if found

2. Attempt to look up a ruleset using z3c.caching.registry.lookup()
and return that if found (this is necessary because this adapter will
override the default lookup in most cases).

3. Get the name of the published object (i.e. the name of the view or
page template).

4. Find the parent of the published object, possibly a content object.

4.1. If the parent is a content object:
4.1.1. Get the default view of the parent content object
4.1.2. If the name of the published object is the same as the default
view of the parent:
4.1.2.1. Otherwise, look up the parent type in the content type mapping
and return that if found
4.1.2.2. Look up a ruleset on the parent object and return if that
matches
"""

def __call__(self):
registry = queryUtility(IRegistry)

if registry is None:
return

ploneCacheSettings = registry.forInterface(IPloneCacheSettings, check=False)

# 2. Get the name of the published object
name = getattr(self.published, "__name__", None)

if name is None:
return

# 3. Look up the published name in the page template mapping
ruleset = (
ploneCacheSettings.templateRulesetMapping
and ploneCacheSettings.templateRulesetMapping.get(name, None)
) or None
if ruleset is not None:
return ruleset

return super().__call__()
6 changes: 6 additions & 0 deletions src/plone/rest/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
provides="plone.rest.interfaces.IShouldAllowAcquiredItemPublication"
/>

<adapter
factory=".caching.RestContentItemLookup"
for="plone.rest.interfaces.IService
*"
/>

</configure>
Loading