我在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,但从日志来看,应用程序似乎仍在访问数据库。
发布于 2012-05-07 00:59:44
听起来像是你在找fragment caching。
试着把它放在一个部分中,然后这样做:
<% cache do %>
<% Posts.published.recent.each do |p| %>
<%= link_to p.name, post_url(p) %>
<% end %>
<% end %>https://stackoverflow.com/questions/10472337
复制相似问题