1

我在 ApplicationController 中创建了一个辅助方法,允许访问所有页面上的最新帖子。

有什么办法可以缓存它以阻止我的应用程序经常访问数据库?

class ApplicationController < ActionController::Base
  protect_from_forgery
  helper_method :recent_posts

  def recent_posts
    @recent_posts = Post.published.recent
  end
end

我已经尝试过 cache_action :recent_posts 但是,从查看日志来看,应用程序似乎仍然命中了数据库。

4

1 回答 1

0

听起来您正在寻找片段缓存

试着把它放在一个部分并做这样的事情:

<% cache do %>
  <% Posts.published.recent.each do |p| %>
    <%= link_to p.name, post_url(p) %>
  <% end %>
<% end %>
于 2012-05-06T16:59:44.300 回答