RoniFinTech commited on
Commit
afc8995
1 Parent(s): 6a0af53
Files changed (1) hide show
  1. 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(func, key_name, ttl_secs=20):
11
- @wraps(func)
12
- async def wrapper(*args, **kwargs):
13
- # Assuming the prompt is the key for caching, change as necessary
14
- key = kwargs.get(key_name)
15
- ttl = timedelta(seconds=ttl_secs)
16
- # Check cache
17
- if key in _cache:
18
- if datetime.now() - _cache_time[key] > ttl:
19
- # Cache has expired
20
- del _cache[key]
21
- del _cache_time[key]
22
- else:
23
- return _cache[key]
24
-
25
- # Call the actual function if not in cache or expired
26
- response = await func(*args, **kwargs)
27
-
28
- # Cache the result
29
- _cache[key] = response
30
- _cache_time[key] = datetime.now()
31
-
32
- return response
33
-
34
- return wrapper
 
 
 
 
 
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