Commit 53360432 authored by Rafael Monnerat's avatar Rafael Monnerat Committed by Xiaowu Zhang

Implement RAMCache for Forms and Field

  Use OFS.Cache.Cachable to cache result outcome (html) of forms and
  fields for ERP5Form.

  Include Cacheable setup on ZMIField and extend render_htmlgrid.
parent 8a0c9241
...@@ -179,6 +179,9 @@ class WebSection(Domain, DocumentExtensibleTraversableMixin): ...@@ -179,6 +179,9 @@ class WebSection(Domain, DocumentExtensibleTraversableMixin):
if self.REQUEST.get(self.web_section_key, MARKER) is MARKER: if self.REQUEST.get(self.web_section_key, MARKER) is MARKER:
self.REQUEST[self.web_section_key] = self.getPhysicalPath() self.REQUEST[self.web_section_key] = self.getPhysicalPath()
self.REQUEST.set('current_web_section', self) self.REQUEST.set('current_web_section', self)
# Populate essecial caching
self.REQUEST.set('current_web_section_url', self.absolute_url())
self.REQUEST.set('current_web_site_url', self.getWebSiteValue().absolute_url())
if not self.REQUEST.get('editable_mode') and not self.REQUEST.get('ignore_layout'): if not self.REQUEST.get('editable_mode') and not self.REQUEST.get('ignore_layout'):
document = None document = None
if self.isDefaultPageDisplayed(): if self.isDefaultPageDisplayed():
......
...@@ -36,6 +36,7 @@ from Products.Formulator.XMLToForm import XMLToForm ...@@ -36,6 +36,7 @@ from Products.Formulator.XMLToForm import XMLToForm
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
from Products.CMFCore.utils import _checkPermission, getToolByName from Products.CMFCore.utils import _checkPermission, getToolByName
from Products.CMFCore.exceptions import AccessControl_Unauthorized from Products.CMFCore.exceptions import AccessControl_Unauthorized
from AccessControl.SecurityManagement import getSecurityManager
from Products.ERP5Type import PropertySheet, Permissions from Products.ERP5Type import PropertySheet, Permissions
from urllib import quote from urllib import quote
...@@ -648,10 +649,34 @@ class ERP5Form(ZMIForm, ZopePageTemplate): ...@@ -648,10 +649,34 @@ class ERP5Form(ZMIForm, ZopePageTemplate):
here=obj, here=obj,
context=obj, context=obj,
) )
return pt.pt_render(extra_context=extra_context) LOG('ERP5Form/Form.py:__call__', 0, '%s %s ' % (self.pt, extra_context))
security = getSecurityManager()
# Retrieve the value from the cache.
keyset = None
if self.ZCacheable_isCachingEnabled():
# Prepare a cache key.
keyset = {'here': self._getContext(),
'options': kwargs}
LOG('ERP5Form/Form.py:__call__', 0, '%s %s %s' % (self.pt, keyset, self.ZCacheable_isCachingEnabled()))
result = self.ZCacheable_get(keywords=keyset)
if result is not None:
# Got a cached value.
return result
# Execute the template in a new security context.
LOG('ERP5Form/Form.py:__call__', 0, '%s %s %s' % (self.pt, keyset, self.ZCacheable_isCachingEnabled()))
result = pt.pt_render(extra_context=extra_context)
if keyset is not None:
# Store the result in the cache.
self.ZCacheable_set(result, keywords=keyset)
return result
def _exec(self, bound_names, args, kw): def _exec(self, bound_names, args, kw):
pt = getattr(self,self.pt) pt = getattr(self,self.pt)
LOG('ERP5Form/Form.py:_exec', 0, '%s %s ' % (self.pt, bound_names))
return pt._exec(self, bound_names, args, kw) return pt._exec(self, bound_names, args, kw)
def manage_renameObject(self, id, new_id, REQUEST=None): def manage_renameObject(self, id, new_id, REQUEST=None):
......
...@@ -460,6 +460,7 @@ class ZMIField( ...@@ -460,6 +460,7 @@ class ZMIField(
Acquisition.Implicit, Acquisition.Implicit,
Persistent, Persistent,
OFS.SimpleItem.Item, OFS.SimpleItem.Item,
OFS.Cache.Cacheable,
Field, Field,
): ):
"""Base class for a field implemented as a Python (file) product. """Base class for a field implemented as a Python (file) product.
...@@ -480,6 +481,9 @@ class ZMIField( ...@@ -480,6 +481,9 @@ class ZMIField(
'help':('Formulator', 'fieldMessages.txt')}, 'help':('Formulator', 'fieldMessages.txt')},
{'label':'Test', 'action':'fieldTest', {'label':'Test', 'action':'fieldTest',
'help':('Formulator', 'fieldTest.txt')}, 'help':('Formulator', 'fieldTest.txt')},
{'label': 'Cache', 'action': 'ZCacheable_manage',
'filter': OFS.Cache.filterCacheTab,
'help': ('OFSP', 'Cacheable-properties.stx')}
) + OFS.SimpleItem.SimpleItem.manage_options ) + OFS.SimpleItem.SimpleItem.manage_options
security.declareProtected('View', 'title') security.declareProtected('View', 'title')
...@@ -705,6 +709,34 @@ class ZMIField( ...@@ -705,6 +709,34 @@ class ZMIField(
return self return self
getRecursiveTemplateField = getTemplateField getRecursiveTemplateField = getTemplateField
security.declareProtected('View', 'render_htmlgrid')
def render_htmlgrid(self, value=None, REQUEST=None, key=None, render_prefix=None, key_prefix=None):
"""
render_htmlgrid returns a list of tuple (title, html render)
"""
# Retrieve the value from the cache.
keyset = None
if self.ZCacheable_isCachingEnabled():
# Prepare a cache key.
keyset = {'here': self,
'value': value,
'render_prefix' : render_prefix,
'key_prefix': key_prefix}
LOG("ZMIField.render_htmlgrid", 0, keyset)
result = self.ZCacheable_get(keywords=keyset)
if result is not None:
# Got a cached value.
return result
result = Field.render_htmlgrid(self, value,
REQUEST, key,
render_prefix, key_prefix)
if keyset is not None:
# Store the result in the cache.
self.ZCacheable_set(result, keywords=keyset)
return result
InitializeClass(ZMIField) InitializeClass(ZMIField)
PythonField = ZMIField # NOTE: for backwards compatibility PythonField = ZMIField # NOTE: for backwards compatibility
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment