From 60e87a2cc6da7bceafdc1746e3dbc93b59455323 Mon Sep 17 00:00:00 2001 From: Fabien Meghazi Date: Mon, 18 Aug 2014 15:57:51 +0200 Subject: [PATCH] [IMP] Cache bundles in ir.attachments instead of a LRU Forward ported from 8.0's commit: 8402625 --- openerp/addons/base/ir/ir_qweb.py | 48 ++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/openerp/addons/base/ir/ir_qweb.py b/openerp/addons/base/ir/ir_qweb.py index 7860066b835..c0b5388d8b1 100644 --- a/openerp/addons/base/ir/ir_qweb.py +++ b/openerp/addons/base/ir/ir_qweb.py @@ -1005,7 +1005,6 @@ class AssetNotFound(AssetError): pass class AssetsBundle(object): - cache = openerp.tools.lru.LRU(32) rx_css_import = re.compile("(@import[^;{]+;?)", re.M) rx_preprocess_imports = re.compile("""(@import\s?['"]([^'"]+)['"](;?))""") rx_css_split = re.compile("\/\*\! ([a-f0-9-]+) \*\/") @@ -1113,22 +1112,16 @@ class AssetsBundle(object): return hashlib.sha1(check).hexdigest() def js(self): - key = 'js_%s' % self.xmlid - if key in self.cache and self.cache[key][0] != self.version: - # Invalidate cache on version mismach - self.cache.pop(key) - if key not in self.cache: - content =';\n'.join(asset.minify() for asset in self.javascripts) - self.cache[key] = (self.version, content) - return self.cache[key][1] + content = self.get_cache('js') + if content is None: + content = ';\n'.join(asset.minify() for asset in self.javascripts) + self.set_cache('js', content) + return content def css(self): """Generate css content from given bundle""" - key = 'css_%s' % self.xmlid - if key in self.cache and self.cache[key][0] != self.version: - # Invalidate cache on version mismach - self.cache.pop(key) - if key not in self.cache: + content = self.get_cache('css') + if content is None: content = self.preprocess_css() if self.css_errors: @@ -1147,9 +1140,32 @@ class AssetsBundle(object): content = u'\n'.join(matches) if self.css_errors: return content - self.cache[key] = (self.version, content) + self.set_cache('css', content) - return self.cache[key][1] + return content + + def get_cache(self, type): + content = None + domain = [('url', '=', '/web/%s/%s/%s' % (type, self.xmlid, self.version))] + bundle = self.registry['ir.attachment'].search_read(self.cr, self.uid, domain, ['datas'], context=self.context) + if bundle: + content = bundle[0]['datas'].decode('base64') + return content + + def set_cache(self, type, content): + ira = self.registry['ir.attachment'] + url_prefix = '/web/%s/%s/' % (type, self.xmlid) + # Invalidate previous caches + oids = ira.search(self.cr, self.uid, [('url', '=like', url_prefix + '%')], context=self.context) + if oids: + ira.unlink(self.cr, openerp.SUPERUSER_ID, oids, context=self.context) + url = url_prefix + self.version + ira.create(self.cr, openerp.SUPERUSER_ID, dict( + datas=content.encode('utf8').encode('base64'), + type='binary', + name=url, + url=url, + ), context=self.context) def css_message(self, message): # '\A' == css content carriage return