Skip to content

Commit feb0ba4

Browse files
committed
change default cache directory to follow XDG convention and respect user env overrides
1 parent 2b7c535 commit feb0ba4

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

tidy3d/config/sections.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import os
56
import ssl
67
from os import PathLike
78
from pathlib import Path
@@ -385,6 +386,21 @@ def apply_web(config: WebConfig) -> None:
385386
manager.apply_web_env(dict(config.env_vars))
386387

387388

389+
def _default_cache_directory() -> Path:
390+
"""Determine the default on-disk cache directory respecting platform conventions."""
391+
392+
base_override = os.getenv("TIDY3D_BASE_DIR")
393+
if base_override:
394+
base = Path(base_override).expanduser().resolve() / ".cache"
395+
else:
396+
xdg_cache = os.getenv("XDG_CACHE_HOME")
397+
if xdg_cache:
398+
base = Path(xdg_cache).expanduser().resolve()
399+
else:
400+
base = Path.home() / ".cache"
401+
return (base / "tidy3d" / "simulations").resolve()
402+
403+
388404
@register_section("local_cache")
389405
class LocalCacheConfig(ConfigSection):
390406
"""Settings controlling the optional local simulation cache."""
@@ -397,7 +413,7 @@ class LocalCacheConfig(ConfigSection):
397413
)
398414

399415
directory: DirectoryPath = Field(
400-
Path.home() / ".tidy3d" / "cache",
416+
default_factory=_default_cache_directory,
401417
title="Cache directory",
402418
description="Directory where cached artifacts are stored.",
403419
json_schema_extra={"persist": True},

tidy3d/web/cache.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,9 @@
2222
from tidy3d.web.core.constants import TaskId
2323
from tidy3d.web.core.http_util import get_version as _get_protocol_version
2424

25-
DEFAULT_CACHE_RELATIVE_DIR = Path(".tidy3d") / "cache" / "simulations"
2625
CACHE_ARTIFACT_NAME = "simulation_data.hdf5"
2726
CACHE_METADATA_NAME = "metadata.json"
2827

29-
ENV_ENABLE = "TIDY3D_CACHE_ENABLED"
30-
ENV_DIRECTORY = "TIDY3D_CACHE_DIR"
31-
ENV_MAX_SIZE = "TIDY3D_CACHE_MAX_SIZE_GB"
32-
ENV_MAX_ENTRIES = "TIDY3D_CACHE_MAX_ENTRIES"
33-
3428
TMP_PREFIX = "tidy3d-cache-"
3529
TMP_BATCH_PREFIX = "tmp_batch"
3630

0 commit comments

Comments
 (0)