【发布时间】:2015-11-12 16:03:46
【问题描述】:
我有两个实现一个简单的控制器。
在第一个实现中,一切正常,动作显示仅在缓存丢失时执行。 但我知道在缓存操作的 Proc 中设置对象 @page 是个坏主意。
这就是为什么我有第二个实现,看起来好多了。
它也可以工作并返回缓存视图。
但是,我不明白为什么当我使用before_filter 时,即使缓存命中,动作show 仍在执行。在日志中我看到当前时间。
你能解释一下为什么吗? 请。
实施 1
class Frontend::StaticPagesController < Frontend::FrontendController
caches_action :show, :cache_path => Proc.new {
@page = StaticPage.find_in_cache(params[:permalink])
{key: "#{@page.cache_key}-#{I18n.locale}"}
}
def show
logger.debug Time.now.to_s.yellow
end
end
实施 2
class Frontend::StaticPagesController < Frontend::FrontendController
before_filter :set_page, :show
caches_action :show, :cache_path => Proc.new {
{key: "#{@page.cache_key}-#{I18n.locale}"}
}
def show
logger.debug Time.now.to_s.yellow
end
def set_page
@page = StaticPage.find_in_cache(params[:permalink])
end
end
附:导轨'4.2.3'
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 caching