Skip to content

Commit

Permalink
[IMP] util/misc: cache unique functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
diagnoza committed Feb 22, 2024
1 parent aa7131e commit 07665d7
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@


def _cached(func):
sentinel = object()
func._cache = {}

@functools.wraps(func)
def wrapper(*args, **kwargs):
result = getattr(func, "_result", sentinel)
if result == sentinel:
result = func._result = func(*args, **kwargs)
return result
key = (args, tuple(sorted(kwargs.items())))

return wrapper
if key not in func._cache:
func._cache[key] = func(*args, **kwargs)
return func._cache[key]

return wrapper

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

0 comments on commit 07665d7

Please sign in to comment.