Rails avoiding chunk caching requests in basic show action

I'm playing around with fragment caching, I've read manuals and watched railscast.

I'm trying to do some fragment caching on a basic show action:

Controller:

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

      

View:

<% cache @post do %>

  <h1><%= @post.title %></h1>
  <%= @post.content %>

<% end %>

      

Problem: while the snippet is building and reading (see logs below) the database is still getting hit. Is this normal behavior?

I suspect the before action filter triggers the request before reading the cache.

I suspected a friendly identification system, but the question also happens to be a classic find.

How should I cache this to avoid the request?

Logs:

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)

      

+3


source to share


1 answer


If your application only has one message, I really don't think you want to cache the first call. Fragment caching caches a given message, but it still needs to know which message is being accessed.



If you do cache memory, every time you load the # post page, no matter what you asked for, you will see one post - the post that was cached.

+1


source







All Articles