mirror of
https://github.com/3b1b/manim.git
synced 2025-07-28 12:32:36 +08:00
Make caching on disk a decorator, and update implementations for Tex and Text mobjects
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user