【发布时间】: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