2024-12-05 10:09:15 -06:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-12-04 19:51:01 -06:00
|
|
|
import os
|
|
|
|
from diskcache import Cache
|
|
|
|
from contextlib import contextmanager
|
2024-12-05 10:09:15 -06:00
|
|
|
from functools import wraps
|
2024-12-04 19:51:01 -06:00
|
|
|
|
2024-12-04 20:50:42 -06:00
|
|
|
from manimlib.utils.directories import get_cache_dir
|
2024-12-05 10:09:15 -06:00
|
|
|
from manimlib.utils.simple_functions import hash_string
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
T = TypeVar('T')
|
2024-12-04 20:50:42 -06:00
|
|
|
|
2024-12-04 19:51:01 -06:00
|
|
|
|
|
|
|
CACHE_SIZE = 1e9 # 1 Gig
|
2024-12-05 10:09:15 -06:00
|
|
|
_cache = Cache(get_cache_dir(), size_limit=CACHE_SIZE)
|
|
|
|
|
2024-12-04 19:51:01 -06:00
|
|
|
|
2024-12-05 10:09:15 -06:00
|
|
|
def cache_on_disk(func: Callable[..., T]) -> Callable[..., T]:
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
2024-12-05 15:05:37 -06:00
|
|
|
key = hash_string(f"{func.__name__}{args}{kwargs}")
|
2024-12-05 10:09:15 -06:00
|
|
|
value = _cache.get(key)
|
|
|
|
if value is None:
|
|
|
|
value = func(*args, **kwargs)
|
|
|
|
_cache.set(key, value)
|
|
|
|
return value
|
|
|
|
return wrapper
|
2024-12-04 19:51:01 -06:00
|
|
|
|
|
|
|
|
2024-12-05 10:09:15 -06:00
|
|
|
def clear_cache():
|
|
|
|
_cache.clear()
|