diff options
author | Konstantin Haase <konstantin.mailinglists@googlemail.com> | 2012-09-04 15:50:09 +0200 |
---|---|---|
committer | Konstantin Haase <konstantin.mailinglists@googlemail.com> | 2012-09-04 15:51:06 +0200 |
commit | 1cb684c2ef12eea5661b17903f4986edfba0a464 (patch) | |
tree | a9bb9147a136d5b982df49a28f402bf0d26d93c0 /guides | |
parent | db354204b7656b010630448248b112ce39c4cf41 (diff) | |
download | rails-1cb684c2ef12eea5661b17903f4986edfba0a464.tar.gz rails-1cb684c2ef12eea5661b17903f4986edfba0a464.tar.bz2 rails-1cb684c2ef12eea5661b17903f4986edfba0a464.zip |
update caching guide: stale? can also figure out last_modified on its own
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/caching_with_rails.textile | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/guides/source/caching_with_rails.textile b/guides/source/caching_with_rails.textile index f6d6c8b550..9f1ac18814 100644 --- a/guides/source/caching_with_rails.textile +++ b/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 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 you’ve got an easy helper in fresh_when: <ruby> |