Caching with Rails (part 1): Fragment and Russian Doll caching

Tram Ho

Introduction

Caching is one of the effective methods to improve the performance of the website by saving the little-changed components of the website to the cache to reuse each time they re-visit.

Rails provides us with 3 main caching techniques: Page caching , Action caching and Fragment caching . In this article we will focus on fragment caching only .

By default Rails only enables caching for production environments, to use it with development environment, we will have to add it in the config file:

Fragment Caching

We can cache the whole website and return subsequent requests, but this method only applies to pages that are almost completely static, because when the user generates different interactive requests, the content will have to be done. new constantly. When we cache the whole page, the user only sees the old content, so the above method does not work. In this case, we should cache only the least changed parts of the page, and the Fragment Caching born to do that job.

Fragment Caching allows a portion of the view to be encapsulated in a cache and returned when the next request comes.

For example, in case we want to cache each product, we can do the following:

When our application receives the first request, Rails will create a new cache entry with a unique key of the form:

The long string in the middle is product_id with the timestamp value of the product’s updated_at field. Rails uses this timestamp value to ensure the data does not age. If the value of the updated_at field is changed, a new key will be generated, and Rails will create a new cache with the key just created, the old cache will be removed. This process is called key-based expiration .

The fragments cache will also expire if the fragment’s view is changed. The sequence of letters at the end of the cache entry is called the template tree digest . It is a hash digest created based on the content of the view fragment we are trying to cache. When we change the fragment’s view, the digest chain will also be changed accordingly, and the cache will expire.

If we want to cache fragment under different conditions, we can use cache_if or cache_unless :

Collection Caching

The render helper method can also cache the individual templates of a collection by reading the entire cache templates in advance instead of using each read each cache one after another as above. To do this, we pass cached: true when rendering a collection:

All templates cached from the previous render will be fetched at once to improve the loading speed much faster. The template that has not been cached will be cached on the next render.

Russian Doll Caching

Russian Doll Caching is a technique that allows nested cached fragments – to cache sub-fragments within another cached fragment.

The advantage of this technique is that if the outer fragment is updated, only this fragment will change and the entire internal fragments will still be cached for reuse.

But in the opposite case, for example:

The internal view is rendered:

If any of the game ‘s attribute changes, the updated_at field will be updated, meaning the cache will expire. However, because the updated_at value of the product does not change, the product cache will not expire, the data rendered will remain the same.

To fix this problem, we can use the touch method on the model:

With the set touch set to true , any action that changes the game ‘s updated_at value will affect the product , so the cache for both the game and the product will expire and be removed.

From above, we can draw the principle of Russian Doll Caching technique as follows:

  • When the outer fragment is updated, only the fragment will change, the remaining fragments will remain, and can be reused.
  • When the innermost fragment is updated, it begins a chain that causes all its parent fragments to change.

Summary

The article to share about Caching in Rails and the powerful and effective caching technique is Russian Doll Caching . The article is limited, thank you for taking the time to read.

Source and reference: https://guides.rubyonrails.org/caching_with_rails.html

Share the news now

Source : Viblo