Partial Cache Restoration & Bundler

bundler
caching
cache

#1

I’m confused about the code block in the Bundler (Ruby) section here:
https://circleci.com/docs/2.0/caching/#bundler-ruby

The code block in question is:

- run: bundle install & bundle clean
- restore_cache:
    keys:
      # when lock file changes, use increasingly general patterns to restore cache
      - v1-gem-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
      - v1-gem-cache-{{ arch }}-{{ .Branch }}-
      - v1-gem-cache-{{ arch }}-
- save_cache:
    paths:
      - ~/.bundle
    key: v1-gem-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile.lock" }}

I’m reading this process as:

  • Run bundle install before the cache is put in place (a full gem installation time before restoring cache, which negates the time-saving benefits of a cache) and then run bundle clean
  • Restore the cache over the installed gems
  • Don’t do anything (between restore_cache and save_cache steps)
  • Save the new cache

Am I understanding that process correctly?
This looks to me like restore_cache and save_cache steps are not going to be effective, because the full bundle install time would have already been spent.

If I’m understanding things, would this be a more efficient process?

- restore_cache:
    keys:
      # when lock file changes, use increasingly general patterns to restore cache
      - v1-gem-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile.lock" }}
      - v1-gem-cache-{{ arch }}-{{ .Branch }}-
      - v1-gem-cache-{{ arch }}-
- run: bundle install & bundle clean
- save_cache:
    paths:
      - ~/.bundle
    key: v1-gem-cache-{{ arch }}-{{ .Branch }}-{{ checksum "Gemfile.lock" }}

If I’m not understanding this correctly, can anyone help clarify how the suggested code block works?


#2

I understand the code like you describe it, and it indeed looks wrong. But, it looks like the example on https://circleci.com/docs/2.0/caching/#bundler-ruby has been updated to match your suggested more efficient process.