【问题标题】:Rails caching: stale? always returns true in developmentRails 缓存:陈旧?在开发中总是返回 true
【发布时间】:2016-05-14 03:03:23
【问题描述】:

在我的个人模型中遵循hawkins.io

def self.cache_key
   Digest::MD5.hexdigest "#{maximum(:updated_at)}.try(:to_i)-#{count}"
end

我正在使用 Pundit 进行授权。所以在我的人员控制器中,我有:

def show
    @person = Person.find(params[:id])
    if authorize @person
      if stale? @person
        @person = Person.basic_details.last_details.find(params[:id]).decorate
        @person_histories = PersonHistory.new(person_id: @person.id).results
        respond_with @person
      end
    end
end

在我的 development.rb 环境中:

config.cache_store = :file_store, Rails.root.join('tmp', 'cache'), { expires_in: 4.hours }
config.consider_all_requests_local       = true
config.action_controller.perform_caching = true

(我在 Windows 上,所以没有 memcached 等设置)。

当我重新加载人员显示视图时,在加载它之后,我希望它会被完全缓存。然而它要求数据库等等等。是否有设置或我缺少的东西?当我检查缓存键时,它们是相同的,但 stale? @person 似乎总是返回 true。

【问题讨论】:

  • 它必须从数据库中进行人员查找,那么您认为应该消失的数据库查询到底是什么?
  • 那是为了一个例子而整理我的代码太多的例子!这里第二个@person 调用链接了很多模型,大约需要 200-400 毫秒。第二次调用略少。如果没有任何更新,我真的想避免这些调用。

标签: ruby-on-rails caching


【解决方案1】:

你试过了吗?

禁用 Rack::MiniProfiler 缓存

Rack::MiniProfiler 中间件将删除与缓存相关的标头,因此是否过时?将始终返回 TRUE。我们可以使用以下初始化程序完全禁用缓存:

Rack::MiniProfiler.config.disable_caching = false

【讨论】:

  • 我会试试看它是否像我使用 MiniProfiler 一样工作。
  • 您是否在后续请求中发送 If-None_Match 和/或 If_Modified-Since 标头中的 etag 和/或 last_modified 值?如果不这样做,stale 将始终返回 true。
  • 在人员控制器中我有etag { [current_user.try(:id), current_user.try(:locale)] }
  • 您需要在后续请求中发送标头中的 etag [curl -I localhost:3000/books/1 --header 'If-None-Match: "ETAG"']
  • 谢谢。这引起了数小时的沮丧。如果没有 Rack::MiniProfiler 中的配置,if stale? 将返回 true。
猜你喜欢
  • 1970-01-01
  • 2021-11-07
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 2016-05-20
  • 1970-01-01
  • 2012-10-12
相关资源
最近更新 更多