Skip to content

Commit

Permalink
Clear the resource viewlet cache when changing the resource registry …
Browse files Browse the repository at this point in the history
…or activating an add-on.

This avoids needing a restart before seeing changes when you run in production mode.
Fixes [issue 3505](#3505).
  • Loading branch information
mauritsvanrees committed Apr 14, 2023
1 parent ba1f67c commit 7a5d9ad
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Products/CMFPlone/controlpanel/browser/resourceregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from plone.base import PloneMessageFactory as _
from plone.base.interfaces import IBundleRegistry
from plone.registry.interfaces import IRegistry
from Products.CMFPlone.resources.browser.resource import clear_resource_viewlet_caches
from Products.Five.browser import BrowserView
from Products.statusmessages.interfaces import IStatusMessage
from zope.component import getUtility
Expand Down Expand Up @@ -149,4 +150,5 @@ def process_form(self):
self._switch_cache(False)
else:
raise ValueError("Invalid form data")
clear_resource_viewlet_caches()
self.request.response.redirect(self.request["ACTUAL_URL"])
27 changes: 25 additions & 2 deletions Products/CMFPlone/resources/browser/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
logger = logging.getLogger(__name__)

REQUEST_CACHE_KEY = "_WEBRESOURCE_CACHE_"

SITE_ROOT_CACHE_KEY_PREFIX = "_v_rendered_cache_"
GRACEFUL_DEPENDENCY_REWRITE = {
"plone-base": "plone",
"plone-legacy": "plone",
Expand Down Expand Up @@ -59,7 +59,7 @@ def _cache_attr_name(self, site):
for bundle in e_bundles | d_bundles:
hashtool.update(bundle.encode('utf8'))
hashtool.update(self._user_local_roles(site).encode("utf8"))
return f"_v_renderend_cache_{hashtool.hexdigest()}"
return f"{SITE_ROOT_CACHE_KEY_PREFIX}{hashtool.hexdigest()}"

@property
def _rendered_cache(self):
Expand Down Expand Up @@ -313,3 +313,26 @@ def index(self):
rendered = self.renderer["css"].render()
self._rendered_cache = rendered
return rendered


def clear_resource_viewlet_caches():
"""Remove volatile cache of resource viewlets.
See discussion in https://github.com/plone/Products.CMFPlone/issues/3505
"""
site = getSite()

# I don't trust removing keys from a dict when iterating over this dict,
# so gather them in a list first.
to_remove = [
name for name in site.__dict__
if name.startswith(SITE_ROOT_CACHE_KEY_PREFIX)
]
for name in to_remove:
# The attribute is volatile, meaning it may disappear at any time,
# so we catch errors.
try:
delattr(site, name)
except AttributeError:
pass
logger.info("Cleared resource viewlet caches.")
1 change: 1 addition & 0 deletions Products/CMFPlone/resources/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
i18n_domain="plone.registry">

<include package=".browser" />
<subscriber handler=".eventhandlers.check_registry_update" />

</configure>
16 changes: 16 additions & 0 deletions Products/CMFPlone/resources/eventhandlers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from Products.CMFPlone.resources.browser.resource import clear_resource_viewlet_caches
from Products.GenericSetup.interfaces import IProfileImportedEvent
from zope.component import adapter


@adapter(IProfileImportedEvent)
def check_registry_update(event):
"""Check if a profile import may have updated the configuration registry.
Main concern for now is: the resource registries may have changed.
This means the resource viewlet caches should be cleared.
See discussion in https://github.com/plone/Products.CMFPlone/issues/3505
"""
if not (event.full_import or "plone.app.registry" in event.steps):
return
clear_resource_viewlet_caches()
4 changes: 4 additions & 0 deletions news/3505.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Clear the resource viewlet cache when changing the resource registry or activating an add-on.
This avoids needing a restart before seeing changes when you run in production mode.
Fixes [issue 3505](https://github.com/plone/Products.CMFPlone/issues/3505).
[maurits]

0 comments on commit 7a5d9ad

Please sign in to comment.