From 814c9875ec4cf01e1348d1ab1c9d12eee5a9adac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicholas=20Schultz-M=C3=B8ller?= Date: Tue, 15 Jan 2013 14:44:08 +0100 Subject: Improves documentation about Fragment Caching. [ci skip] Adds documentation for: - Aggregate cache keys, - using an ActiveRecord model as cache key and - Russian Doll Caching. --- guides/source/caching_with_rails.md | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'guides/source/caching_with_rails.md') diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md index 0228d463cf..c91483ed77 100644 --- a/guides/source/caching_with_rails.md +++ b/guides/source/caching_with_rails.md @@ -86,6 +86,53 @@ This fragment is then available to all actions in the `ProductsController` using expire_fragment('all_available_products') ``` +If you want to avoid expiring the fragment manually, whenever an action updates a product, you can define a helper method: + +```ruby +module ProductsHelper + def cache_key_for_products + count = Product.count + max_updated_at = Product.maximum(:updated_at).try(:utc).try(:to_s, :number) + "products/all-#{count}-#{max_updated_at}" + end +end +``` + +This method generates a cache key that depends on all products and can be used in the view: + +```erb +<% cache(cache_key_for_products) do %> + All available products: +<% end %> +``` + +You can also use an `ActiveRecord` model as the cache key: + +```erb +<% Product.all.each do |p| %> + <% cache(p) do %> + <%= link_to p.name, product_url(p) %> + <% end %> +<% end %> +``` + +Behind the scenes, a method called `cache_key` will be invoked on the model and it returns a string like `products/23-20130109142513`. The cache key includes the model name, the id and finally the `updated_at` timestamp. Thus it will automatically generate a new fragment when the product is updated because the key changes. + +You can also combine the two schemes which is called "Russian Doll Caching": + +```erb +<% cache(cache_key_for_products) do %> + All available products: + <% Product.all.each do |p| %> + <% cache(p) do %> + <%= link_to p.name, product_url(p) %> + <% end %> + <% end %> +<% end %> +``` + +It's called "Russian Doll Caching" because it nests multiple fragments. The advantage is that if a single product is updated, all the other inner fragments can be reused when regenerating the outer fragment. + ### SQL Caching Query caching is a Rails feature that caches the result set returned by each query so that if Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again. -- cgit v1.2.3 From 4dcc9ddddea12cca2d4e1a6160d0d2db1dfba6f2 Mon Sep 17 00:00:00 2001 From: davetoxa Date: Wed, 16 Jan 2013 01:12:37 +0600 Subject: Revert "Improves documentation about Fragment Caching. [ci skip]" This reverts commit 814c9875ec4cf01e1348d1ab1c9d12eee5a9adac. --- guides/source/caching_with_rails.md | 47 ------------------------------------- 1 file changed, 47 deletions(-) (limited to 'guides/source/caching_with_rails.md') diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md index c91483ed77..0228d463cf 100644 --- a/guides/source/caching_with_rails.md +++ b/guides/source/caching_with_rails.md @@ -86,53 +86,6 @@ This fragment is then available to all actions in the `ProductsController` using expire_fragment('all_available_products') ``` -If you want to avoid expiring the fragment manually, whenever an action updates a product, you can define a helper method: - -```ruby -module ProductsHelper - def cache_key_for_products - count = Product.count - max_updated_at = Product.maximum(:updated_at).try(:utc).try(:to_s, :number) - "products/all-#{count}-#{max_updated_at}" - end -end -``` - -This method generates a cache key that depends on all products and can be used in the view: - -```erb -<% cache(cache_key_for_products) do %> - All available products: -<% end %> -``` - -You can also use an `ActiveRecord` model as the cache key: - -```erb -<% Product.all.each do |p| %> - <% cache(p) do %> - <%= link_to p.name, product_url(p) %> - <% end %> -<% end %> -``` - -Behind the scenes, a method called `cache_key` will be invoked on the model and it returns a string like `products/23-20130109142513`. The cache key includes the model name, the id and finally the `updated_at` timestamp. Thus it will automatically generate a new fragment when the product is updated because the key changes. - -You can also combine the two schemes which is called "Russian Doll Caching": - -```erb -<% cache(cache_key_for_products) do %> - All available products: - <% Product.all.each do |p| %> - <% cache(p) do %> - <%= link_to p.name, product_url(p) %> - <% end %> - <% end %> -<% end %> -``` - -It's called "Russian Doll Caching" because it nests multiple fragments. The advantage is that if a single product is updated, all the other inner fragments can be reused when regenerating the outer fragment. - ### SQL Caching Query caching is a Rails feature that caches the result set returned by each query so that if Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again. -- cgit v1.2.3 From cc585c8b01105064a2e74bd4f9f5e86616a23c2b Mon Sep 17 00:00:00 2001 From: davetoxa Date: Wed, 16 Jan 2013 01:26:47 +0600 Subject: revert to 814c9875ec4cf01e1348d1ab1c9d12eee5a9adac --- guides/source/caching_with_rails.md | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'guides/source/caching_with_rails.md') diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md index 0228d463cf..99150de43a 100644 --- a/guides/source/caching_with_rails.md +++ b/guides/source/caching_with_rails.md @@ -85,6 +85,50 @@ This fragment is then available to all actions in the `ProductsController` using ```ruby expire_fragment('all_available_products') ``` +If you want to avoid expiring the fragment manually, whenever an action updates a product, you can define a helper method: + +```ruby +module ProductsHelper + def cache_key_for_products + count = Product.count + max_updated_at = Product.maximum(:updated_at).try(:utc).try(:to_s, :number) + "products/all-#{count}-#{max_updated_at}" + end +end +``` + +This method generates a cache key that depends on all products and can be used in the view: + +```ruby +<% cache(cache_key_for_products) do %> + All available products: +<% end %> +``` +You can also use an `ActiveRecord` model as the cache key: + +```ruby +<% Product.all.each do |p| %> + <% cache(p) do %> + <%= link_to p.name, product_url(p) %> + <% end %> +<% end %> +``` + +Behind the scenes, a method called `cache_key` will be invoked on the model and it returns a string like `products/23-20130109142513`. The cache key includes the model name, the id and finally the updated_at timestamp. Thus it will automatically generate a new fragment when the product is updated because the key changes. + +You can also combine the two schemes which is called "Russian Doll Caching": + +```ruby +<% cache(cache_key_for_products) do %> + All available products: + <% Product.all.each do |p| %> + <% cache(p) do %> + <%= link_to p.name, product_url(p) %> + <% end %> + <% end %> +<% end %> +It's called "Russian Doll Caching" because it nests multiple fragments. The advantage is that if a single product is updated, all the other inner fragments can be reused when regenerating the outer fragment. +``` ### SQL Caching -- cgit v1.2.3 From 728f9c2eed42959d5a3e9202ba80635872738c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicholas=20Schultz-M=C3=B8ller?= Date: Wed, 16 Jan 2013 11:44:55 +0100 Subject: Fix formatting error introduced in cc585c8. --- guides/source/caching_with_rails.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'guides/source/caching_with_rails.md') diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md index 99150de43a..7e33edda86 100644 --- a/guides/source/caching_with_rails.md +++ b/guides/source/caching_with_rails.md @@ -127,9 +127,10 @@ You can also combine the two schemes which is called "Russian Doll Caching": <% end %> <% end %> <% end %> -It's called "Russian Doll Caching" because it nests multiple fragments. The advantage is that if a single product is updated, all the other inner fragments can be reused when regenerating the outer fragment. ``` +It's called "Russian Doll Caching" because it nests multiple fragments. The advantage is that if a single product is updated, all the other inner fragments can be reused when regenerating the outer fragment. + ### SQL Caching Query caching is a Rails feature that caches the result set returned by each query so that if Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again. -- cgit v1.2.3 From 03a391482aa6a9d532eb43bb3e697852e0b83618 Mon Sep 17 00:00:00 2001 From: Cyril Jouve Date: Sun, 20 Jan 2013 03:38:45 +0100 Subject: consistently inherit from ApplicationController in guides exemples --- guides/source/caching_with_rails.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/caching_with_rails.md') diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md index 7e33edda86..7e4253b1ba 100644 --- a/guides/source/caching_with_rails.md +++ b/guides/source/caching_with_rails.md @@ -138,7 +138,7 @@ Query caching is a Rails feature that caches the result set returned by each que For example: ```ruby -class ProductsController < ActionController +class ProductsController < ApplicationController def index # Run a find query -- cgit v1.2.3 From 4313461587254fd8e5b5a8ea7dfc9f0230c70ecb Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 26 Jan 2013 17:36:38 +0100 Subject: generic pass before merging docrails --- guides/source/caching_with_rails.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/caching_with_rails.md') diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md index 7e4253b1ba..e52264f296 100644 --- a/guides/source/caching_with_rails.md +++ b/guides/source/caching_with_rails.md @@ -104,7 +104,7 @@ This method generates a cache key that depends on all products and can be used i All available products: <% end %> ``` -You can also use an `ActiveRecord` model as the cache key: +You can also use an Active Record model as the cache key: ```ruby <% Product.all.each do |p| %> -- cgit v1.2.3