Spaces:
Runtime error
Runtime error
Commit
·
afc8995
1
Parent(s):
6a0af53
cache-fix
Browse files- cache/local_cache.py +29 -25
cache/local_cache.py
CHANGED
@@ -7,28 +7,32 @@ _cache = {}
|
|
7 |
_cache_time = {}
|
8 |
|
9 |
|
10 |
-
def ttl_cache(
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
7 |
_cache_time = {}
|
8 |
|
9 |
|
10 |
+
def ttl_cache(key_name, ttl_secs=20):
|
11 |
+
|
12 |
+
def decorator(func):
|
13 |
+
@wraps(func)
|
14 |
+
async def wrapper(*args, **kwargs):
|
15 |
+
# Assuming the prompt is the key for caching, change as necessary
|
16 |
+
key = kwargs.get(key_name)
|
17 |
+
ttl = timedelta(seconds=ttl_secs)
|
18 |
+
# Check cache
|
19 |
+
if key in _cache:
|
20 |
+
if datetime.now() - _cache_time[key] > ttl:
|
21 |
+
# Cache has expired
|
22 |
+
del _cache[key]
|
23 |
+
del _cache_time[key]
|
24 |
+
else:
|
25 |
+
return _cache[key]
|
26 |
+
|
27 |
+
# Call the actual function if not in cache or expired
|
28 |
+
response = await func(*args, **kwargs)
|
29 |
+
|
30 |
+
# Cache the result
|
31 |
+
_cache[key] = response
|
32 |
+
_cache_time[key] = datetime.now()
|
33 |
+
|
34 |
+
return response
|
35 |
+
|
36 |
+
return wrapper
|
37 |
+
|
38 |
+
return decorator
|