rails 3缓存:命名路由的到期操作

我的控制器有这个:

caches_action :render_ticker_for_channel, :expires_in => 30.seconds 

在我的路线文件中,我有这个:

 match '/render_c_t/:channel_id' => 'render#render_ticker_for_channel', :as => :render_channel_ticker 

在日志文件中我看到:

 Write fragment views/mcr3.dev/render_c_t/63 (11.6ms) 

我如何手动过期? 我需要从与渲染控制器不同的控制器使其到期,但即使在渲染控制器中,我也无法让它过期正确的事情。

如果我做:

  expire_action(:controller => 'render', :action => 'render_ticker_for_channel', :id => c.id) 

我知道了:

 Expire fragment views/mcr3.dev/render/render_ticker_for_channel/63 (3.2ms) 

如果我做:

 expire_action(:controller => 'render', :action => 'render_c_t', :id => c.id) 

我知道了:

 Expire fragment views/mcr3.dev/render/render_c_t/63 (3.2ms) 

这个:

 expire_action("render_c_t/#{c.id}") 

生产:

 Expire fragment views/render_c_t/63 (3.5ms) 

我怎样才能让它过期’caches_action’产生的路径?!

使用expire_fragment的正则表达式版本:

 expire_fragment %r{render_c_t/#{c.id}/} 

应该有更多的“Rails方式”来做到这一点,但这可能作为一个后门: Rails.application.routes.url_helpers将让你访问你的助手,每个将返回一个字符串,它是路径和/或url将在Location标头中发送到浏览器。

尝试Rails.application.routes.url_helpers.render_ticker_for_channel_path(63) ,它应返回/render_c_t/63Rails.application.routes.url_helpers.render_ticker_for_channel(63, :host => 'mcr3.dev') ,它们应返回http://mcr3.dev/render_c_t/63

通过一些操作,您可以解析第二个字符串以回到Rails用于缓存操作的名称:

 def funky_action_cache_name(route, params) Rails.application.routes.url_helpers.send(route.to_s+'_url', params).gsub(/https?:\/\//,'') end # expire_action(funky_action_cache_name(:render_ticker_for_channel, :id => 63)) 

不是最美丽的解决方案,但应该工作!

 caches_action :render_ticker_for_channel, :if => proc do !!params['doCache'] end 

但是要使这个解决方案起作用,我们需要通过查询字符串或post体来传递额外的参数。