Allow for a configurable cache location

This commit is contained in:
Grant Sanderson
2024-12-04 20:50:42 -06:00
parent 0c385e820f
commit 89ddfadf6b
4 changed files with 16 additions and 8 deletions

View File

@ -10,12 +10,12 @@ directories:
vector_images: "" vector_images: ""
# If you want to use sounds, manim will look here to find it. # If you want to use sounds, manim will look here to find it.
sounds: "" sounds: ""
# Manim often generates tex_files or other kinds of serialized data
# to keep from having to generate the same thing too many times. By
# default, these will be stored at tempfile.gettempdir(), e.g. this might
# return whatever is at to the TMPDIR environment variable. If you want to
# specify them elsewhere,
temporary_storage: "" temporary_storage: ""
# For certain object types, especially Tex and Text, manim will save information
# to file to prevent the need to re-compute, e.g. recompiling the latex. By default,
# it stores this saved data to whatever directory appdirs.user_cache_dir("manim") returns,
# but here a user can specify a different cache location
cache: ""
universal_import_line: "from manimlib import *" universal_import_line: "from manimlib import *"
style: style:
tex_template: "default" tex_template: "default"

View File

@ -1,15 +1,15 @@
import appdirs
import os import os
from diskcache import Cache from diskcache import Cache
from contextlib import contextmanager from contextlib import contextmanager
from manimlib.utils.directories import get_cache_dir
CACHE_SIZE = 1e9 # 1 Gig CACHE_SIZE = 1e9 # 1 Gig
def get_cached_value(key, value_func, message=""): def get_cached_value(key, value_func, message=""):
cache_dir = appdirs.user_cache_dir("manim") cache = Cache(get_cache_dir(), size_limit=CACHE_SIZE)
cache = Cache(cache_dir, size_limit=CACHE_SIZE)
value = cache.get(key) value = cache.get(key)
if value is None: if value is None:

View File

@ -1,5 +1,6 @@
import os import os
import tempfile import tempfile
import appdirs
from manimlib.config import get_custom_config from manimlib.config import get_custom_config
from manimlib.config import get_manim_dir from manimlib.config import get_manim_dir
@ -17,6 +18,9 @@ def get_customization():
if not directories["temporary_storage"]: if not directories["temporary_storage"]:
directories["temporary_storage"] = tempfile.gettempdir() directories["temporary_storage"] = tempfile.gettempdir()
if not directories["cache"]:
directories["cache"] = appdirs.user_cache_dir("manim")
# Assumes all shaders are written into manimlib/shaders # Assumes all shaders are written into manimlib/shaders
directories["shaders"] = os.path.join( directories["shaders"] = os.path.join(
get_manim_dir(), "manimlib", "shaders" get_manim_dir(), "manimlib", "shaders"

View File

@ -10,6 +10,10 @@ def get_directories() -> dict[str, str]:
return get_customization()["directories"] return get_customization()["directories"]
def get_cache_dir() -> str:
return get_directories()["cache"]
def get_temp_dir() -> str: def get_temp_dir() -> str:
return get_directories()["temporary_storage"] return get_directories()["temporary_storage"]