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