aboutsummaryrefslogtreecommitdiff
path: root/cachetools/decorators.py
diff options
context:
space:
mode:
Diffstat (limited to 'cachetools/decorators.py')
-rw-r--r--cachetools/decorators.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/cachetools/decorators.py b/cachetools/decorators.py
index 217b9a8..3e78603 100644
--- a/cachetools/decorators.py
+++ b/cachetools/decorators.py
@@ -8,11 +8,15 @@ def cached(cache, key=hashkey, lock=None):
results in a cache.
"""
+
def decorator(func):
if cache is None:
+
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
+
elif lock is None:
+
def wrapper(*args, **kwargs):
k = key(*args, **kwargs)
try:
@@ -25,7 +29,9 @@ def cached(cache, key=hashkey, lock=None):
except ValueError:
pass # value too large
return v
+
else:
+
def wrapper(*args, **kwargs):
k = key(*args, **kwargs)
try:
@@ -40,7 +46,9 @@ def cached(cache, key=hashkey, lock=None):
return cache.setdefault(k, v)
except ValueError:
return v # value too large
+
return functools.update_wrapper(wrapper, func)
+
return decorator
@@ -49,8 +57,10 @@ def cachedmethod(cache, key=hashkey, lock=None):
callable that saves results in a cache.
"""
+
def decorator(method):
if lock is None:
+
def wrapper(self, *args, **kwargs):
c = cache(self)
if c is None:
@@ -66,7 +76,9 @@ def cachedmethod(cache, key=hashkey, lock=None):
except ValueError:
pass # value too large
return v
+
else:
+
def wrapper(self, *args, **kwargs):
c = cache(self)
if c is None:
@@ -84,5 +96,7 @@ def cachedmethod(cache, key=hashkey, lock=None):
return c.setdefault(k, v)
except ValueError:
return v # value too large
+
return functools.update_wrapper(wrapper, method)
+
return decorator