Rails query cache

As per documentation:

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.

To check whether query caching is enabled you can run:

pry(main)> ActiveRecord::Base.connection.query_cache_enabled
=> false

Running the same query twice, will hit the database two times:

pry(main)> User.count
   (33.3ms)  SELECT COUNT(*) FROM "users"
=> 2
pry(main)> User.count
   (0.9ms)  SELECT COUNT(*) FROM "users"
=> 2

After enabling the query cache explicitly, database will be accessed once:

pry(main)> ActiveRecord::Base.connection.enable_query_cache!
=> true
pry(main)> User.count
   (0.7ms)  SELECT COUNT(*) FROM "users"
=> 2
pry(main)> User.count
  CACHE  (0.1ms)  SELECT COUNT(*) FROM "users"
=> 2

Rails automatically enables query cache on a request basis, so calling the same query inside a controller action will use the cached result from a previous call:

class UsersController < ApplicationController
  def show
    users_count = User.count
    all_users_count = User.count
  end
end
Processing by UsersController#show as HTML
   (1.4ms)  SELECT COUNT(*) FROM "users"
  ↳ app/controllers/users_controller.rb:3:in `show'
  CACHE  (0.0ms)  SELECT COUNT(*) FROM "users"