Skip to content

Conditional caching and multi-fetch across partial rendering

Robert May requested to merge conditional-caching into master

Conditional caching

This adds conditional caching support, allowing you to skip caching using either if or unless conditions, e.g.

= chunky_cache(if: -> { user_signed_in? }) do
...
= chunky_cache(unless: -> { fox == :cute }) do

Calls over multiple templates

This also includes a refactor which means that even cache calls from partials rendered inside the main chunky_cache block will be fetched in the same single multi-fetch cache call. This means you can wrap collection partial rendering from the parent template but still use chunked caching inside the partial:

= chunky_cache do
  = render partial: "blorp", collection: @blorps

You can have additional chunky_cache calls inside the nested partial now as well, and it will defer to the outer call instead of running alongside it.

So basically you could render a collection of 40 partials, each one with 20 chunk cache calls inside it, and perform only 1 actual network request to your cache.

Speed improvement

Also includes a notable speed improvement when reinserting the cached data into the captured template string. Somewhere on the order of 10-20x faster from some rough benchmarks.

Edited by Robert May

Merge request reports