---
title: "Rails 5 improves rendering partial from cache"
description:
  "With the inclusion of multi_fetch_fragments gem in Rails core, Rails 5
  improves page load speeds substantially."
canonical_url: "https://www.bigbinary.com/blog/rails-5-makes-partial-redering-from-cache-substantially-faster"
markdown_url: "https://www.bigbinary.com/blog/rails-5-makes-partial-redering-from-cache-substantially-faster.md"
---

# Rails 5 improves rendering partial from cache

With the inclusion of multi_fetch_fragments gem in Rails core, Rails 5 improves
page load speeds substantially.

- Author: Ratnadeep Deshmane
- Published: March 9, 2016
- Categories: Rails 5, Rails

Let's have a look at Rails view code that renders partial using a collection.

```erb
# index.html.erb
<%= render partial: 'todo', collection: @todos %>

# _todo.html.erb
<% cache todo do %>
  <%= todo.name %>
<% end %>

```

In the above case Rails will do one fetch from the cache for each todo.

Fetch is usually pretty fast with any caching solution, however, one fetch per
todo can make the app slow.

Gem [multi_fetch_fragments](https://github.com/n8/multi_fetch_fragments) fixed
this issue by using
[read_multi](http://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html#method-i-read_multi)
api provided by Rails.

In a single call to cache, this gem fetches all the cache fragments for a
collection. The author of the gem saw
[78% speed improvement](http://ninjasandrobots.com/rails-faster-partial-rendering-and-caching)
by using this gem.

The features of this gem
[have been folded into Rails 5](https://github.com/rails/rails/pull/18948).

To get benefits of collection caching, just add `cached: true` as shown below.

```erb
# index.html.erb
<%= render partial: 'todo', collection: @todos, cached: true %>

# _todo.html.erb
<% cache todo do %>
  <%= todo.name %>
<% end %>

```

With `cached: true` present, Rails will use `read_multi` to the cache store
instead of reading from it every partial.

Rails will also log cache hits in the logs as below.

```plaintext
  Rendered collection of todos/_todo.html.erb [100 / 100 cache hits] (339.5ms)
```

Checkout [the pull request](https://github.com/rails/rails/pull/23695) to gain
better understanding about how collection caching works.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-5-makes-partial-redering-from-cache-substantially-faster)
