From 88786dde498f06feeeec78d90f54325094252802 Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Tue, 11 Jul 2023 13:35:35 +0000 Subject: [PATCH] [IMP] base, website: add an api to populate the cache Part-of: odoo/odoo#119813 --- addons/http_routing/models/ir_http.py | 2 +- addons/website/models/ir_http.py | 16 ++++------------ odoo/tools/cache.py | 10 ++++++++-- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/addons/http_routing/models/ir_http.py b/addons/http_routing/models/ir_http.py index 63ae8c68fbd..9ba68b5e0d5 100644 --- a/addons/http_routing/models/ir_http.py +++ b/addons/http_routing/models/ir_http.py @@ -677,5 +677,5 @@ class IrHttp(models.AbstractModel): new_url = path return new_url or path, endpoint and endpoint[0] - def _rewrite_len(self, website_id, rewrites=None): + def _rewrite_len(self, website_id): return 0 diff --git a/addons/website/models/ir_http.py b/addons/website/models/ir_http.py index c019b88aba5..cd073a94b79 100644 --- a/addons/website/models/ir_http.py +++ b/addons/website/models/ir_http.py @@ -76,22 +76,13 @@ class Http(models.AbstractModel): return adapter.build(endpoint, kw) + (qs and '?%s' % qs or '') @tools.ormcache('website_id', cache='routing') - def _rewrite_len(self, website_id, rewrites=None): - # This little hack allow to store a _rewrite_len in cache if we have the value, - # (Populated during routing map computation) - # but also ensures that we will have the correct value if the cache is not populated. - # The cache key actually does not depends on "rewrites" because this is the information - # we are storing, rewrites actually depends on website_id - if rewrites is None: - rewrites = self._get_rewrites(website_id) + def _rewrite_len(self, website_id): + rewrites = self._get_rewrites(website_id) return len(rewrites) def _get_rewrites(self, website_id): domain = [('redirect_type', 'in', ('308', '404')), '|', ('website_id', '=', False), ('website_id', '=', website_id)] - rewrites = {x.url_from: x for x in self.env['website.rewrite'].sudo().search(domain)} - self._rewrite_len(website_id, rewrites) # optionnal, update value in cache - #self._rewrite_len.add_cache_value(website_id, value=rewrites) - return rewrites + return {x.url_from: x for x in self.env['website.rewrite'].sudo().search(domain)} def _generate_routing_rules(self, modules, converters): if not request: @@ -100,6 +91,7 @@ class Http(models.AbstractModel): website_id = request.website_routing logger.debug("_generate_routing_rules for website: %s", website_id) rewrites = self._get_rewrites(website_id) + self._rewrite_len.cache.add_value(self, website_id, cache_value=len(rewrites)) for url, endpoint in super()._generate_routing_rules(modules, converters): if url in rewrites: diff --git a/odoo/tools/cache.py b/odoo/tools/cache.py index b1939eff696..bd3be43a231 100644 --- a/odoo/tools/cache.py +++ b/odoo/tools/cache.py @@ -59,9 +59,15 @@ class ormcache(object): self.method = method self.determine_key() lookup = decorator(self.lookup, method) - lookup.ormcache = self + lookup.cache = self return lookup + def add_value(self, *args, cache_value=None, **kwargs): + model = args[0] + d, key0, _ = self.lru(model) + key = key0 + self.key(*args, **kwargs) + d[key] = cache_value + def determine_key(self): """ Determine the function that computes a cache key from arguments. """ if self.skiparg is None: @@ -167,7 +173,7 @@ def log_ormcache_stats(sig=None, frame=None): def get_cache_key_counter(bound_method, *args, **kwargs): """ Return the cache, key and stat counter for the given call. """ model = bound_method.__self__ - ormcache = bound_method.ormcache + ormcache = bound_method.cache cache, key0, counter = ormcache.lru(model) key = key0 + ormcache.key(model, *args, **kwargs) return cache, key, counter