Skip to content

Commit e6f797c

Browse files
committed
[ADD] base_registry_cache_custom
1 parent 6b21bbb commit e6f797c

File tree

10 files changed

+674
-0
lines changed

10 files changed

+674
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
=====================
2+
Registry Custom Cache
3+
=====================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:6c8e306992a72a18f30d78f12456cec5b1802ae1fde1c9179f122297a61c4562
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
18+
:alt: License: LGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
20+
:target: https://github.com/OCA/server-tools/tree/17.0/base_registry_cache_custom
21+
:alt: OCA/server-tools
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/server-tools-17-0/server-tools-17-0-base_registry_cache_custom
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=17.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module allows adding custom caches to the DBs' registries.
32+
33+
**Table of contents**
34+
35+
.. contents::
36+
:local:
37+
38+
Configuration
39+
=============
40+
41+
Make sure you run Odoo with
42+
``--load=base,web,base_registry_cache_custom`` or using the Odoo
43+
configuration file:
44+
45+
.. code:: ini
46+
47+
[options]
48+
(...)
49+
server_wide_modules = base,web,base_registry_cache_custom
50+
51+
Usage
52+
=====
53+
54+
If you need to create a custom cache, create a new module and:
55+
56+
- add this module as its dependency
57+
- add a ``post_load`` hook like this:
58+
59+
.. code:: python
60+
61+
from odoo.addons.base_registry_cache_custom.registry import add_custom_cache
62+
63+
64+
def post_load():
65+
add_custom_cache(name="my_cache", size=256)
66+
67+
If you make use of multiple caches, and some of them should be
68+
invalidated when another one gets invalidated itself, use the ``deps``
69+
argument:
70+
71+
.. code:: python
72+
73+
from odoo.addons.base_registry_cache_custom.registry import add_custom_cache
74+
75+
76+
def post_load():
77+
add_custom_cache(name="my_cache", size=256, deps=["my_cache.subcache"])
78+
add_custom_cache(name="my_cache.subcache", size=128)
79+
80+
Bug Tracker
81+
===========
82+
83+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
84+
In case of trouble, please check there if your issue has already been reported.
85+
If you spotted it first, help us to smash it by providing a detailed and welcomed
86+
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20base_registry_cache_custom%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
87+
88+
Do not contact contributors directly about support or help with technical issues.
89+
90+
Credits
91+
=======
92+
93+
Authors
94+
-------
95+
96+
* Camptocamp
97+
98+
Contributors
99+
------------
100+
101+
- Silvio Gregorini <[email protected]>
102+
103+
Maintainers
104+
-----------
105+
106+
This module is maintained by the OCA.
107+
108+
.. image:: https://odoo-community.org/logo.png
109+
:alt: Odoo Community Association
110+
:target: https://odoo-community.org
111+
112+
OCA, or the Odoo Community Association, is a nonprofit organization whose
113+
mission is to support the collaborative development of Odoo features and
114+
promote its widespread use.
115+
116+
This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/17.0/base_registry_cache_custom>`_ project on GitHub.
117+
118+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

base_registry_cache_custom/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2025 Camptocamp SA
2+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
3+
4+
{
5+
"name": "Registry Custom Cache",
6+
"version": "17.0.1.0.0",
7+
"category": "Tools",
8+
"summary": "Add custom caches to Odoo registries",
9+
"author": "Camptocamp,Odoo Community Association (OCA)",
10+
"license": "LGPL-3",
11+
"website": "https://github.com/OCA/server-tools",
12+
"depends": ["base"],
13+
"installable": True,
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["whool"]
3+
build-backend = "whool.buildapi"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Make sure you run Odoo with ``--load=base,web,base_registry_cache_custom`` or using the Odoo configuration file:
2+
3+
``` ini
4+
[options]
5+
(...)
6+
server_wide_modules = base,web,base_registry_cache_custom
7+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Silvio Gregorini \<<[email protected]>\>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This module allows adding custom caches to the DBs' registries.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
If you need to create a custom cache, create a new module and:
2+
3+
- add this module as its dependency
4+
- add a `post_load` hook like this:
5+
6+
```python
7+
from odoo.addons.base_registry_cache_custom.registry import add_custom_cache
8+
9+
10+
def post_load():
11+
add_custom_cache(name="my_cache", size=256)
12+
```
13+
14+
If you make use of multiple caches, and some of them should be invalidated when another
15+
one gets invalidated itself, use the `deps` argument:
16+
17+
```python
18+
from odoo.addons.base_registry_cache_custom.registry import add_custom_cache
19+
20+
21+
def post_load():
22+
add_custom_cache(name="my_cache", size=256, deps=["my_cache.subcache"])
23+
add_custom_cache(name="my_cache.subcache", size=128)
24+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2025 Camptocamp SA
2+
# License LGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html)
3+
4+
import logging
5+
import typing
6+
7+
from odoo.modules import registry
8+
from odoo.tools.lru import LRU
9+
from odoo.tools.misc import OrderedSet
10+
11+
_logger = logging.getLogger(__name__)
12+
13+
14+
def add_custom_cache(name: str, size: int, deps: typing.Iterable[str] = None):
15+
"""Adds a custom cache into the Odoo registry
16+
17+
:param name: name of the custom cache to add
18+
:param size: size of the custom cache (must be > 0)
19+
:param deps: iterable of other cache names, to be registered for cache
20+
invalidation dependencies; ``cache_name`` is always included as first item
21+
"""
22+
_logger.info(f"Adding cache '{name}' to registries...")
23+
24+
# ``registry._REGISTRY_CACHES`` is used by ``registry.Registry.init()`` to
25+
# initialize registries' caches (attr ``__cache``)
26+
registry._REGISTRY_CACHES[name] = size
27+
28+
# ``registry._CACHES_BY_KEY`` is used by a variety of ``registry.Registry`` methods
29+
# to handle caches dependencies
30+
# NB: use an ``OrderedSet`` to avoid duplicates while keeping the dependency order
31+
# (with the main cache as first item anyway), then convert to tuple for consistency
32+
# w/ the standard ``registry._CACHES_BY_KEY`` structure
33+
registry._CACHES_BY_KEY[name] = tuple(OrderedSet([name] + list(deps or [])))
34+
35+
# Update existing registries by:
36+
# - adding the custom cache to the registry (name-mangle: avoid AttributeError)
37+
# - setting up the proper signaling workflow
38+
# ``registry.Registry.registries`` is a class attribute that returns an
39+
# ``odoo.tools.lru.LRU`` object that maps each DB name to its ``registry.Registry``
40+
# object through its variable ``d`` (which is an ``OrderedDict`` object)
41+
for db_name, db_registry in registry.Registry.registries.d.items():
42+
_logger.info(f"Adding cache '{name}' to '{db_name}' registry")
43+
db_registry._Registry__caches[name] = LRU(size)
44+
db_registry.setup_signaling()

0 commit comments

Comments
 (0)