views:

258

answers:

1

I have a controller action which could benefit from caching. However, when I turn on action caching via the usual:

  caches_action :myaction, :expires_in=>15.minutes

...caching doesn't get invoked. It looks like this is because the action is invoked using an HTTP POST. For similar actions invoked using HTTP GET, caching works fine.

I realise using a POST for this action is probably not great style and breaks resource routing conventions - presumably this is also why the response isn't being cached, even though it could be. However for now I'm stuck with it as this is what the client currently does and I can't change it easily.

So, is there a way to force caching for this method even though it is accessed via POST?

edit: I should clarify perhaps that the POST has no side effects, so it is safe to cache the action. It really should have been a GET in the first place, it just isn't and can't easily be changed for now. Also it does not matter for this that browsers or proxies won't cache the response.

+1  A: 

Are the contents of the post data the same every post? I suspect they arent and this is why the action wont cache.

A couple of ways to deal with this:

1) Forget about caches_action and use Rails.cache easily inside your controller to cache the expensive parts of your controller code

2) Use Rack Middleware/ Metal Endpoint to receive the post data from the other application and shoehorn the data to the shape you want.

edit:

Im running Rails 2.3.3 and i can confirm that it does cache POST requests

For the purpose of checking while your developing make sure you have set perform_caching to true in development.rb :

config.action_controller.perform_caching             = true

Also make sure its the same in production.rb

I tested this scenario with the following in my controller :

 caches_action :index

 def index
    @listings = Listing.find(:all)
 end

Using both GET and POST requests this cached as expected. Also i tried setting the http headers Cache-Control: no-cache on my post client and the action still cached

If you're running OSX use this awesome tool http://ditchnet.org/httpclient/ to create GET and POST requests

ADAM
The parameters are identical - is there other data with the post (headers?) that could cause it not to cache? I'm not seeing those in the logs - but perhaps the client is setting headers to require bypassing the cache? I will give the Rails.cache suggestion a go. For rack/metal do I need rails 2.3? I'm on 2.2.2 at present.
frankodwyer
See edits above to you comments.
ADAM