ruby-on-railsrubyruby-on-rails-5memorycachefragment-caching

Rails 5.2.1 - Model & Fragment Caching


I am trying to setup Model and Fragment Caching in Rails 5.2.1

I have had success with Fragment caching, but I am still seeing database queries after implementing model caching for my Model.

I have enabled development caching

$ rails dev:cache

Model Helper

module LanguagesHelper
  def approved_languages
    Rails.cache.fetch("approved_languages") { Languages.is_active.is_approved }
  end
end

Controller

class LanguagesController < ApplicationController
  include LanguagesHelper

  def index
    @languages = approved_languages
  end
end

Views

app/views/languages/index.html.erb

<%= render partial: 'languages/language', collection: @languages, cached: true %>

app/views/languages/_language.html.erb

<% cache language do %>
  <%= language.name %>
<% end %>

Console

Started GET "/languages" for 127.0.0.1 at 2018-08-21 14:13:29 -0400
Processing by LanguagesController#index as HTML
  Rendering languages/index.html.erb within layouts/application
  Language Load (1.2ms)  SELECT "languages".* FROM "languages" WHERE "languages"."deleted" = $1 AND "languages"."approved" = $2  [["deleted", false], ["approved", true]]
  ↳ app/views/languages/index.html.erb:4
  Rendered collection of languages/_language.html.erb [1 / 1 cache hits] (3.0ms)
  Rendered languages/index.html.erb within layouts/application (10.9ms)
Completed 200 OK in 50ms (Views: 46.2ms | ActiveRecord: 1.2ms)

Why am I still seeing database queries with each request?


Solution

  • I was close before but now have successfully implemented caching. The above answer was appreciated but not what I was looking for, this is my current setup.

    I was calling the Language model query in a Helper module when I should have moved it to the model.

    Model

    after_save :clear_cache
    after_destroy :clear_cache
    
    def clear_language_cache
      Rails.cache.delete('Language.active.approved')
    end
    
    def self.active_approved
      Rails.cache.fetch('Language.active.approved') { is_active.is_approved.order(created_at: :desc).to_a }
    end
    

    Controller

    class LanguagesController < ApplicationController
      def index
        @languages = Language.active_approved
      end
    end
    

    Console

    Started GET "/languages" for 127.0.0.1 at 2018-08-22 18:13:21 -0400
    Processing by LanguagesController#index as HTML
      Rendering languages/index.html.erb within layouts/application
      Rendered collection of languages/_language.html.erb [1 / 1 cache hits] (11.0ms)
      Rendered languages/index.html.erb within layouts/application (15.9ms)
    Completed 200 OK in 68ms (Views: 45.5ms | ActiveRecord: 5.8ms)