profiles_cache.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """The per-profile derived-value cache behind ``list_profiles`` (``hermes_cli.profiles``)."""
  2. from pathlib import Path
  3. from typing import Optional
  4. # (path, kind) -> (file signature, the small derived value). `list_profiles` re-reads three YAML
  5. # files PER PROFILE, and it is the shared body of `GET /api/profiles` and `profiles.list`, which the
  6. # Bots roster polls every 5s per connection — so an installer-seeded config.yaml (the annotated
  7. # template, ~119KB) was re-parsed for every bot every five seconds to yield the same two strings.
  8. # Only DERIVED values are cached, never a document a caller could write back: the raw readers
  9. # (`read_user_config_raw`, `_load_yaml_dict`) keep their uncached contract. See #117378.
  10. _PROFILE_FILE_CACHE: dict[tuple, tuple] = {}
  11. _PROFILE_FILE_CACHE_MAX = 512
  12. def _profile_file_signature(path: Path) -> Optional[tuple]:
  13. """``(mtime_ns, size, inode)``, or None when the file is absent. The atomic writers rename a
  14. temp file into place, so a rewrite always lands a new inode even within one mtime tick."""
  15. try:
  16. stat = path.stat()
  17. except OSError:
  18. return None
  19. return (stat.st_mtime_ns, stat.st_size, stat.st_ino)
  20. def _cached_profile_read(path: Path, kind: str, compute, signature_of=_profile_file_signature):
  21. """``compute()``'s value, reused while ``signature_of(path)`` is unchanged. A missing file is never
  22. cached: reading it costs nothing, and one created later must be picked up."""
  23. signature = signature_of(path)
  24. if signature is None:
  25. return compute()
  26. key = (str(path), kind)
  27. cached = _PROFILE_FILE_CACHE.get(key)
  28. if cached is not None and cached[0] == signature:
  29. return cached[1]
  30. value = compute()
  31. if len(_PROFILE_FILE_CACHE) >= _PROFILE_FILE_CACHE_MAX:
  32. _PROFILE_FILE_CACHE.clear()
  33. _PROFILE_FILE_CACHE[key] = (signature, value)
  34. return value