Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 07665d7

Browse files
committedFeb 22, 2024
[IMP] util/misc: cache unique functions
Improves caching to not simply cache the result of the first call of a function, but each *unique* (based on args and kwargs, taking into consideration their order) call.
1 parent aa7131e commit 07665d7

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed
 

Diff for: ‎src/util/misc.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030

3131

3232
def _cached(func):
33-
sentinel = object()
33+
func._cache = {}
3434

3535
@functools.wraps(func)
3636
def wrapper(*args, **kwargs):
37-
result = getattr(func, "_result", sentinel)
38-
if result == sentinel:
39-
result = func._result = func(*args, **kwargs)
40-
return result
37+
key = (args, tuple(sorted(kwargs.items())))
4138

42-
return wrapper
39+
if key not in func._cache:
40+
func._cache[key] = func(*args, **kwargs)
41+
return func._cache[key]
4342

43+
return wrapper
4444

4545
# copied from odoo as older OpenERP versions doesn't have it
4646
def str2bool(s, default=None):

0 commit comments

Comments
 (0)
Please sign in to comment.