Commit bd11a50d authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

no need to call hasConversion() before getConversion() because it does mostly...

no need to call hasConversion() before getConversion() because it does mostly the same thing and waste of time. use try ... except KeyError instead.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@28367 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 0f0c1a54
...@@ -171,22 +171,28 @@ class ConversionCacheMixin: ...@@ -171,22 +171,28 @@ class ConversionCacheMixin:
security.declareProtected(Permissions.View, 'hasConversion') security.declareProtected(Permissions.View, 'hasConversion')
def hasConversion(self, **kw): def hasConversion(self, **kw):
""" """
If you want to get conversion cache value if exists, please write
the code like:
try:
mime, data = getConversion(**kw)
except KeyError:
...
instead of:
if self.hasConversion(**kw):
mime, data = self.getConversion(**kw)
else:
...
for better performance.
""" """
self.updateConversionCache() try:
cache_id = self.generateCacheId(**kw) self.getConversion(**kw)
if self.isTempObject(): return True
temp_conversion_dict = getattr(aq_base(self), 'temp_conversion_data') except KeyError:
return temp_conversion_dict.has_key(cache_id) return False
cache_factory = self._getCacheFactory()
plugin_list = cache_factory.getCachePluginList()
#If there is no plugin list return False OR one them is doesn't contain
#cache_id for givent scope, return False
for cache_plugin in plugin_list:
if cache_plugin.has_key(self.getPath(), DEFAULT_CACHE_SCOPE):
cache_entry = cache_plugin.get(self.getPath(), DEFAULT_CACHE_SCOPE)
if cache_entry.getValue().has_key(cache_id):
return True
return False
security.declareProtected(Permissions.ModifyPortalContent, 'setConversion') security.declareProtected(Permissions.ModifyPortalContent, 'setConversion')
def setConversion(self, data, mime=None, calculation_time=None, **kw): def setConversion(self, data, mime=None, calculation_time=None, **kw):
...@@ -232,9 +238,10 @@ class ConversionCacheMixin: ...@@ -232,9 +238,10 @@ class ConversionCacheMixin:
def getConversionSize(self, **kw): def getConversionSize(self, **kw):
""" """
""" """
if self.hasConversion(**kw): try:
return len(self.getConversion(**kw)) return len(self.getConversion(**kw))
return 0 except KeyError:
return 0
def generateCacheId(self, **kw): def generateCacheId(self, **kw):
"""Generate proper cache id based on **kw. """Generate proper cache id based on **kw.
...@@ -1149,13 +1156,14 @@ class Document(PermanentURLMixIn, XMLObject, UrlMixIn, ConversionCacheMixin, Sna ...@@ -1149,13 +1156,14 @@ class Document(PermanentURLMixIn, XMLObject, UrlMixIn, ConversionCacheMixin, Sna
""" """
if not self.hasBaseData(): if not self.hasBaseData():
raise ConversionError('This document has not been processed yet.') raise ConversionError('This document has not been processed yet.')
if self.hasConversion(format='base-html'): try:
# FIXME: no substitution may occur in this case. # FIXME: no substitution may occur in this case.
mime, data = self.getConversion(format='base-html') mime, data = self.getConversion(format='base-html')
return data return data
kw['format'] = 'html' except KeyError:
mime, html = self.convert(**kw) kw['format'] = 'html'
return html mime, html = self.convert(**kw)
return html
security.declareProtected(Permissions.View, 'asStrippedHTML') security.declareProtected(Permissions.View, 'asStrippedHTML')
def asStrippedHTML(self, **kw): def asStrippedHTML(self, **kw):
...@@ -1166,13 +1174,14 @@ class Document(PermanentURLMixIn, XMLObject, UrlMixIn, ConversionCacheMixin, Sna ...@@ -1166,13 +1174,14 @@ class Document(PermanentURLMixIn, XMLObject, UrlMixIn, ConversionCacheMixin, Sna
""" """
if not self.hasBaseData(): if not self.hasBaseData():
return '' return ''
if self.hasConversion(format='stripped-html'): # XXX this is redundant since we never set it try:
# FIXME: no substitution may occur in this case. # FIXME: no substitution may occur in this case.
mime, data = self.getConversion(format='stripped-html') mime, data = self.getConversion(format='stripped-html')
return data return data
kw['format'] = 'html' except KeyError:
mime, html = self.convert(**kw) kw['format'] = 'html'
return self._stripHTML(str(html)) mime, html = self.convert(**kw)
return self._stripHTML(str(html))
def _guessEncoding(self, string): def _guessEncoding(self, string):
""" """
......
...@@ -500,10 +500,11 @@ class OOoDocument(PermanentURLMixIn, File, ConversionCacheMixin): ...@@ -500,10 +500,11 @@ class OOoDocument(PermanentURLMixIn, File, ConversionCacheMixin):
archive_file.close() archive_file.close()
def _getExtensibleContent(self, request, name): def _getExtensibleContent(self, request, name):
if self.hasConversion(format='_embedded', file_name=name): try:
mime, data = self.getConversion(format='_embedded', file_name=name) mime, data = self.getConversion(format='_embedded', file_name=name)
return OFSFile(name, name, data, content_type=mime).__of__(self.aq_parent) return OFSFile(name, name, data, content_type=mime).__of__(self.aq_parent)
return PermanentURLMixIn._getExtensibleContent(self, request, name) except KeyError:
return PermanentURLMixIn._getExtensibleContent(self, request, name)
# Base format implementation # Base format implementation
security.declareProtected(Permissions.AccessContentsInformation, 'hasBaseData') security.declareProtected(Permissions.AccessContentsInformation, 'hasBaseData')
......
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