[IMP] base, website: add an api to populate the cache
Part-of: odoo/odoo#119813
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
+8
-2
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user