Commit 6b08e40c authored by Julien Muchembled's avatar Julien Muchembled

Add a decorator to use transactional cache

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@34251 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 39766508
...@@ -291,3 +291,19 @@ def caching_instance_method(*args, **kw): ...@@ -291,3 +291,19 @@ def caching_instance_method(*args, **kw):
# which the method is called would not be passed as first parameter. # which the method is called would not be passed as first parameter.
return lambda *args, **kw: caching_method(*args, **kw) return lambda *args, **kw: caching_method(*args, **kw)
return wrapped return wrapped
def transactional_cached(key_method=lambda *args: args):
def decorator(function):
key = repr(function)
def wrapper(*args, **kw):
cache = getTransactionalVariable(None).setdefault(key, {})
subkey = key_method(*args, **kw)
try:
return cache[subkey]
except KeyError:
cache[subkey] = result = function(*args, **kw)
return result
wrapper.__doc__ = function.__doc__
wrapper.__name__ = function.__name__
return wrapper
return decorator
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment