-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_utils.py
47 lines (35 loc) · 1.34 KB
/
cache_utils.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
import hashlib
import os
import pickle
from typing import Callable, Any, Coroutine, Optional
from functools import wraps
# Constants
CACHE_DIR = "cache"
def get_video_hash(video_url: str) -> str:
"""Generate a unique hash for the video URL."""
return hashlib.md5(video_url.encode()).hexdigest()
def cache_result(step: str):
"""
Decorator to cache the result of a function.
:param step: The step name to use for the cache key.
"""
def decorator(func: Callable[..., Coroutine[Any, Any, Any]]):
@wraps(func)
async def wrapper(*args, **kwargs):
video_hash = kwargs.get("video_hash")
if not video_hash:
raise ValueError("Missing 'video_hash' argument")
# Create directory for the video hash
video_cache_dir = os.path.join(CACHE_DIR, video_hash)
os.makedirs(video_cache_dir, exist_ok=True)
cache_key = f"{step}.pkl"
cache_path = os.path.join(video_cache_dir, cache_key)
if os.path.exists(cache_path):
with open(cache_path, "rb") as file:
return pickle.load(file)
result = await func(*args, **kwargs)
with open(cache_path, "wb") as file:
pickle.dump(result, file)
return result
return wrapper
return decorator