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,21 +171,27 @@ class ConversionCacheMixin: ...@@ -171,21 +171,27 @@ 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():
temp_conversion_dict = getattr(aq_base(self), 'temp_conversion_data')
return temp_conversion_dict.has_key(cache_id)
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 True
except KeyError:
return False return False
security.declareProtected(Permissions.ModifyPortalContent, 'setConversion') security.declareProtected(Permissions.ModifyPortalContent, 'setConversion')
...@@ -232,8 +238,9 @@ class ConversionCacheMixin: ...@@ -232,8 +238,9 @@ 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))
except KeyError:
return 0 return 0
def generateCacheId(self, **kw): def generateCacheId(self, **kw):
...@@ -1149,10 +1156,11 @@ class Document(PermanentURLMixIn, XMLObject, UrlMixIn, ConversionCacheMixin, Sna ...@@ -1149,10 +1156,11 @@ 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
except KeyError:
kw['format'] = 'html' kw['format'] = 'html'
mime, html = self.convert(**kw) mime, html = self.convert(**kw)
return html return html
...@@ -1166,10 +1174,11 @@ class Document(PermanentURLMixIn, XMLObject, UrlMixIn, ConversionCacheMixin, Sna ...@@ -1166,10 +1174,11 @@ 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
except KeyError:
kw['format'] = 'html' kw['format'] = 'html'
mime, html = self.convert(**kw) mime, html = self.convert(**kw)
return self._stripHTML(str(html)) return self._stripHTML(str(html))
......
...@@ -500,9 +500,10 @@ class OOoDocument(PermanentURLMixIn, File, ConversionCacheMixin): ...@@ -500,9 +500,10 @@ 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)
except KeyError:
return PermanentURLMixIn._getExtensibleContent(self, request, name) return PermanentURLMixIn._getExtensibleContent(self, request, name)
# Base format implementation # Base format implementation
......
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