Browse files

update caching guide: stale? can also figure out last_modified on its…

… own
  • Loading branch information...
1 parent db35420 commit 1cb684c2ef12eea5661b17903f4986edfba0a464 @rkh rkh committed Sep 4, 2012
Showing with 12 additions and 1 deletion.
  1. +12 −1 guides/source/caching_with_rails.textile
@@ -439,7 +439,7 @@ class ProductsController < ApplicationController
# If the request is stale according to the given timestamp and etag value
# (i.e. it needs to be processed again) then execute this block
- if stale?(:last_modified => @product.updated_at.utc, :etag => @product)
+ if stale?(:last_modified => @product.updated_at.utc, :etag => @product.cache_key)
respond_to do |wants|
# ... normal response processing
@@ -453,6 +453,17 @@ class ProductsController < ApplicationController
+Instead of a options hash, you can also simply pass in a model, Rails will use the methods +updated_at+ and +cache_key+ for setting +last_modified+ and +etag+:
+class ProductsController < ApplicationController
+ def show
+ @product = Product.find(params[:id])
+ respond_with(@product) if stale?(@product)
+ end
If you don't have any special response processing and are using the default rendering mechanism (i.e. you're not using respond_to or calling render yourself) then you’ve got an easy helper in fresh_when:

0 comments on commit 1cb684c

Please sign in to comment.