简体   繁体   English

Rails 5.2.1-模型和片段缓存

[英]Rails 5.2.1 - Model & Fragment Caching

I am trying to setup Model and Fragment Caching in Rails 5.2.1 我正在尝试在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 app / views / languages / index.html.erb

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

app/views/languages/_language.html.erb 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? 为什么我仍然在每个请求中看到数据库查询?

What seems to be going on here is that you're caching the relation before it loads the records, and so it still has to actually load them before it can be used (using my Ad model because it's there and convenient for testing in irb): 这里似乎正在发生的事情是,您在加载记录之前先缓存该关系,因此它仍然必须实际加载它们才能使用(使用我的Ad模型,因为它存在并且可以在irb中进行测试) :

ads = Ad.all;nil # no query here, this is what I think you're caching
# this next line is where the query is run, (this would be
# equivalent to your render line)
ads.each { ... } 
#  Ad Load (0.1ms)  SELECT "ads".* FROM "ads"

instead you might try forcing active record to load the relation before you cache it and see if that helps. 相反,您可以在缓存它之前尝试强制活动记录加载该关系,看看是否有帮助。 You can do this by using load : 您可以使用load来做到这一点:

ads = Ad.all.load;nil # query is now run here
#  Ad Load (0.1ms)  SELECT "ads".* FROM "ads"
ads.each { ... } # and not run here

and together with caching enabled (all in a single rails console session, multiple sessions seems to forget the cache from the previous, but I've not configured caching so probably just an in-memory store of some kind) 并启用了缓存(所有都在一个Rails控制台会话中,多个会话似乎忘记了以前的缓存,但是我尚未配置缓存,因此可能只是某种内存存储)

ads = Rails.cache.fetch("test load") { Ad.all.load };nil # query
#  Ad Load (0.9ms)  SELECT "ads".* FROM "ads"
ads = Rails.cache.fetch("test load") { Ad.all.load };nil # no query
ads.each {  } # no query
ads = Rails.cache.fetch("test without load") { Ad.all };nil # no query
ads.each { };nil # query
#  Ad Load (0.1ms)  SELECT "ads".* FROM "ads"
ads = Rails.cache.fetch("test without load") { Ad.all };nil # no query
ads.each { };nil # query
#  Ad Load (0.1ms)  SELECT "ads".* FROM "ads"

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. 我应该在Helper模块中调用语言模型查询时将其移至模型中。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM