使用包含的缓存响应

时间:2011-02-04 16:22:45

标签: ruby-on-rails caching include

我已经学会了如何使用Rails.cache.fetch函数来缓存数据库调用,但是,当我响应请求时,我包含了几个相关表的内容,例如

tracks = Rails.cache.fetch("events-by-track", :expires_in => 12.hours) do
  Track.find(:all)
end
respond_with(tracks, :include => {:events => {:include => :speaker}})

这个缓存(我认为)调用Track.find(:all),但它是否还缓存了包含的事件和发言者?

1 个答案:

答案 0 :(得分:0)

不,它不是缓存那些。您必须将这些内容放入调用中以包含它们。例如,像这样:

tracks = Rails.cache.fetch("events-by-track", :expires_in => 12.hours) do
  Track.find(:all, :include => {:events => :speaker})
end
respond_with(tracks)

关联对象数组的示例:

tracks, events = Rails.cache.fetch("events-by-track", :expires_in => 12.hours) do
  tracks = Track.find(:all, :include => [:events])
  events = tracks.collect(&:events)
  tracks, events
end
respond_with(tracks)