1

私のrails4アプリには、投稿用に次の構造があります。ユーザーは投稿にコメントでき、コメントに返信を書き込むことができます。ページで自動期限切れキーを使用して russian-doll-caching を使用したいのですが、この場合、どのようにすればよいかわかりません。

この場合の使い方を教えてもらえますか?

モデル:

#post.rb

belongs_to :user
has_many :post_comments, dependent: :destroy

#post_comments.rb

belongs_to :user
belongs_to :post
has_many :post_comment_replies, dependent: :destroy

#post_comment_replies.rb

belongs_to :user
belongs_to :post_comments

投稿/index.html.erb

 <div class="post-index new-post-insert">
   <%= render @posts %>
 </div>

_post.html.erb

<%= post.body %>
<%= post.user.full_name %>
....
<%= render partial: 'posts/post_comments/post_comment', collection: post.post_comments.ordered.included, as: :post_comment, locals: {post: post} %>

_post_comment.html.erb

<%= post_comment.body %>
<%= post_comment.user.full_name %>
......
<%= render partial: 'posts/post_comment_replies/post_comment_reply', collection: post_comment.post_comment_replies.ordered.included, as: :post_comment_reply, locals: { post_comment: post_comment } %>

_post_comment_reply.html.erb

<%= post_comment_reply.user.full_name %>
<%= post_comment_reply.body %>
4

1 に答える 1

1

あなたはいくつかのことをする必要があります

belongs_toあなたの関係にタッチを追加します

の子と孫は、列が更新されてキャッシュ キーが無効になるPostように、親にアクセスする必要があります。updated_at

#post_comments.rb

belongs_to :user
belongs_to :post, touch: true
has_many :post_comment_replies, dependent: :destroy

#post_comment_replies.rb

belongs_to :user
belongs_to :post_comments, touch: true

cacheコマンドをビューに追加します

投稿/index.html.erb

投稿のメイン リストで、最新updated_atの投稿とupdated_at各ユーザーの最新をキャッシュします。

 <div class="post-index new-post-insert">
    <% cache ["posts", @posts.maximum(:updated_at).to_i, @posts.map {|p| p.user.try(:updated_at).to_i}.max] %>
     <%= render @posts %>
    <% end %>
 </div>

_post.html.erb

<% cache ["postlist", post, post.user] %>
  <%= post.body %>
  <%= post.user.full_name %>
  ....
  <%= render partial: 'posts/post_comments/post_comment', collection: post.post_comments.ordered.included, as: :post_comment, locals: {post: post} %>
<% end %>

_post_comment.html.erb

<% cache ["postcommentlist", post_comment, post_comment.user] %>
  <%= post_comment.body %>
  <%= post_comment.user.full_name %>
  ......
  <%= render partial: 'posts/post_comment_replies/post_comment_reply', collection: post_comment.post_comment_replies.ordered.included, as: :post_comment_reply, locals: { post_comment: post_comment } %>
<% end %>

_post_comment_reply.html.erb

<% cache ["postcommentreplylist", post_comment_reply, post_comment_reply.user] %>
  <%= post_comment_reply.user.full_name %>
  <%= post_comment_reply.body %>
<% end %>

cached: trueこれは、関数で使用することで改善できますrender partial。ただし、ユーザーがユーザー名を変更した場合にキャッシュを期限切れにしたいので、少し注意が必要です。

cache_keyすべてのモデル関数をオーバーライドすれば、それを行うことができます。

で使用する必要があるのはなぜcached: trueですかrender partial?

cache(上記のように)各パーシャル内で呼び出す代わりに、次のことができます

<%= render partial: 'posts/post_comments/post_comment', collection: post.post_comments, cached: true %>

post_comment のupdated_at.

この 2 つの違いは、部分的なRails内にキャッシュするときにget、オブジェクトごとに 1 回、キャッシュストア (memcache など) にコマンドを発行することです。したがって、50 個のポストコメントがある場合、すべてを取得するために memcached に対して 50 個の個別のリクエストが発生します。

しかし、代わりにcached: truerender呼び出しで使用すると、Rails はmulti_getmemcached にリクエストを発行し、1 回のリクエストで 50 個のオブジェクトすべてを取得します。したがって、ページの読み込み時間が改善されます。テストでは、本番環境で実施しました。データの量に応じて、ページの読み込み時間が最大 50 ミリ秒から最大 200 ミリ秒短縮されました。

于 2016-03-31T18:13:58.487 に答える