开发者

will_paginate problem with action and fragment caching

开发者 https://www.devze.com 2023-02-02 06:01 出处:网络
i have added action caching to my index part and listing 9 records in a page will_paginate dosent work, and renders same set of recors again and again.

i have added action caching to my index part and listing 9 records in a page

will_paginate dosent work, and renders same set of recors again and again.

and same issue w开发者_如何转开发ith fragment caching

please suggest some solution for this..

thanks


try this: Rails action caching with querystring parameters


Unless you have a very basic Rails app, I would recommend using fragment caching as it's a much easier to use and maintain compared to action/page caching. If you decide to use fragment caching, you could add something like the following to your index template:

<% @models.each do |model| %>
  <% cache ["v1", model] do %>
    <%= render model %>
  <% end %>
<% end %>

This will render the partial _model.html.erb and cache the result. For details about the cache mechanisms in Rails, I suggest reading Rails Guides.

A more aggressive caching strategy would be to cache all the models on a single page. That could be done by setting the current page to an instance variable in the controller:

def index
  @page   = params[:page]
  @models = ...
end

Now in your template you can include the page in the composite cache key:

<% cache ["v1", cache_key_for(@models), @page] do %>
  <% @models.each do |model| %>
    <%= render model %>
  <% end %>
<% end %>

<% end %>

cache_key_for is a helper that computes a cache key for a set of models. It could be defined as:

def cache_key_for(models)
  "#{models.count}-{models.map(&:updated_at).max.utc.to_s(:number)}"
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号