# Caching

`helm-schema` caches the Kubernetes and CRD schemas it downloads, so later runs are fast and can
run [offline](https://romnn.github.io/helm-schema/docs/guide/kubernetes-schemas/#working-offline).

The cache only saves downloads. A schema missing from it is fetched, and a run with a cold cache
writes the same schema as one with a warm cache.

## Cache roots

Two managed cache roots, each set, versioned and invalidated on its own:

| Flag | Holds |
|---|---|
| `--k8s-schema-cache-dir <DIR>` | Upstream Kubernetes JSON schemas. |
| `--crd-catalog-cache-dir <DIR>` | CRD catalog schemas. |

A forward-incompatible Kubernetes cache does not block CRD resolution, and vice versa.

### Default location

Without a flag or an environment variable, each root is in the per-user cache directory, or else in
the system's temporary directory:

| Platform | Kubernetes schemas | CRD catalog |
|---|---|---|
| Linux, macOS | `$XDG_CACHE_HOME/helm-schema/kubernetes-json-schema`, else `$HOME/.cache/…` | `…/helm-schema/crds-catalog` |
| Windows | `%LOCALAPPDATA%\helm-schema\kubernetes-json-schema`, else `%USERPROFILE%\AppData\Local\…` | `…\helm-schema\crds-catalog` |

To move a root, from highest to lowest precedence:

1. **The flag:** `--k8s-schema-cache-dir` or `--crd-catalog-cache-dir`.
2. **The environment variable:** `HELM_SCHEMA_K8S_SCHEMA_CACHE` or `HELM_SCHEMA_CRD_SCHEMA_CACHE`.

Both take a relative path as given, relative to the current directory. The defaults never do: a
relative `XDG_CACHE_HOME` or `HOME` is ignored, so the same chart always reads the same cache,
wherever `helm-schema` starts.

`--crd-override-dir` is not a cache. It holds schemas you keep yourself, is never cleared and has no
layout marker, so it cannot share a directory with a cache root.

## Per-source layout

Each managed root uses a per-source layout so a mirror URL never silently masks the default catalog:

```text
<k8s-cache-root>/
├── CACHE_LAYOUT_VERSION
├── default/                    # built-in catalog
│   └── v1.35.0/service-v1.json
└── <hash-of-mirror-url>/       # per-mirror namespace
    └── v1.35.0/service-v1.json

<crd-cache-root>/
├── CACHE_LAYOUT_VERSION
├── default/
│   └── monitoring.coreos.com/servicemonitor_v1.json
└── <hash-of-mirror-url>/
    └── monitoring.coreos.com/servicemonitor_v1.json
```

The default source wins over mirrors. A mirror's files stay in their own directory, where you can
inspect them.

## Compatibility policy (alpha)

While `helm-schema` is alpha, the cache layout may change between releases. Each root has a
`CACHE_LAYOUT_VERSION` file, which `helm-schema` compares with its own layout when it starts:

| Marker state | Behavior | Diagnostic |
|---|---|---|
| Matches the binary | Used as-is | — |
| Missing, root populated (legacy) | Managed subtree wiped and repopulated | `CacheLayoutInvalidated` |
| Missing, root empty | First-populate; marker written | — |
| Older than the binary | Wiped and repopulated | `CacheLayoutInvalidated` |
| Newer than the binary | Binary refuses to mutate the root; left untouched | `CacheLayoutForwardIncompatible` |

On a forward-incompatible marker, upgrade `helm-schema` or point the flag at a different path.

## Refreshing

- **`--no-cache`:** skips reading the cache and asks the source again. Answers, a schema or a
  definite "not found", still update the cache, which repairs a stale or partial entry.
- **`--offline`:** reads only what is on disk. A schema the cache lacks is reported as unknown,
  never as nonexistent.

## Warming a cache for offline CI

```sh
# Once, online: fill the cache with what the chart needs
helm-schema ./mychart \
  --k8s-schema-cache-dir ./cache/k8s \
  --crd-catalog-cache-dir ./cache/crd \
  --output /dev/null

# Later, without network: read it
helm-schema ./mychart \
  --offline \
  --k8s-schema-cache-dir ./cache/k8s \
  --crd-catalog-cache-dir ./cache/crd \
  --output values.schema.json
```

Keep the cache directories, committed or as a build artifact, so the offline run can be repeated.
