Explain the difference between Page, Action, Fragment, Low-Level, SQL caching types.

Technology CommunityCategory: Ruby on RailsExplain the difference between Page, Action, Fragment, Low-Level, SQL caching types.
VietMX Staff asked 3 years ago

Rails provides a set of caching features out of the box.

  • Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the webserver (i.e. Apache or NGINX) without having to go through the entire Rails stack.
  • Action Caching works like Page Caching except the incoming web request hits the Rails stack so that before filters can be run on it before the cache is served.
  • Fragment Caching allows a fragment of view logic to be wrapped in a cache block and served out of the cache store when the next request comes in.
  • Russian Doll Caching. You may want to nest cached fragments inside other cached fragments. This is called Russian doll caching.
  • Low-Level Caching. Sometimes you need to cache a particular value or query result instead of caching view fragments. The most efficient way to implement low-level caching is using the Rails.cache.fetch method.
  • SQL Caching. Query caching is a Rails feature that caches the result set returned by each query. If Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again.