Jekyll2019-05-09T20:29:30-05:00www.ethangunderson.com/feed.xmlEthan Gunderson.comEthan GundersonA better PLT cache key2019-04-06T00:00:00-05:002019-04-06T00:00:00-05:00www.ethangunderson.com/posts/a-better-plt-cache-key<p>If you’re running <a href="https://github.com/jeremyjh/dialyxir">Dialyzer</a> in your CircleCI build, chances are you have a plt cache directive that looks something like this:</p> <figure class="highlight"><pre><code class="language-conf" data-lang="conf">- <span class="n">restore_cache</span>: <span class="n">keys</span>: - <span class="n">v3</span>-<span class="n">plt</span>-<span class="n">cache</span>{{ <span class="n">checksum</span> <span class="s2">"mix.lock"</span> }} - <span class="n">run</span>: <span class="n">mix</span> <span class="n">dialyzer</span> --<span class="n">plt</span> - <span class="n">save_cache</span>: <span class="n">key</span>: <span class="n">v3</span>-<span class="n">plt</span>-<span class="n">cache</span>{{ <span class="n">checksum</span> <span class="s2">"mix.lock"</span> }}</code></pre></figure> <p>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:</p> <figure class="highlight"><pre><code class="language-conf" data-lang="conf">- <span class="n">run</span>: <span class="n">name</span>: <span class="n">write_otp_version</span> <span class="n">command</span>: <span class="n">erl</span> -<span class="n">eval</span> <span class="s1">'{ok, Version} = file:read_file(filename:join([code:root_dir(), "releases", erlang:system_info(otp_release), "OTP_VERSION"])), io:fwrite(Version), halt().'</span> -<span class="n">noshell</span> &gt; /<span class="n">tmp</span>/<span class="n">otp_version</span>.<span class="n">txt</span> - <span class="n">run</span>: <span class="n">name</span>: <span class="n">write_elixir_version</span> <span class="n">command</span>: <span class="n">elixir</span> -<span class="n">v</span> &gt; /<span class="n">tmp</span>/<span class="n">elixir_version</span>.<span class="n">txt</span> - <span class="n">restore_cache</span>: <span class="n">keys</span>: - <span class="n">v3</span>-<span class="n">plt</span>-<span class="n">cache</span>{{ <span class="n">checksum</span> <span class="s2">"mix.lock"</span> }}{{ <span class="n">checksum</span> <span class="s2">"/tmp/otp_version.txt"</span> }}{{ <span class="n">checksum</span> <span class="s2">"/tmp/elixir_version.txt"</span>}} - <span class="n">run</span>: <span class="n">mix</span> <span class="n">dialyzer</span> --<span class="n">plt</span> - <span class="n">save_cache</span>: <span class="n">key</span>: <span class="n">v3</span>-<span class="n">plt</span>-<span class="n">cache</span>{{ <span class="n">checksum</span> <span class="s2">"mix.lock"</span> }}{{ <span class="n">checksum</span> <span class="s2">"/tmp/otp_version.txt"</span> }}{{ <span class="n">checksum</span> <span class="s2">"/tmp/elixir_version.txt"</span>}}</code></pre></figure>Ethan GundersonIf you’re running Dialyzer in your CircleCI build, chances are you have a plt cache directive that looks something like this: