Creating a "daily" cache

Here is a trick to give yourself a fresh cache daily. This is not required or recommended for many use cases but can prove potentially useful for some instances.

For instance, in building static blog sites that fetch from external API's, you may want to build with a new cache periodically to ensure any new information is fetched.

How:

Early in your job create a file containing today's date without a timestamp

- run: date +%F > date

You can now use this file to test against when choosing when to rebuild cache by utilizing the checksum template.


      - restore_cache:
          keys:
            - date-cache-{{ checksum "date" }}
      - save_cache:
            key: date-cache-{{ checksum "date" }}
            paths:
                - "/docs"

The checksum of the date file will remain static until the next day, at which point a new cache will be created.

Note: the old caches will still exist under a different cache key and will expire after 15 days as per our retention policy:

https://circleci.com/docs/2.0/caching/#cache-expiration

 

Was this article helpful?
16 out of 17 found this helpful

Comments

0 comments

Please sign in to leave a comment.