1
就如何緩存jbuilder視圖和activerecord查詢的一些建議。我現在做這件事的方式感覺不對,因爲我實際上在緩存中存儲了兩件東西。我可以以某種方式組合嗎?我需要緩存SQL記錄,以便數據庫不會受到影響,並且還需要查看文件以最大限度地提高速度。Rails的jbuilder緩存+查詢
# Controller
@posts = Rails.cache.fetch ["posts"], :expires_in => 1.hour do
Post.all.limit(10).order("id desc").to_a
end
和
# Jbuilder view
json.cache! ["posts"], :expires_in => 1.hour do |json|
json.array! @posts do |post|
json.id post.id
json.title post.title
end
end