Skip to content

Commit e625847

Browse files
committed
fixes
1 parent 4e3060e commit e625847

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

botasaurus/decorator_helpers.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,38 @@
22
from functools import wraps
33
import traceback
44
from time import sleep, time
5+
from datetime import timedelta
56
from .utils import is_errors_instance
67

78
from .cache import Cache, _get,CacheMissException, _has, _get_cache_path, _create_cache_directory_if_not_exists
89
from .dontcache import is_dont_cache
910

10-
def cache(_func=None, *, cache=True):
11+
def cache(_func=None, *, cache=True, expires_in: Optional[timedelta] = None):
12+
"""
13+
Cache decorator to store and retrieve function results.
14+
15+
Args:
16+
cache: Enable caching (True/False/'REFRESH' to force refresh)
17+
expires_in: Optional timedelta specifying how long the cache is valid.
18+
Example: , timedelta(days=7), timedelta(minutes=30)
19+
If the cached item is older than this duration, it will be treated as expired
20+
and the function will be executed again.
21+
22+
Example:
23+
from datetime import timedelta
24+
25+
@cache(cache=True, expires_in=timedelta(hours=24))
26+
def fetch_data(url):
27+
# This result will be cached for 24 hours
28+
return expensive_operation(url)
29+
"""
1130
def decorator_cache(func):
1231
@wraps(func)
1332
def wrapper_cache(*args, **kwargs):
14-
nonlocal cache
33+
nonlocal cache, expires_in
1534

1635
cache = kwargs.pop("cache", cache)
36+
expires_in = kwargs.pop("expires_in", expires_in)
1737

1838
if cache:
1939
_create_cache_directory_if_not_exists(func)
@@ -23,7 +43,23 @@ def run_cache(*args, **kwargs):
2343
path = _get_cache_path(func, [args, kwargs])
2444
if _has(path):
2545
try:
26-
return _get(path)
46+
# Check if cache has expired
47+
if expires_in is not None:
48+
if Cache.is_item_older_than(
49+
func,
50+
[args, kwargs],
51+
days=expires_in.days,
52+
seconds=expires_in.seconds,
53+
microseconds=expires_in.microseconds,
54+
):
55+
# Cache expired, delete it and re-execute
56+
Cache.delete(func, [args, kwargs])
57+
else:
58+
# Cache is still valid, return it
59+
return _get(path)
60+
else:
61+
# No expiration set, return cache
62+
return _get(path)
2763
except CacheMissException:
2864
pass
2965

0 commit comments

Comments
 (0)