[IMP] website: ir.http route cache for auth public
- remove useless cache clear_prefix - add a route cache attribute when used it will save the rendered page into the ormcache for the specified time, this is only enabled if the user is public and reponse code is 200 - enable it on /page controller for 5min
This commit is contained in:
@@ -70,7 +70,7 @@ class Website(openerp.addons.web.controllers.main.Home):
|
||||
redirect.set_cookie('website_lang', lang)
|
||||
return redirect
|
||||
|
||||
@http.route('/page/<page:page>', type='http', auth="public", website=True)
|
||||
@http.route('/page/<page:page>', type='http', auth="public", website=True, cache=300)
|
||||
def page(self, page, **opt):
|
||||
values = {
|
||||
'path': page,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import traceback
|
||||
|
||||
import werkzeug
|
||||
@@ -160,7 +161,29 @@ class ir_http(orm.AbstractModel):
|
||||
request.context['tz'] = request.session['geoip'].get('time_zone')
|
||||
# bind modified context
|
||||
request.website = request.website.with_context(request.context)
|
||||
resp = super(ir_http, self)._dispatch()
|
||||
|
||||
# cache for auth public
|
||||
cache_time = func.routing.get('cache')
|
||||
cache_enable = cache_time and request.httprequest.method == "GET" and request.website.user_id.id == request.uid
|
||||
cache_response = None
|
||||
if cache_enable:
|
||||
key = (self._name, "cache", request.uid, request.httprequest.full_path)
|
||||
try:
|
||||
r = self.pool.cache[key]
|
||||
if r['time'] + cache_time > time.time():
|
||||
cache_response = openerp.http.Response(r['content'], mimetype=r['mimetype'])
|
||||
else:
|
||||
del self.pool.cache[key]
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
if cache_response:
|
||||
request.cache_save = False
|
||||
resp = cache_response
|
||||
else:
|
||||
request.cache_save = key if cache_enable else False
|
||||
resp = super(ir_http, self)._dispatch()
|
||||
|
||||
if request.website_enabled and cook_lang != request.lang and hasattr(resp, 'set_cookie'):
|
||||
resp.set_cookie('website_lang', request.lang)
|
||||
return resp
|
||||
|
||||
@@ -1379,6 +1379,17 @@ class Root(object):
|
||||
else:
|
||||
response = result
|
||||
|
||||
# save to cache if requested and possible
|
||||
if getattr(request, 'cache_save', False) and response.status_code == 200:
|
||||
response.freeze()
|
||||
r = response.response
|
||||
if isinstance(r, list) and len(r) == 1 and isinstance(r[0], str):
|
||||
request.registry.cache[request.cache_save] = {
|
||||
'content': r[0],
|
||||
'mimetype': response.headers['Content-Type'],
|
||||
'time': time.time(),
|
||||
}
|
||||
|
||||
if httprequest.session.should_save:
|
||||
self.session_store.save(httprequest.session)
|
||||
# We must not set the cookie if the session id was specified using a http header or a GET parameter.
|
||||
|
||||
+1
-1
@@ -1811,7 +1811,7 @@ class BaseModel(object):
|
||||
``tools.ormcache`` or ``tools.ormcache_multi``.
|
||||
"""
|
||||
try:
|
||||
self.pool.cache.clear_prefix((self._name,))
|
||||
self.pool.cache.clear()
|
||||
self.pool._any_cache_cleared = True
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
@@ -78,13 +78,9 @@ class ormcache(object):
|
||||
return self.method(*args, **kwargs)
|
||||
|
||||
def clear(self, model, *args):
|
||||
""" Remove *args entry from the cache or all keys if *args is undefined """
|
||||
""" Clear the registry cache """
|
||||
d, key0, _ = self.lru(model)
|
||||
if args:
|
||||
_logger.warn("ormcache.clear arguments are deprecated and ignored "
|
||||
"(while clearing caches on (%s).%s)",
|
||||
model._name, self.method.__name__)
|
||||
d.clear_prefix(key0)
|
||||
d.clear()
|
||||
model.pool._any_cache_cleared = True
|
||||
|
||||
|
||||
|
||||
@@ -118,11 +118,3 @@ class LRU(object):
|
||||
self.d = {}
|
||||
self.first = None
|
||||
self.last = None
|
||||
|
||||
@synchronized()
|
||||
def clear_prefix(self, prefix):
|
||||
""" Remove from `self` all the items with the given `prefix`. """
|
||||
n = len(prefix)
|
||||
for key in self.keys():
|
||||
if key[:n] == prefix:
|
||||
del self[key]
|
||||
|
||||
Reference in New Issue
Block a user