-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathdecorators.py
49 lines (42 loc) · 1.52 KB
/
decorators.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from functools import WRAPPER_ASSIGNMENTS, wraps
from .jobs import FunctionJob
def cacheback(
lifetime=None,
fetch_on_miss=None,
cache_alias=None,
job_class=None,
task_options=None,
**job_class_kwargs
):
"""
Decorate function to cache its return value.
:lifetime: How long to cache items for
:fetch_on_miss: Whether to perform a synchronous fetch when no cached
result is found
:cache_alias: The Django cache alias to store the result into.
:job_class: The class to use for running the cache refresh job. Defaults
using the FunctionJob.
:job_class_kwargs: Any extra kwargs to pass to job_class constructor.
Useful with custom job_class implementations.
"""
if job_class is None:
job_class = FunctionJob
job = job_class(
lifetime=lifetime,
fetch_on_miss=fetch_on_miss,
cache_alias=cache_alias,
task_options=task_options,
**job_class_kwargs
)
def _wrapper(fn):
# using available_attrs to work around http://bugs.python.org/issue3445
@wraps(fn, assigned=WRAPPER_ASSIGNMENTS)
def __wrapper(*args, **kwargs):
return job.get(fn, *args, **kwargs)
# Assign reference to unwrapped function so that we can access it
# later without descending into infinite regress.
__wrapper.fn = fn
# Assign reference to job so we can use the full Job API
__wrapper.job = job
return __wrapper
return _wrapper