# Build caching with ccache

**URL:** <https://discuss.circleci.com/t/build-caching-with-ccache/51807>\
**Category:** Caching Dependencies\
**Created:** [August 28, 2024, 10:09pm UTC](https://discuss.circleci.com/t/build-caching-with-ccache/51807 "2024-08-28T22:09:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkeating-planted](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rkeating-planted/32/35549_2.png) [@rkeating-planted](https://discuss.circleci.com/u/rkeating-planted)\
**Post date:** [August 28, 2024, 10:09pm UTC](https://discuss.circleci.com/t/build-caching-with-ccache/51807/1 "2024-08-28T22:09:16Z")

</div>

I recently setup ccache to speed up our build in local development, and I’d like to enable it on CircleCI as well.

Initially adding this to our config worked:  
- run:  
name: Ensure ~/ccache directory exists  
command: mkdir -p ~/.ccache  
- restore\_cache:  
keys:  
- persistent\_ccache\_v2  
…  
- save\_cache:  
paths:  
- ~/.ccache  
key: persistent\_ccache\_v2

However, I was not aware that caches are immutable at that point. As a result, the cache ends up becoming useless as the code changes.

I tried to setup a daily cache like this instead:  
- run:  
name: Ensure ~/ccache directory exists  
command: mkdir -p ~/.ccache  
- run:  
name: Create daily cache key  
command: echo “export CACHE\_KEY=$(date --iso-8601)” \>\> $BASH\_ENV  
- restore\_cache:  
keys:  
- ccache\_{{ .Environment.CACHE\_KEY }}  
…  
- save\_cache:  
paths:  
- ~/.ccache  
key: ccache\_{{ .Environment.CACHE\_KEY }}

Unfortunately that doesn’t work either because that environment variable is neither exported by CircleCI or setup within a context: the key ends up being “ccache\_”.

Any ideas on how to make this work?

---

<div class="post-metadata">

**Author:** ![wyardley](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/wyardley/32/19231_2.png) [@wyardley](https://discuss.circleci.com/u/wyardley)\
**Post date:** [August 29, 2024, 11:35pm UTC](https://discuss.circleci.com/t/build-caching-with-ccache/51807/2 "2024-08-29T23:35:20Z")

</div>

I have had limited success the time or two I’ve wanted to do something similar.

From what I remember, there is a workaround, which would be to have a step that touches a file with a datestamp, then use the `{{ checksum "the_file" }}` as your cache key.

For example:

> [@How to use python version or image name in cache key](https://discuss.circleci.com/t/how-to-use-python-version-or-image-name-in-cache-key/41108/3):
>
> Seeing the same issue (and the discussion board help actually found this topic to offer me!) I can’t use a composite key that includes, say, {{ checksum ".venv/bin/python" }}, because the restore job doesn’t yet have that file to compute the checksum on. This basic idea works for me: commands: checkout-and-restore-deps: steps: - checkout - run: python --version \> python\_version - restore\_cache: keys: - pipenv-v3-{{ .Branch }}-{{ checksum "Pipfile.…

In your case, you’d put the datestamp in the _contents_ of the file.

There’s a support article that suggests more or less exactly this for your use case:

> **[Creating a "daily" cache](https://support.circleci.com/hc/en-us/articles/360012618473-Creating-a-daily-cache)**
>
> Daily Cache
> Note: This is not required or recommended for many use cases, but can prove potentially useful for some instances.
> For example, if you are building static blog sites that fetch from ext...

---

<div class="post-metadata">

**Author:** ![rkeating-planted](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rkeating-planted/32/35549_2.png) [@rkeating-planted](https://discuss.circleci.com/u/rkeating-planted)\
**Post date:** [August 30, 2024, 12:52am UTC](https://discuss.circleci.com/t/build-caching-with-ccache/51807/3 "2024-08-30T00:52:59Z")

</div>

Ah, that’s a clever workaround. Thanks!
