What Is actions/cache in GitHub Actions?
actions/cache saves and restores directories between runs by key, so you do not re-download dependencies every time.
Installing dependencies on every run is slow and wasteful. actions/cache persists chosen directories keyed on something like a lockfile hash, restoring them on the next run for a big speedup.
What it is
The official caching action. You give it a path to cache and a key; on a hit it restores the path, and at job end it saves the path under that key on a miss.
steps:
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-How it works
At the start of the job the action looks for an exact key match, falling back to restore-keys prefixes. If nothing matches, the job runs normally and the path is uploaded as a new cache entry afterward.
Choosing a good key
Keys usually hash a lockfile so the cache invalidates only when dependencies change. restore-keys provide partial fallbacks so you still get most of a stale cache after a small change.
Why it matters
Caching can cut minutes off every run, which directly lowers cost. Many setup-* actions have built-in caching, but actions/cache covers anything else. Latchkey managed runners add their own layer caching on top for even faster builds.
Related concepts
Caching differs from artifacts: caches speed up future runs, artifacts share outputs of the current run.
Key takeaways
actions/cacherestores and saves paths by key.- Hash a lockfile for the key; use
restore-keysfor fallback. - Caches speed up runs and lower cost.