6

My controller has this:

caches_action :render_ticker_for_channel, :expires_in => 30.seconds

In my routes file I have this:

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

In the log file I see this:

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

How do I expire this manually? I need to expire this from a different controller than the render controller, but even within the render controller I can't get it to expire the right thing.

If I do:

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

I see:

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

If I do:

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

I see:

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

This:

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

produces:

Expire fragment views/render_c_t/63 (3.5ms)

How can I get it to expire the same path that 'caches_action' is producing?!

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
phil
  • 4,668
  • 4
  • 33
  • 51
  • I'm not confident enough to add this as a full fledged answer, but have you tried the back door? `Rails.application.routes.url_helpers` will give you access to your helpers and returns a string that's the path and/or url that would be sent to the browser in a Location header. Try `Rails.application.routes.url_helpers.render_ticker_for_channel_path(63)` and `Rails.application.routes.url_helpers.render_ticker_for_channel(63, :host => 'mcr3.dev')` You might be able to pass the resulting string (with some manipulation) to `expire_action` and get it find the right cached chunk. Ugly, but might work – Rob Cameron Sep 08 '11 at 22:22

3 Answers3

5

Use the regex version of expire_fragment:

expire_fragment %r{render_c_t/#{c.id}/}
klochner
  • 8,077
  • 1
  • 33
  • 45
0

There should definitely be a more "Rails Way" to do this, but this might work as a back door: Rails.application.routes.url_helpers will give you access to your helpers and each will return a string that's the path and/or url that would be sent to the browser in a Location header.

Try Rails.application.routes.url_helpers.render_ticker_for_channel_path(63) which should return /render_c_t/63 and Rails.application.routes.url_helpers.render_ticker_for_channel(63, :host => 'mcr3.dev') which should return http://mcr3.dev/render_c_t/63

With some manipulation you could parse apart that second string to get back to the name that Rails is using for the cached action:

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))

Not the most beautiful solution, but should work!

Rob Cameron
  • 9,674
  • 7
  • 39
  • 42
-1
caches_action :render_ticker_for_channel, :if => proc do 
  !!params['doCache']
end

But for this solution to work we need to pass an extra param either through query string or post body.

Rajkamal Subramanian
  • 6,884
  • 4
  • 52
  • 69