Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Rails cache templates?

I assume yes, and some tests point to yes, but I'd like to find the documentations that says Rails caches (unevaluated) templates in memory.

That is, for any .erb, .sass, .jbuilder, etc template Rails will:

  1. read the template from file, only once
  2. retrieve the template from memory when needed
  3. apply data to the template on every invocation unless the generated output is cached.

All template/cache searches and documentation seem to be focused on point #3. And development Rails flags enable/disable class-caching. But finding docs that verify claim #1/#2 seem illusive. Does Rails re-read template files every time and rely on OS level file caching?

like image 218
Mark Bolusmjak Avatar asked May 24 '16 18:05

Mark Bolusmjak


People also ask

What does Rails cache do?

1.1 Page Caching Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the web server (i.e. Apache or NGINX) without having to go through the entire Rails stack. While this is super fast it can't be applied to every situation (such as pages that need authentication).

Does Rails cache use Redis?

To use Redis as a Rails cache store, use a dedicated Redis cache that's set up as a LRU (Last Recently Used) cache instead of pointing the store at your existing Redis server, to make sure entries are dropped from the store when it reaches its maximum size.

What is view caching in Rails?

What is View Caching. View caching in Ruby on Rails is taking the HTML that a view generates and storing it for later. Although Rails has support for writing these to the filesystem or keeping them in memory, for production use, you'll almost certainly want a standalone caching server, such as Memcached or Redis.

Is Rails cache thread safe?

The default Rails cache (ActiveSupport::Cache MemoryStore) is thread-safe as of Rails version 3.1: http://api.rubyonrails.org/v3.1.0/files/activesupport/CHANGELOG.html As the CHANGELOG notes: "Make thread safe so that the default cache implementation used by Rails is thread safe."


2 Answers

I had the same question and did some hunting.

Yes - Rails caches templates.

like image 155
hcarver Avatar answered Oct 27 '22 15:10

hcarver


Pay attention to the fact cache takes locals as key, and I would say then that unevaluated template is not cached.

like image 40
Guillaume Avatar answered Oct 27 '22 13:10

Guillaume