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