Ethan
Gunderson

A Better PLT Cache Key

Apr 6, 2019 :: elixir

If you’re running Dialyzer in your CircleCI build, chances are you might have a plt cache directive that looks something like:

- restore_cache:
keys:
- v3-plt-cache{{ checksum "mix.lock" }}
- run: mix dialyzer --plt
- save_cache:
key: v3-plt-cache{{ checksum "mix.lock" }}

This works fine. Any change to your mix dependency tree will bust the cache. But, what happens if the Elixir or Erlang version changes? Your builds will be slow until you change part of the cache key. While this works, it’s a pain, and it has an easy solution. Checksome all the things:

- run:
name: write_otp_version
command: erl -eval '{ok, Version} = file:read_file(filename:join([code:root_dir(), "releases", erlang:system_info(otp_release), "OTP_VERSION"])), io:fwrite(Version), halt().' -noshell > /tmp/otp_version.txt
- run:
name: write_elixir_version
command: elixir -v > /tmp/elixir_version.txt
- restore_cache:
keys:
- v3-plt-cache{{ checksum "mix.lock" }}{{ checksum "/tmp/otp_version.txt" }}{{ checksum "/tmp/elixir_version.txt"}}
- run: mix dialyzer --plt
- save_cache:
key: v3-plt-cache{{ checksum "mix.lock" }}{{ checksum "/tmp/otp_version.txt" }}{{ checksum "/tmp/elixir_version.txt"}}