aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/caching_with_rails.md
diff options
context:
space:
mode:
authorVijay Dev <vijaydev.cse@gmail.com>2012-11-17 01:50:49 +0530
committerVijay Dev <vijaydev.cse@gmail.com>2012-11-17 01:50:49 +0530
commit7b70eeed43045dc29e73e23fbfdc323e9d397026 (patch)
tree8c60cd5893f7e2d231130b7c0abdc0aee8e3bec7 /guides/source/caching_with_rails.md
parent8eefdb6d7056dc0d4d63a5c34a4b12701ba21c88 (diff)
parent1fd008cd44cd2eea37db57ee6b3c17d3585d88c1 (diff)
downloadrails-7b70eeed43045dc29e73e23fbfdc323e9d397026.tar.gz
rails-7b70eeed43045dc29e73e23fbfdc323e9d397026.tar.bz2
rails-7b70eeed43045dc29e73e23fbfdc323e9d397026.zip
Merge branch 'master' of github.com:lifo/docrails
Conflicts: actionpack/lib/action_dispatch/routing/redirection.rb
Diffstat (limited to 'guides/source/caching_with_rails.md')
-rw-r--r--guides/source/caching_with_rails.md44
1 files changed, 22 insertions, 22 deletions
diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md
index 08f1ef879d..4cb76bfe5f 100644
--- a/guides/source/caching_with_rails.md
+++ b/guides/source/caching_with_rails.md
@@ -5,10 +5,10 @@ This guide will teach you what you need to know about avoiding that expensive ro
After reading this guide, you should be able to use and configure:
-* Page, action, and fragment caching.
-* Sweepers.
-* Alternative cache stores.
-* Conditional GET support.
+* Page, action, and fragment caching
+* Sweepers
+* Alternative cache stores
+* Conditional GET support
--------------------------------------------------------------------------------
@@ -61,7 +61,7 @@ class ProductsController < ActionController
end
def create
- expire_page :action => :index
+ expire_page action: :index
end
end
@@ -82,13 +82,13 @@ location / {
You can disable gzipping by setting `:gzip` option to false (for example, if action returns image):
```ruby
-caches_page :image, :gzip => false
+caches_page :image, gzip: false
```
Or, you can set custom gzip compression level (level names are taken from `Zlib` constants):
```ruby
-caches_page :image, :gzip => :best_speed
+caches_page :image, gzip: :best_speed
```
NOTE: Page caching ignores all parameters. For example `/products?page=1` will be written out to the filesystem as `products.html` with no reference to the `page` parameter. Thus, if someone requests `/products?page=2` later, they will get the cached first page. A workaround for this limitation is to include the parameters in the page's path, e.g. `/products/page/1`.
@@ -114,13 +114,13 @@ class ProductsController < ActionController
end
def create
- expire_action :action => :index
+ expire_action action: :index
end
end
```
-You can also use `:if` (or `:unless`) to pass a Proc that specifies when the action should be cached. Also, you can use `:layout => false` to cache without layout so that dynamic information in the layout such as logged in user info or the number of items in the cart can be left uncached. This feature is available as of Rails 2.2.
+You can also use `:if` (or `:unless`) to pass a Proc that specifies when the action should be cached. Also, you can use `layout: false` to cache without layout so that dynamic information in the layout such as logged in user info or the number of items in the cart can be left uncached. This feature is available as of Rails 2.2.
You can modify the default action cache path by passing a `:cache_path` option. This will be passed directly to `ActionCachePath.path_for`. This is handy for actions with multiple possible routes that should be cached differently. If a block is given, it is called with the current controller instance.
@@ -152,14 +152,14 @@ As an example, if you wanted to show all the orders placed on your website in re
The cache block in our example will bind to the action that called it and is written out to the same place as the Action Cache, which means that if you want to cache multiple fragments per action, you should provide an `action_suffix` to the cache call:
```html+erb
-<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
+<% cache(action: 'recent', action_suffix: 'all_products') do %>
All available products:
```
and you can expire it using the `expire_fragment` method, like so:
```ruby
-expire_fragment(:controller => 'products', :action => 'recent', :action_suffix => 'all_products')
+expire_fragment(controller: 'products', action: 'recent', action_suffix: 'all_products')
```
If you don't want the cache block to bind to the action that called it, you can also use globally keyed fragments by calling the `cache` method with a key:
@@ -206,7 +206,7 @@ class ProductSweeper < ActionController::Caching::Sweeper
private
def expire_cache_for(product)
# Expire the index page now that we added a new product
- expire_page(:controller => 'products', :action => 'index')
+ expire_page(controller: 'products', action: 'index')
# Expire a fragment
expire_fragment('all_available_products')
@@ -217,7 +217,7 @@ end
You may notice that the actual product gets passed to the sweeper, so if we were caching the edit action for each product, we could add an expire method which specifies the page we want to expire:
```ruby
-expire_action(:controller => 'products', :action => 'edit', :id => product.id)
+expire_action(controller: 'products', action: 'edit', id: product.id)
```
Then we add it to our controller to tell it to call the sweeper when certain actions are called. So, if we wanted to expire the cached content for the list and edit actions when the create action was called, we could do the following:
@@ -265,7 +265,7 @@ class ProductSweeper < ActionController::Caching::Sweeper
observe Product
def after_create(product)
- expire_action(:controller => '/products', :action => 'index')
+ expire_action(controller: '/products', action: 'index')
end
end
```
@@ -340,13 +340,11 @@ There are some common options used by all cache implementations. These can be pa
This cache store keeps entries in memory in the same Ruby process. The cache store has a bounded size specified by the `:size` options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur and the least recently used entries will be removed.
```ruby
-config.cache_store = :memory_store, { :size => 64.megabytes }
+config.cache_store = :memory_store, { size: 64.megabytes }
```
If you're running multiple Ruby on Rails server processes (which is the case if you're using mongrel_cluster or Phusion Passenger), then your Rails server process instances won't be able to share cache data with each other. This cache store is not appropriate for large application deployments, but can work well for small, low traffic sites with only a couple of server processes or for development and test environments.
-This is the default cache store implementation.
-
### ActiveSupport::Cache::FileStore
This cache store uses the file system to store entries. The path to the directory where the store files will be stored must be specified when initializing the cache.
@@ -359,6 +357,8 @@ With this cache store, multiple server processes on the same host can share a ca
Note that the cache will grow until the disk is full unless you periodically clear out old entries.
+This is the default cache store implementation.
+
### ActiveSupport::Cache::MemCacheStore
This cache store uses Danga's `memcached` server to provide a centralized cache for your application. Rails uses the bundled `dalli` gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very a high performance and redundancy.
@@ -394,8 +394,8 @@ In addition to the standard `:expires_in` option, the `write` method on this cac
These options are passed to the `write` method as Hash options using either camelCase or underscore notation, as in the following examples:
```ruby
-Rails.cache.write('key', 'value', :time_to_idle => 60.seconds, :timeToLive => 600.seconds)
-caches_action :index, :expires_in => 60.seconds, :unless_exist => true
+Rails.cache.write('key', 'value', time_to_idle: 60.seconds, timeToLive: 600.seconds)
+caches_action :index, expires_in: 60.seconds, unless_exist: true
```
For more information about Ehcache, see [http://ehcache.org/](http://ehcache.org/) .
@@ -427,7 +427,7 @@ You can use Hashes and Arrays of values as cache keys.
```ruby
# This is a legal cache key
-Rails.cache.read(:site => "mysite", :owners => [owner_1, owner_2])
+Rails.cache.read(site: "mysite", owners: [owner_1, owner_2])
```
The keys you use on `Rails.cache` will not be the same as those actually used with the storage engine. They may be modified with a namespace or altered to fit technology backend constraints. This means, for instance, that you can't save values with `Rails.cache` and then try to pull them out with the `memcache-client` gem. However, you also don't need to worry about exceeding the memcached size limit or violating syntax rules.
@@ -449,7 +449,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.cache_key)
+ if stale?(last_modified: @product.updated_at.utc, etag: @product.cache_key)
respond_to do |wants|
# ... normal response processing
end
@@ -484,7 +484,7 @@ class ProductsController < ApplicationController
def show
@product = Product.find(params[:id])
- fresh_when :last_modified => @product.published_at.utc, :etag => @product
+ fresh_when last_modified: @product.published_at.utc, etag: @product
end
end
```