19 Nov, 2026Nitesh Jangid
GitHub Actions Cache Keys and Restore Keys: Guide & Examples
Learn GitHub Actions cache keys and restore keys with practical YAML examples. Reduce cache misses and speed up cache restores by up to 10x with Monk CI.
GitHub Actions workflows often waste valuable build minutes downloading the same dependencies repeatedly. Caching helps eliminate unnecessary downloads, but poorly configured cache keys can cause frequent cache misses and slower CI builds.
Understanding GitHub Actions cache keys and restore keys is essential for efficient dependency caching. This guide explains how they work, with practical YAML examples and optimization tips.
What Are GitHub Actions Cache Keys?
A cache key identifies cached dependencies or build files. GitHub Actions uses this key to find and restore previously saved data.
- uses: actions/cache@v5
with:
path: ~/.npm
key: npm-${{ runner.os }}-${{ hashFiles('package-lock.json') }}Here:
- path specifies the directory to cache.
- key identifies the cached data.
- hashFiles() generates a fingerprint based on the dependency lockfile.
When package-lock.json changes, GitHub Actions generates a different cache key, allowing dependencies to be refreshed without reusing an outdated exact-match cache.
How Do Restore Keys Work?
Restore keys provide fallback prefixes when an exact cache match isn't available.
key: npm-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
restore-keys: |
npm-${{ runner.os }}-Suppose the previous cache key was npm-Linux-abc123, but updated dependencies generate npm-Linux-xyz789.
Instead of starting from scratch, GitHub Actions searches for a compatible cache matching npm-Linux-. The package manager then installs any missing or updated packages.
GitHub Actions searches for exact matches first, followed by partial matches and restore keys. Eligible caches from the default branch may also be restored.
The result: better cache reuse, fewer redundant downloads, and faster CI workflows.
Practical GitHub Actions Cache Examples
1. Node.js Dependency Caching
Cache npm downloads while keeping installed dependencies synchronized with your lockfile.
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v5
with:
path: ~/.npm
key: npm-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
restore-keys: |
npm-${{ runner.os }}-
- run: npm ci
- run: npm testRunning npm ci after restoration ensures dependencies match the lockfile, even when a fallback cache is restored.
2. Python Dependency Caching
Python workflows can cache pip downloads using the same approach.
- uses: actions/cache@v5
with:
path: ~/.cache/pip
key: pip-${{ runner.os }}-${{ hashFiles('requirements.txt') }}
restore-keys: |
pip-${{ runner.os }}-
- run: pip install -r requirements.txtThis reduces repeated package downloads while allowing dependencies to change safely.
3. Monorepo Cache Keys
For monorepos, use application-specific cache keys to avoid unnecessary cache invalidation.
key: web-${{ runner.os }}-${{ hashFiles('apps/web/package-lock.json') }}
restore-keys: |
web-${{ runner.os }}-Use separate cache paths when applications require independent caches.
Best Practices for GitHub Actions Caching
Follow these practices to improve cache efficiency:
- Hash dependency files: Use hashFiles() to invalidate caches when dependencies change.
- Include environment details: Add operating system, architecture, or runtime information when cached files are environment-specific.
- Use specific restore keys: Avoid overly broad prefixes that might restore unrelated files.
- Avoid commit-specific keys: Using ${{ github.sha }} creates unnecessary caches for unchanged dependencies.
- Monitor cache hits: Check workflow logs and the cache-hit output to diagnose caching issues.
Remember that caching should accelerate dependency installation, not replace it. Workflows must still succeed when no cache is available.
Speed Up GitHub Actions Cache Restores by Up to 10x with Monk CI
Even with perfectly optimized cache keys, GitHub Actions cache restoration can become a bottleneck, particularly when transferring large dependency caches from remote storage.
Monk CI accelerates GitHub Actions cache restores by up to 10x using a high-speed, co-located caching backend. By minimizing network transfer overhead and optimizing cache delivery, Monk CI helps developers spend less time waiting for dependencies and more time building and shipping code.
Switch Runners Without Changing Your Cache Configuration
Getting started requires only a runner label change:
# Standard GitHub Actions runner
runs-on: ubuntu-24.04
# Monk CI runner
runs-on: monkci-ubuntu-24.04-4Your existing actions/cache steps, cache keys, restore keys, and dependency installation commands continue working without additional caching configuration.
Monk CI also accelerates caching supported by setup actions such as actions/setup-node and actions/setup-python.
With up to 10x faster cache restores, teams can reduce dependency-related CI delays without rewriting their workflows.
Actual performance improvements depend on cache size, workload, and network conditions.
Final Thoughts
Effective GitHub Actions cache keys and restore keys reduce repeated downloads, improve dependency reuse, and make CI workflows more efficient.
Combining well-structured cache keys with Monk CI's optimized caching infrastructure can further reduce build times and improve developer productivity.
Optimize your cache keys. Restore caches up to 10x faster. Ship code faster.
Try Monk CI Free with 5,000 free build minutes, or explore our complete GitHub Actions caching guide.