From cf27ff8fd3476fd2b3b4b8736d7476dfbe75cfff Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Mon, 23 Sep 2019 17:07:47 +0000 Subject: [PATCH] [IMP] http, *: review cache TTL values * base, web Google now recommends a TTL of one year for static contents. We used to use 1 week in almost every case. This commit increases that value to one year for safe resources, like assets bundles which contain a specific hash in the URL which changes if the bundle is recomputed anyway. Note: this commit refactors the code so that both the one week and one year durations are defined in http.py and used by others apps. Loading the library "locale" file used to be done with 10-hours-cache, this has been increased to 1-week-cache by using the http.py STATIC_CACHE var. closes odoo/odoo#37402 Signed-off-by: Quentin Smetz (qsm) --- addons/web/controllers/main.py | 10 +++------- odoo/addons/base/models/ir_http.py | 4 ++-- odoo/http.py | 7 +++++-- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 2d5ce8eada2..355ce5a9418 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -59,11 +59,7 @@ else: env = jinja2.Environment(loader=loader, autoescape=True) env.filters["json"] = json.dumps -# 1 week cache for asset bundles as advised by Google Page Speed -BUNDLE_MAXAGE = 60 * 60 * 24 * 7 - -# 1 year cache for content (menus, translations, static qweb) -CONTENT_MAXAGE = 60 * 60 * 24 * 356 +CONTENT_MAXAGE = http.STATIC_CACHE_LONG # menus, translations, static qweb DBNAME_PATTERN = '^[a-zA-Z0-9][a-zA-Z0-9_.-]+$' @@ -889,7 +885,7 @@ class WebClient(http.Controller): file_open('web/static/lib/moment/locale/%s.js' % code, 'rb') ), content_type='application/javascript; charset=utf-8', - headers=[('Cache-Control', 'max-age=36000')], + headers=[('Cache-Control', 'max-age=%s' % http.STATIC_CACHE)], direct_passthrough=True, ) except IOError: @@ -897,7 +893,7 @@ class WebClient(http.Controller): return request.make_response("", headers=[ ('Content-Type', 'application/javascript'), - ('Cache-Control', 'max-age=36000'), + ('Cache-Control', 'max-age=%s' % http.STATIC_CACHE), ]) @http.route('/web/webclient/qweb/', type='http', auth="none", cors="*") diff --git a/odoo/addons/base/models/ir_http.py b/odoo/addons/base/models/ir_http.py index 64ab86aea9b..cc76a8d0ff9 100644 --- a/odoo/addons/base/models/ir_http.py +++ b/odoo/addons/base/models/ir_http.py @@ -20,7 +20,7 @@ import werkzeug.utils import odoo from odoo import api, http, models, tools, SUPERUSER_ID from odoo.exceptions import AccessDenied, AccessError -from odoo.http import request, STATIC_CACHE, content_disposition +from odoo.http import request, content_disposition from odoo.tools import consteq, pycompat from odoo.tools.mimetypes import guess_mimetype from odoo.modules.module import get_resource_path, get_module_path @@ -393,7 +393,7 @@ class IrHttp(models.AbstractModel): headers.append(('ETag', filehash)) if etag == filehash and status == 200: status = 304 - headers.append(('Cache-Control', 'max-age=%s' % (STATIC_CACHE if unique else 0))) + headers.append(('Cache-Control', 'max-age=%s' % (http.STATIC_CACHE_LONG if unique else 0))) # content-disposition default name if download: headers.append(('Content-Disposition', content_disposition(filename))) diff --git a/odoo/http.py b/odoo/http.py index 9e0da1768fc..a332baeadb2 100644 --- a/odoo/http.py +++ b/odoo/http.py @@ -58,8 +58,11 @@ _logger = logging.getLogger(__name__) rpc_request = logging.getLogger(__name__ + '.rpc.request') rpc_response = logging.getLogger(__name__ + '.rpc.response') -# 1 week cache for statics as advised by Google Page Speed -STATIC_CACHE = 60 * 60 * 24 * 7 +# One week cache for static content (static files in apps, library files, ...) +# Safe resources may use what google page speed recommends (1 year) +# (attachments with unique hash in the URL, ...) +STATIC_CACHE = 3600 * 24 * 7 +STATIC_CACHE_LONG = 3600 * 24 * 365 # To remove when corrected in Babel babel.core.LOCALE_ALIASES['nb'] = 'nb_NO'