Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3335 (closed defect: fixed)

Opened 3 years ago

Last modified 1 year ago

[PATCH] Action cache must only cache status 200 responses

Reported by: tom@craz8.com Assigned to: bitsweat
Priority: normal Milestone: 1.x
Component: ActionPack Version: edge
Severity: normal Keywords: action cache verified
Cc: halfbyte

Description

The Action Cache only caches the response body. The headers, including the status code, is not kept. If the response from the action is not a '200 OK' response, then when the cache serves up this response as a '200 OK' with no response body, the client will not be receiving the correct data.

My patch changes the _after_ filter for action caching to only cache responses that have a status of '200 OK'.

Attachments

caching.diff (0.7 kB) - added by tom@craz8.com on 12/27/05 05:30:25.
Patch file
action_caching_for_non_success_status_codes.diff (2.0 kB) - added by halfbyte on 02/22/07 21:44:41.
Combined Patch for caching.rb and for caching_test.rb
action_caching_for_non_success_status_codes.2.diff (2.8 kB) - added by josh on 06/08/07 00:06:00.

Change History

12/27/05 05:30:25 changed by tom@craz8.com

  • attachment caching.diff added.

Patch file

01/20/06 19:02:37 changed by anonymous

Please commit this patch!

06/16/06 08:50:54 changed by anonymous

  • keywords changed from action cache to action cache tiny.
  • priority changed from normal to high.

Please commit

08/29/06 05:57:44 changed by tom@craz8.com

  • severity changed from 1 to normal.
  • cc deleted.
  • component changed from 1 to ActionPack.
  • summary changed from Lee to [PATCH] Action cache must only cache status 200 responses.
  • priority changed from 1 to high.
  • version changed from 1 to 1.0.0.
  • milestone deleted.
  • keywords changed from Lee to action cache.
  • type changed from 1 to defect.

Revert spammed info

02/18/07 04:39:48 changed by josh

  • status changed from new to closed.
  • resolution set to untested.

Please reopen this ticket once you have provided unit tests of the existence of the defect.

02/22/07 21:44:41 changed by halfbyte

  • attachment action_caching_for_non_success_status_codes.diff added.

Combined Patch for caching.rb and for caching_test.rb

02/22/07 21:48:39 changed by halfbyte

  • cc set to halfbyte.
  • status changed from closed to reopened.
  • version changed from 1.0.0 to edge.
  • resolution deleted.

I submitted two simple tests in the >200 status code range

(302 by using redirect_to and 403 by setting the header by hand)

I also resubmitted the caching.rb patch in the diff to demonstrate the effect of toms test

02/22/07 21:58:43 changed by josh

  • keywords changed from action cache to action cache verified.

Awesome, all tests pass.

03/11/07 07:06:46 changed by dkubb

+1 for the idea behind this patch.

This patch should probably be updated to only match against the 3 digit status code "200" and ignore the reason phrase "OK". This is according to RFC 2616 (sec 6.1.1):

The Reason-Phrase is intended to give a short textual description
of the Status-Code. The Status-Code is intended for use by automata
and the Reason-Phrase is intended for the human user. The client is
not required to examine or display the Reason-Phrase.

I could technically set the Status to "200 Awesome" and it should still be cached. ;)

Also, should action caching work for responses to non-GET requests? I'm not entirely sure you'd want the response to a PUT or POST request to be cached. This would be consistent with page caching behaviour added in [5755]. Could the caching_allowed method (that's used in page caching) be used to determine whether or not to cache an action too?

06/08/07 00:06:00 changed by josh

  • attachment action_caching_for_non_success_status_codes.2.diff added.

06/08/07 00:07:29 changed by josh

  • owner changed from David to bitsweat.
  • priority changed from high to normal.
  • status changed from reopened to new.
  • milestone set to 1.x.

+1 Uploaded a revised patch with dkubb suggestions.

06/08/07 04:09:09 changed by bitsweat

Looks good. Should only 200 status be cached? What about e.g. 204 No Content? Or all 2xx?

06/08/07 04:32:29 changed by dkubb

I guess the question would be: how many GET/HEAD requests would result in something other than 200 OK? Not including Conditional requests that result in 304's, I can't think of too many others.

If you review the 2xx response codes most of them are usually the result of something other than GET. Even 204 is rarely used in response to GET (in most apps I've seen). its much more common to see 204 sent in response to stuff like DELETE requests.

I suppose the only 2xx that comes close is 206 Partial Content, but I'm not sure how you'd cache that, unless you worked in the Range request header into the cache key. Even in that case it would probably make more sense to cache the normal 200 OK response and just return the range of content when the client requests it. (if anyone's interested, I could submit this as a patch although it shouldn't delay this particular patch from being applied)

Anyone have any other scenarios where 2xx responses other than 200 OK need to be cached? I can't think of any.

06/08/07 04:59:01 changed by bitsweat

Indeed. This is a good step that we can iterate on later should the need arise. Thanks!

06/08/07 05:01:39 changed by bitsweat

  • status changed from new to closed.
  • resolution set to fixed.

(In [6970]) Action caching is limited to GET requests returning 200 OK status. Closes #3335.