cache_for_request should take function itself into account too

Previously it only looked at the arguments to the function, but not the
function itself.
This commit is contained in:
Sybren A. Stüvel 2018-01-30 18:19:58 +01:00
parent ca7d528c85
commit 20d80dee61

View File

@ -1,3 +1,4 @@
import collections
import functools import functools
from flask import g from flask import g
@ -13,13 +14,13 @@ def cache_for_request():
@functools.wraps(func) @functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
if not hasattr(g, 'request_level_cache'): if not hasattr(g, 'request_level_cache'):
g.request_level_cache = {} g.request_level_cache = collections.defaultdict(dict)
try: try:
return g.request_level_cache[args] return g.request_level_cache[func][args]
except KeyError: except KeyError:
val = func(*args, **kwargs) val = func(*args, **kwargs)
g.request_level_cache[args] = val g.request_level_cache[func][args] = val
return val return val
return wrapper return wrapper
return decorator return decorator