Sharing a Cache: Build Dirs, CI & Remote Storage
One developer with a warm cache is nice. A cache shared by every build directory, every CI job and every teammate is where the large savings are. ccache supports three levels of sharing:
- One machine, several build directories: automatic, because the cache lives in
CCACHE_DIR(default~/.cache/ccache), not in the build directory. - CI jobs over time: save and restore
CCACHE_DIRwith the CI system's cache feature. - Many machines at once: remote storage (ccache 4.4+), a shared backend that every client reads from and writes to: a directory (
file:, e.g. on NFS), an HTTP server (http:, e.g. a WebDAV server or a bazel-remote/Nginx cache) or Redis (redis:).
Several build directories
Two build directories with the same flags share entries; a build with different flags (another build type, a sanitizer) gets its own:
mkdir -p proj/src && cd proj
for n in parser lexer eval; do printf '#include <string>\nint %s() { return int(std::string("%s").size()); }\n' $n $n > src/$n.cpp; done
echo 'int parser(); int lexer(); int eval(); int main() { return parser() + lexer() + eval() - 15; }' > src/main.cpp
printf 'cmake_minimum_required(VERSION 3.25)\nproject(P CXX)\nadd_executable(app src/main.cpp src/parser.cpp src/lexer.cpp src/eval.cpp)\n' > CMakeLists.txt
st() { ccache --print-stats | awk -F'\t' '$2 > 0 && $1 ~ /^(direct_cache_hit|preprocessed_cache_hit|cache_miss)$/ { printf "%s=%s ", $1, $2 } END { print "" }'; }
export CCACHE_BASEDIR=$PWD
b() {
cmake -S . -B $1 -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_BUILD_TYPE=$2 > /dev/null
ccache -z > /dev/null && cmake --build $1 > /dev/null && printf '%-22s %s\n' "$1 ($2):" "$(st)"
}
b build-rel Release
b build-rel-copy Release
b build-dbg Debug
b build-relwithdeb RelWithDebInfo
echo "--- the cache directory: $(ccache -k cache_dir)"build-rel (Release): cache_miss=4
build-rel-copy (Release): direct_cache_hit=4
build-dbg (Debug): cache_miss=4
build-relwithdeb (RelWithDebInfo): cache_miss=4
--- the cache directory: /home/learner/.cache/ccache
The second Release directory compiled nothing. Debug (-g) and RelWithDebInfo (-O2 -g -DNDEBUG) are different compiles and fill their own entries. This is why a CI matrix with five build types needs about five times the cache space of one.
Size limits and cleanup
ccache evicts least-recently-used entries when the cache exceeds max_size (default 5 GiB) or max_files. Set the limit to hold at least one full build of every configuration you care about, or the cache evicts entries before they're reused:
ccache -M 20G # = ccache -o max_size=20G
ccache --evict-older-than 7d # drop entries not used for a week
ccache -X 5 # recompress at zstd level 5 (default is 1, via compression_level = 0)
ccache -x # show compression statisticsRough sizing: ccache -s after one clean build shows its cache size; multiply by the number of configurations, add headroom for a few branches.
CI: save and restore the cache directory
The pattern is the same on every CI system:
# GitHub Actions sketch
env:
CCACHE_DIR: ${{ github.workspace }}/.ccache
CCACHE_BASEDIR: ${{ github.workspace }}
CCACHE_COMPILERCHECK: content
CCACHE_MAXSIZE: 2G
steps:
- uses: actions/cache@v4
with:
path: .ccache
key: ccache-${{ runner.os }}-gcc13-${{ github.ref_name }}-${{ github.sha }}
restore-keys: |
ccache-${{ runner.os }}-gcc13-${{ github.ref_name }}-
ccache-${{ runner.os }}-gcc13-main-
- run: ccache -z
- run: cmake --preset ci && cmake --build --preset ci
- run: ccache -s- The key ends in the commit, so every run saves a new snapshot;
restore-keysfall back to the newest snapshot of the branch, then ofmain. - Zero the stats at the start and print them at the end, so the log shows this job's hit rate.
- Keep the cache per compiler and OS in the key: mixing them only wastes space.
The CI caches vs artifact repositories lesson compares this with other ways to reuse build output.
Remote storage: one cache for many machines
With remote_storage set, ccache looks in its local cache first, then in the remote one; results compiled locally are written to both. This session plays two machines: each has its own local cache directory, and both point at the same shared directory:
mkdir -p src && cd src
for n in a b c; do printf '#include <map>\nint %s() { return int(std::map<int, int>{{1, 2}}.size()); }\n' $n > $n.cpp; done
st() { ccache --print-stats | awk -F'\t' '$2 > 0 && $1 ~ /^(direct_cache_hit|cache_miss|local_storage_hit|remote_storage_hit|remote_storage_write)$/ { printf "%s=%s ", $1, $2 } END { print "" }'; }
compile_all() { for n in a b c; do ccache g++ -O2 -c $n.cpp -o $n.o; done; }
mkdir -p /home/learner/shared-cache
export CCACHE_REMOTE_STORAGE="file:/home/learner/shared-cache"
CCACHE_DIR=/home/learner/machine-a ccache -z > /dev/null
CCACHE_DIR=/home/learner/machine-a compile_all
echo "machine A: $(CCACHE_DIR=/home/learner/machine-a st)"
CCACHE_DIR=/home/learner/machine-b compile_all
echo "machine B: $(CCACHE_DIR=/home/learner/machine-b st)"
CCACHE_DIR=/home/learner/machine-b ccache -z > /dev/null
CCACHE_DIR=/home/learner/machine-b compile_all
echo "machine B, again: $(CCACHE_DIR=/home/learner/machine-b st)"
echo "--- a pull-request build that may read but not write:"
export CCACHE_REMOTE_STORAGE="file:/home/learner/shared-cache|read-only"
printf 'int d() { return 4; }\n' > d.cpp
CCACHE_DIR=/home/learner/machine-c ccache g++ -O2 -c d.cpp -o d.o
echo "machine C: $(CCACHE_DIR=/home/learner/machine-c st)"machine A: cache_miss=3 remote_storage_write=6
machine B: direct_cache_hit=3 remote_storage_hit=3
machine B, again: direct_cache_hit=3 local_storage_hit=3
--- a pull-request build that may read but not write:
machine C: cache_miss=1
- Machine A compiled everything and wrote the results (and manifests) to the shared storage.
- Machine B had an empty local cache but got remote hits for all three, and copied them into its local cache.
- On B's next build the local cache answered first.
- Machine C used the storage read-only (
|read-only): its miss was compiled locally and not uploaded. That's the setting for untrusted builds, such as pull requests from forks.
The same mechanism works with http://cache.example.com/ccache|layout=bazel (ccache can talk to a bazel-remote or other HTTP cache) and redis://host. Securing who may write is the important part: see cache poisoning.
Test yourself
build-rel and build-rel-copy are two Release build directories of the same checkout. Why did the second one compile nothing?
- Ninja shared its outputs
- The cache lives in CCACHE_DIR, outside the build directories, and the compiles were identical
- CMake copied the objects
- The second build was a no-op
With remote_storage set, what happens on a local miss and a remote hit?
- The compile runs anyway
- The result is fetched from remote storage and also stored locally
- ccache fails
- Only the manifest is fetched
Which remote storage attribute suits pull-request builds?
- |layout=subdirs
- |read-only
- |shards
- none, PRs should write