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

This commit is contained in:
Konstantin Haase 2012-09-04 15:50:09 +02:00
parent db354204b7
commit 1cb684c2ef
1 changed files with 12 additions and 1 deletions

View File

@ -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
end
@ -453,6 +453,17 @@ class ProductsController < ApplicationController
end
</ruby>
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+:
<ruby>
class ProductsController < ApplicationController
def show
@product = Product.find(params[:id])
respond_with(@product) if stale?(@product)
end
end
</ruby>
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 youve got an easy helper in fresh_when:
<ruby>