Skip to content

Commit 414dded

Browse files
committed
-
1 parent c4bf887 commit 414dded

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

python_toolbox/caching/cache.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# todo: examine thread-safety
1010

1111
import functools
12+
import datetime as datetime_module
1213

1314
from python_toolbox import decorator_tools
1415

@@ -23,7 +24,7 @@ class CLEAR_ENTIRE_CACHE(object):
2324

2425

2526
@decorator_tools.helpful_decorator_builder
26-
def cache(max_size=infinity):
27+
def cache(max_size=infinity, time_limit=None):
2728
'''
2829
Cache a function, saving results so they won't have to be computed again.
2930
@@ -54,6 +55,19 @@ def f(a, b=2):
5455
# compile a function accordingly, so functions with a simple argspec won't
5556
# have to go through so much shit. update: probably it will help only for
5657
# completely argumentless function. so do one for those.
58+
59+
if time_limit is not None:
60+
if not isinstance(time_limit, datetime_module.timedelta):
61+
try:
62+
time_limit = datetime_module.timedelta(**time_limit)
63+
except Exception:
64+
raise TypeError(
65+
'`time_limit` must be either a `timedelta` object or a '
66+
'dict of keyword arguments for constructing a '
67+
'`timedelta` object.'
68+
)
69+
assert isinstance(time_limit, datetime_module.timedelta)
70+
5771

5872
def decorator(function):
5973

0 commit comments

Comments
 (0)