私はフラグメントキャッシングで遊んでいます。ガイドを読み、レールキャストを見ました。
基本的な show アクションでフラグメント キャッシュを作成しようとしています。
コントローラ:
class PostsController < ApplicationController
before_action :set_post, only: [:show]
def show
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.friendly.find(params[:id])
# @post = Post.find(params[:id])
end
end
意見:
<% cache @post do %>
<h1><%= @post.title %></h1>
<%= @post.content %>
<% end %>
問題: フラグメントがビルドされて読み取られているのに (以下のログを参照)、データベースがまだヒットしています。これは正常な動作ですか?
キャッシュが読み取られる前にクエリをトリガーする前のアクションフィルターを疑っています。
私はフレンドリIDシステムを疑っていましたが、クエリは従来の検索でも発生します。
クエリを回避するには、これをどのようにキャッシュする必要がありますか?
ログ:
Started GET "/articles/article-3" for 127.0.0.1 at 2014-08-27 10:05:14 -0400
Processing by PostsController#show as HTML
Parameters: {"id"=>"article-3"}
Post Load (1.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."slug" = 'article-3' ORDER BY "posts"."id" ASC LIMIT 1
Cache digest for app/views/posts/show.html.erb: 18a5c19e6efef2fd1ac4711102048e1c
Read fragment views/posts/3-20140730194235000000000/18a5c19e6efef2fd1ac4711102048e1c (0.5ms)
Rendered posts/show.html.erb within layouts/application (4.8ms)