对缓存的迫切需求似乎是一种代码味道。代替猜测,您是否尝试过分析?(例如http://hiltmon.com/blog/2012/02/27/quick-and-dirty-rails-performance-profiling/)
gem 'ruby-prof'
定义分析器:
# /lib/development_profiler.rb
class DevelopmentProfiler
def self.prof(file_name)
RubyProf.start
yield
results = RubyProf.stop
# Print a flat profile to text
File.open "#{Rails.root}/tmp/performance/#{file_name}-graph.html", 'w' do |file|
RubyProf::GraphHtmlPrinter.new(results).print(file)
end
File.open "#{Rails.root}/tmp/performance/#{file_name}-flat.txt", 'w' do |file|
# RubyProf::FlatPrinter.new(results).print(file)
RubyProf::FlatPrinterWithLineNumbers.new(results).print(file)
end
File.open "#{Rails.root}/tmp/performance/#{file_name}-stack.html", 'w' do |file|
RubyProf::CallStackPrinter.new(results).print(file)
end
end
end
用...包装您的视图逻辑
DevelopmentProfiler::prof("render-profiling") do
# Your slow code here
end
编辑 - 一个额外的想法:因为你的模型数据不太可能改变,一次吃渲染成本可能会更好。您可以在 after_save 回调中静态生成整个呈现的页面。然后只需为后续请求提供该单个文件。
虽然如果向更传统的缓存收费不是很大的不便,但这可能比它的价值更麻烦。