Make caching on disk a decorator, and update implementations for Tex and Text mobjects

This commit is contained in:
Grant Sanderson
2024-12-05 10:09:15 -06:00
parent 89ddfadf6b
commit 43821ab2ba
6 changed files with 104 additions and 74 deletions

View File

@ -1,22 +1,38 @@
from __future__ import annotations
import os
from diskcache import Cache
from contextlib import contextmanager
from functools import wraps
from manimlib.utils.directories import get_cache_dir
from manimlib.utils.simple_functions import hash_string
from typing import TYPE_CHECKING
if TYPE_CHECKING:
T = TypeVar('T')
CACHE_SIZE = 1e9 # 1 Gig
_cache = Cache(get_cache_dir(), size_limit=CACHE_SIZE)
def get_cached_value(key, value_func, message=""):
cache = Cache(get_cache_dir(), size_limit=CACHE_SIZE)
def cache_on_disk(func: Callable[..., T]) -> Callable[..., T]:
@wraps(func)
def wrapper(*args, **kwargs):
key = hash_string("".join(map(str, [func.__name__, args, kwargs])))
value = _cache.get(key)
if value is None:
# print(f"Executing {func.__name__}({args[0]}, ...)")
value = func(*args, **kwargs)
_cache.set(key, value)
return value
return wrapper
value = cache.get(key)
if value is None:
with display_during_execution(message):
value = value_func()
cache.set(key, value)
return value
def clear_cache():
_cache.clear()
@contextmanager