Skip to content

Commit afdab20

Browse files
Improve cached decorator to support class based methods
1 parent 645fc33 commit afdab20

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

flask_apidoc/utils.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
Helpers.
33
"""
44

5+
import functools
6+
57

68
def cached(f):
79
"""
810
Cache decorator for functions taking one or more arguments.
911
:param f: The function to be cached.
1012
:return: The cached value.
1113
"""
12-
class CachedDict(dict):
13-
def __init__(self, f):
14-
self.f = f
15-
16-
def __call__(self, *args):
17-
return self[args]
1814

19-
def __missing__(self, key):
20-
ret = self[key] = self.f(*key)
21-
return ret
15+
cache = f.cache = {}
2216

23-
return CachedDict(f)
17+
@functools.wraps(f)
18+
def decorator(*args, **kwargs):
19+
key = str(args) + str(kwargs)
20+
if key not in cache:
21+
cache[key] = f(*args, **kwargs)
22+
return cache[key]
23+
return decorator

0 commit comments

Comments
 (0)