forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_util.py
32 lines (27 loc) · 1.08 KB
/
cache_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from superset import tables_cache
from flask import request
def view_cache_key(*unused_args, **unused_kwargs):
args_hash = hash(frozenset(request.args.items()))
return 'view/{}/{}'.format(request.path, args_hash)
def memoized_func(timeout=5 * 60, key=view_cache_key):
"""Use this decorator to cache functions that have predefined first arg.
memoized_func uses simple_cache and stored the data in memory.
Key is a callable function that takes function arguments and
returns the caching key.
"""
def wrap(f):
if tables_cache:
def wrapped_f(cls, *args, **kwargs):
cache_key = key(*args, **kwargs)
o = tables_cache.get(cache_key)
if not kwargs['force'] and o is not None:
return o
o = f(cls, *args, **kwargs)
tables_cache.set(cache_key, o, timeout=timeout)
return o
else:
# noop
def wrapped_f(cls, *args, **kwargs):
return f(cls, *args, **kwargs)
return wrapped_f
return wrap