|
10 | 10 | from textwrap import dedent |
11 | 11 | from typing import Any, Callable, Literal, Self, Sequence, TypeVar, overload |
12 | 12 |
|
13 | | -from pydantic import SkipValidation |
| 13 | +from pydantic import Field, SkipValidation |
14 | 14 | from pydantic.dataclasses import dataclass |
15 | 15 |
|
16 | 16 | from hexdoc.model import DEFAULT_CONFIG, HexdocModel |
@@ -50,6 +50,7 @@ class ModResourceLoader(ValidationContext): |
50 | 50 | export_dir: Path | None |
51 | 51 | resource_dirs: Sequence[PathResourceDir] |
52 | 52 | _stack: SkipValidation[ExitStack] |
| 53 | + _cache: dict[Path, str] = Field(default_factory=dict) |
53 | 54 |
|
54 | 55 | @classmethod |
55 | 56 | def clean_and_load_all( |
@@ -447,12 +448,17 @@ def _load_path( |
447 | 448 | decode: Callable[[str], _T] = decode_json_dict, |
448 | 449 | export: ExportFn[_T] | Literal[False] | None = None, |
449 | 450 | ) -> _T: |
450 | | - if not path.is_file(): |
451 | | - raise FileNotFoundError(path) |
| 451 | + if path in self._cache: |
| 452 | + data = self._cache[path] |
| 453 | + logger.debug(f"Fetching {path} from cache") |
| 454 | + else: |
| 455 | + if not path.is_file(): |
| 456 | + raise FileNotFoundError(path) |
452 | 457 |
|
453 | | - logger.debug(f"Loading {path}") |
| 458 | + logger.debug(f"Loading {path}") |
454 | 459 |
|
455 | | - data = path.read_text("utf-8") |
| 460 | + data = path.read_text("utf-8") |
| 461 | + self._cache[path] = data |
456 | 462 | value = decode(data) |
457 | 463 |
|
458 | 464 | if resource_dir.reexport and export is not False: |
|
0 commit comments