Skip to content
Latchkey

actions/cache: Keys, Restore Keys & Best Practices

actions/cache is the biggest CI speedup when keyed correctly - and a silent no-op when keyed wrong.

Cache the expensive, rarely-changing work and key it so the cache changes exactly when the content should.

Key vs restore-keys

The key is an exact match (a lockfile hash). restore-keys are prefix fallbacks so a near-miss restores a useful older cache instead of nothing.

Pattern

.github/workflows/ci.yml
- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: npm-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
    restore-keys: npm-${{ runner.os }}-

Pitfalls

  • Keying on a commit SHA → 0% hit rate.
  • Keying on something too stable → stale deps forever.
  • Caching the rebuilt output instead of the download store.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →