caching_with_rails.textile 22.1 KB
Newer Older
1 2
h2. Caching with Rails: An overview

3
This guide will teach you what you need to know about avoiding that expensive round-trip to your database and returning what you need to return to the web clients in the shortest time possible.
4

P
Pratik Naik 已提交
5 6 7 8 9 10 11
After reading this guide, you should be able to use and configure:

* Page, action, and fragment caching
* Sweepers
* Alternative cache stores
* Conditional GET support

12 13 14 15
endprologue.

h3. Basic Caching

16
This is an introduction to the three types of caching techniques that Rails provides by default without the use of any third party plugins.
17

J
JudeArasu 已提交
18
To start playing with caching you'll want to ensure that +config.action_controller.perform_caching+ is set to +true+, if you're running in development mode. This flag is normally set in the corresponding +config/environments/*.rb+ and caching is disabled by default for development and test, and enabled for production.
19 20 21 22 23 24 25

<ruby>
config.action_controller.perform_caching = true
</ruby>

h4. Page Caching

26
Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the webserver (i.e. apache or nginx), without ever having to go through the Rails stack at all. Obviously, this is super-fast. Unfortunately, it can't be applied to every situation (such as pages that need authentication) and since the webserver is literally just serving a file from the filesystem, cache expiration is an issue that needs to be dealt with.
27

J
JudeArasu 已提交
28
So, how do you enable this super-fast cache behavior?. Simple, let's say you have a controller called +ProductsController+ and an +index+ action that lists all the products.
29 30 31 32

<ruby>
class ProductsController < ActionController

33
  caches_page :index
34

35
  def index
P
Pratik Naik 已提交
36 37
    @products = Products.all
  end
38 39 40 41

end
</ruby>

42
The first time anyone requests +/products+, Rails will generate a file called +products.html+ and the webserver will then look for that file before it passes the next request for +/products+ to your Rails application.
43

44
By default, the page cache directory is set to +Rails.public_path+ (which is usually set to the +public+ folder) and this can be configured by changing the configuration setting +config.action_controller.page_cache_directory+. Changing the default from +public+ helps avoid naming conflicts, since you may want to put other static html in +public+, but changing this will require web server reconfiguration to let the web server know where to serve the cached files from.
45

46
The Page Caching mechanism will automatically add a +.html+ extension to requests for pages that do not have an extension to make it easy for the webserver to find those pages and this can be configured by changing the configuration setting +config.action_controller.page_cache_extension+.
47

48
In order to expire this page when a new product is added we could extend our example controller like this:
49 50 51 52

<ruby>
class ProductsController < ActionController

53
  caches_page :index
54

55
  def index
P
Pratik Naik 已提交
56 57
    @products = Products.all
  end
58 59

  def create
E
eparreno 已提交
60
    expire_page :action => :index
61 62 63 64 65
  end

end
</ruby>

66
If you want a more complicated expiration scheme, you can use cache sweepers to expire cached objects when things change. This is covered in the section on Sweepers.
67

68
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. Be careful when page caching GET parameters in the URL!
69

X
Xavier Noria 已提交
70
INFO: Page caching runs in an after filter. Thus, invalid requests won't generate spurious cache entries as long as you halt them. Typically, a redirection in some before filter that checks request preconditions does the job.
71

72 73
h4. Action Caching

74
One of the issues with Page Caching is that you cannot use it for pages that require to restrict access somehow. This is where Action Caching comes in. Action Caching works like Page Caching except for the fact that the incoming web request does go from the webserver to the Rails stack and Action Pack so that before filters can be run on it before the cache is served. This allows authentication and other restriction to be run while still serving the result of the output from a cached copy.
75

P
Pratik Naik 已提交
76
Clearing the cache works in the exact same way as with Page Caching.
77

P
Pratik Naik 已提交
78
Let's say you only wanted authenticated users to call actions on +ProductsController+.
79 80 81 82

<ruby>
class ProductsController < ActionController

P
Pratik Naik 已提交
83 84
  before_filter :authenticate
  caches_action :index
85

P
Pratik Naik 已提交
86 87 88
  def index
    @products = Product.all
  end
89 90

  def create
P
Pratik Naik 已提交
91
    expire_action :action => :index
92 93 94 95 96
  end

end
</ruby>

97
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.
98

99
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.
100

101
Finally, if you are using memcached or Ehcache, you can also pass +:expires_in+. In fact, all parameters not used by +caches_action+ are sent to the underlying cache store.
102

X
Xavier Noria 已提交
103
INFO: Action caching runs in an after filter. Thus, invalid requests won't generate spurious cache entries as long as you halt them. Typically, a redirection in some before filter that checks request preconditions does the job.
104

105 106
h4. Fragment Caching

107
Life would be perfect if we could get away with caching the entire contents of a page or action and serving it out to the world. Unfortunately, dynamic web applications usually build pages with a variety of components not all of which have the same caching characteristics. In order to address such a dynamically created page where different parts of the page need to be cached and expired differently Rails provides a mechanism called Fragment Caching.
108

109
Fragment Caching allows a fragment of view logic to be wrapped in a cache block and served out of the cache store when the next request comes in.
110

111
As an example, if you wanted to show all the orders placed on your website in real time and didn't want to cache that part of the page, but did want to cache the part of the page which lists all products available, you could use this piece of code:
112 113 114

<ruby>
<% Order.find_recent.each do |o| %>
115
  <%= o.buyer.name %> bought <%= o.product.name %>
116 117 118 119
<% end %>

<% cache do %>
  All available products:
P
Pratik Naik 已提交
120
  <% Product.all.each do |p| %>
121 122 123 124 125
    <%= link_to p.name, product_url(p) %>
  <% end %>
<% end %>
</ruby>

126
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:
127 128

<ruby>
P
Pratik Naik 已提交
129
<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
130 131 132
  All available products:
</ruby>

P
Pratik Naik 已提交
133
and you can expire it using the +expire_fragment+ method, like so:
134 135

<ruby>
P
Pratik Naik 已提交
136
expire_fragment(:controller => 'products', :action => 'recent', :action_suffix => 'all_products')
137 138
</ruby>

139
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, like so:
140 141

<ruby>
P
Pratik Naik 已提交
142
<% cache('all_available_products') do %>
143 144 145 146
  All available products:
<% end %>
</ruby>

147
This fragment is then available to all actions in the +ProductsController+ using the key and can be expired the same way:
148 149

<ruby>
P
Pratik Naik 已提交
150
expire_fragment('all_available_products')
151 152 153 154
</ruby>

h4. Sweepers

155
Cache sweeping is a mechanism which allows you to get around having a ton of +expire_{page,action,fragment}+ calls in your code. It does this by moving all the work required to expire cached content into an +ActionController::Caching::Sweeper+ subclass. This class is an observer and looks for changes to an object via callbacks, and when a change occurs it expires the caches associated with that object in an around or after filter.
156

157
Continuing with our Product controller example, we could rewrite it with a sweeper like this:
158 159

<ruby>
P
Pratik Naik 已提交
160 161
class ProductSweeper < ActionController::Caching::Sweeper
  observe Product # This sweeper is going to keep an eye on the Product model
162 163 164

  # If our sweeper detects that a Product was created call this
  def after_create(product)
165
    expire_cache_for(product)
166 167 168 169
  end

  # If our sweeper detects that a Product was updated call this
  def after_update(product)
170
    expire_cache_for(product)
171 172 173 174
  end

  # If our sweeper detects that a Product was deleted call this
  def after_destroy(product)
175
    expire_cache_for(product)
176 177 178
  end

  private
P
Pratik Naik 已提交
179 180 181
  def expire_cache_for(product)
    # Expire the index page now that we added a new product
    expire_page(:controller => 'products', :action => 'index')
182 183

    # Expire a fragment
P
Pratik Naik 已提交
184
    expire_fragment('all_available_products')
185 186 187 188
  end
end
</ruby>

189
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:
P
Pratik Naik 已提交
190 191 192 193 194

<ruby>
  expire_action(:controller => 'products', :action => 'edit', :id => product)
</ruby>

195
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:
196 197 198 199

<ruby>
class ProductsController < ActionController

P
Pratik Naik 已提交
200 201 202
  before_filter :authenticate
  caches_action :index
  cache_sweeper :product_sweeper
203

P
Pratik Naik 已提交
204 205
  def index
    @products = Product.all
206 207 208 209 210 211 212
  end

end
</ruby>

h4. SQL Caching

213
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.
214 215 216 217 218 219

For example:

<ruby>
class ProductsController < ActionController

P
Pratik Naik 已提交
220
  def index
221
    # Run a find query
P
Pratik Naik 已提交
222
    @products = Product.all
223 224 225 226

    ...

    # Run the same query again
P
Pratik Naik 已提交
227
    @products = Product.all
228 229 230 231 232
  end

end
</ruby>

233
The second time the same query is run against the database, it's not actually going to hit the database. The first time the result is returned from the query it is stored in the query cache (in memory) and the second time it's pulled from memory.
234

235
However, it's important to note that query caches are created at the start of an action and destroyed at the end of that action and thus persist only for the duration of the action. If you'd like to store query results in a more persistent fashion, you can in Rails by using low level caching.
236

237
h3. Cache Stores
238

239
Rails provides different stores for the cached data created by action and fragment caches. Page caches are always stored on disk.
P
Pratik Naik 已提交
240

241
h4. Configuration
P
Pratik Naik 已提交
242

243
You can set up your application's default cache store by calling +config.cache_store=+ in the Application definition inside your +config/application.rb+ file or in an Application.configure block in an environment specific configuration file (i.e. +config/environments/*.rb+). The first argument will be the cache store to use and the rest of the argument will be passed as arguments to the cache store constructor.
244

245
<ruby>
246
config.cache_store = :memory_store
247 248
</ruby>

249
Alternatively, you can call +ActionController::Base.cache_store+ outside of a configuration block.
P
Pratik Naik 已提交
250

251
You can access the cache by calling +Rails.cache+.
252

253
h4. ActiveSupport::Cache::Store
P
Pratik Naik 已提交
254

255 256 257
This class provides the foundation for interacting with the cache in Rails. This is an abstract class and you cannot use it on its own. Rather you must use a concrete implementation of the class tied to a storage engine. Rails ships with several implementations documented below.

The main methods to call are +read+, +write+, +delete+, +exist?+, and +fetch+. The fetch method takes a block and will either return an existing value from the cache, or evaluate the block and write the result to the cache if no value exists.
258

259
There are some common options used by all cache implementations. These can be passed to the constructor or the various methods to interact with entries.
P
Pratik Naik 已提交
260

261
* +:namespace+ - This option can be used to create a namespace within the cache store. It is especially useful if your application shares a cache with other applications. The default value will include the application name and Rails environment.
A
Aditya Chadha 已提交
262

263
* +:compress+ - This option can be used to indicate that compression should be used in the cache. This can be useful for transferring large cache entries over a slow network.
P
Pratik Naik 已提交
264

265
* +:compress_threshold+ - This options is used in conjunction with the +:compress+ option to indicate a threshold under which cache entries should not be compressed. This defaults to 16 kilobytes.
P
Pratik Naik 已提交
266

267
* +:expires_in+ - This option sets an expiration time in seconds for the cache entry when it will be automatically removed from the cache.
P
Pratik Naik 已提交
268

269
* +:race_condition_ttl+ - This option is used in conjunction with the +:expires_in+ option. It will prevent race conditions when cache entries expire by preventing multiple processes from simultaneously regenerating the same entry (also known as the dog pile effect). This option sets the number of seconds that an expired entry can be reused while a new value is being regenerated. It's a good practice to set this value if you use the +:expires_in+ option.
P
Pratik Naik 已提交
270

271
h4. ActiveSupport::Cache::MemoryStore
P
Pratik Naik 已提交
272

273
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.
274 275

<ruby>
276
ActionController::Base.cache_store = :memory_store, :size => 64.megabytes
277 278
</ruby>

279 280 281 282 283 284 285
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.

h4. 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.
P
Pratik Naik 已提交
286 287

<ruby>
288
ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
P
Pratik Naik 已提交
289 290
</ruby>

291 292 293 294 295 296 297 298 299 300 301
With this cache store, multiple server processes on the same host can share a cache. Servers processes running on different hosts could share a cache by using a shared file system, but that set up would not be ideal and is not recommended. The cache store is appropriate for low to medium traffic sites that are served off one or two hosts.

Note that the cache will grow until the disk is full unless you periodically clear out old entries.

h4. ActiveSupport::Cache::MemCacheStore

This cache store uses Danga's +memcached+ server to provide a centralized cache for your application. Rails uses the bundled +memcached-client+ 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.

When initializing the cache, you need to specify the addresses for all memcached servers in your cluster. If none is specified, it will assume memcached is running on the local host on the default port, but this is not an ideal set up for larger sites.

The +write+ and +fetch+ methods on this cache accept two additional options that take advantage of features specific to memcached. You can specify +:raw+ to send a value directly to the server with no serialization. The value must be a string or number. You can use memcached direct operation like +increment+ and +decrement+ only on raw values. You can also specify +:unless_exist+ if you don't want memcached to overwrite an existing entry.
P
Pratik Naik 已提交
302 303

<ruby>
304
ActionController::Base.cache_store = :mem_cache_store, "cache-1.example.com", "cache-2.example.com"
P
Pratik Naik 已提交
305 306
</ruby>

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
h4. ActiveSupport::Cache::EhcacheStore

If you are using JRuby you can use Terracotta's Ehcache as the cache store for your application. Ehcache is an open source Java cache that also offers an enterprise version with increased scalability, management, and commercial support. You must first install the jruby-ehcache-rails3 gem (version 1.1.0 or later) to use this cache store.

<ruby>
ActionController::Base.cache_store = :ehcache_store
</ruby>

When initializing the cache, you may use the +:ehcache_config+ option to specify the Ehcache config file to use (where the default is "ehcache.xml" in your Rails config directory), and the :cache_name option to provide a custom name for your cache (the default is rails_cache).

In addition to the standard +:expires_in+ option, the +write+ method on this cache can also accept the additional  +:unless_exist+ option, which will cause the cache store to use Ehcache's +putIfAbsent+ method instead of +put+, and therefore will not overwrite an existing entry. Additionally, the +write+ method supports all of the properties exposed by the "Ehcache Element class":http://ehcache.org/apidocs/net/sf/ehcache/Element.html , including:

|_. Property |_. Argument Type |_. Description |
| elementEvictionData | ElementEvictionData | Sets this element's eviction data instance. |
| eternal | boolean | Sets whether the element is eternal. |
| timeToIdle, tti | int | Sets time to idle |
| timeToLive, ttl, expires_in | int | Sets time to Live |
| version | long | Sets the version attribute of the ElementAttributes object. |

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
</ruby>

For more information about Ehcache, see "http://ehcache.org/":http://ehcache.org/ .
For more information about Ehcache for JRuby and Rails, see "http://ehcache.org/documentation/jruby.html":http://ehcache.org/documentation/jruby.html

336 337 338 339 340
h4. Custom Cache Stores

You can create your own custom cache store by simply extending +ActiveSupport::Cache::Store+ and implementing the appropriate methods. In this way, you can swap in any number of caching technologies into your Rails application.

To use a custom cache store, simple set the cache store to a new instance of the class.
P
Pratik Naik 已提交
341

342
<ruby>
343
ActionController::Base.cache_store = MyCacheStore.new
344 345
</ruby>

346
h4. Cache Keys
347

348
The keys used in a cache can be any object that responds to either +:cache_key+ or to +:to_param+. You can implement the +:cache_key+ method on your classes if you need to generate custom keys. Active Record will generate keys based on the class name and record id.
P
Pratik Naik 已提交
349

350
You can use Hashes and Arrays of values as cache keys.
P
Pratik Naik 已提交
351 352

<ruby>
353
# This is a legal cache key
354
Rails.cache.read(:site => "mysite", :owners => [owner_1, owner_2])
P
Pratik Naik 已提交
355 356
</ruby>

357 358
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.

P
Pratik Naik 已提交
359
h3. Conditional GET support
360

361
Conditional GETs are a feature of the HTTP specification that provide a way for web servers to tell browsers that the response to a GET request hasn't changed since the last request and can be safely pulled from the browser cache.
362

363
They work by using the +HTTP_IF_NONE_MATCH+ and +HTTP_IF_MODIFIED_SINCE+ headers to pass back and forth both a unique content identifier and the timestamp of when the content was last changed. If the browser makes a request where the content identifier (etag) or last modified since timestamp matches the server’s version then the server only needs to send back an empty response with a not modified status.
364

365
It is the server's (i.e. our) responsibility to look for a last modified timestamp and the if-none-match header and determine whether or not to send back the full response. With conditional-get support in Rails this is a pretty easy task:
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

<ruby>
class ProductsController < ApplicationController

  def show
    @product = Product.find(params[:id])

    # 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)
      respond_to do |wants|
        # ... normal response processing
      end
    end

    # If the request is fresh (i.e. it's not modified) then you don't need to do
    # anything. The default render checks for this using the parameters
    # used in the previous call to stale? and will automatically send a
384
    # :not_modified. So that's it, you're done.
V
Vijay Dev 已提交
385
  end
386 387 388
end
</ruby>

389
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:
390 391 392 393 394 395 396 397 398

<ruby>
class ProductsController < ApplicationController

  # This will automatically send back a :not_modified if the request is fresh,
  # and will render the default template (product.*) if it's stale.

  def show
    @product = Product.find(params[:id])
A
Aditya Chadha 已提交
399
    fresh_when :last_modified => @product.published_at.utc, :etag => @product
400 401 402 403
  end
end
</ruby>

J
Joost Baaij 已提交
404
h3. Further reading
P
Pratik Naik 已提交
405

P
Pratik Naik 已提交
406
* "Scaling Rails Screencasts":http://railslab.newrelic.com/scaling-rails
P
Pratik Naik 已提交
407

408

P
Pratik Naik 已提交
409
h3. Changelog
P
Pratik Naik 已提交
410

411
* Feb       17, 2011: Document 3.0.0 changes to ActiveSupport::Cache
A
Aditya Chadha 已提交
412 413 414 415 416 417 418
* May       02, 2009: Formatting cleanups
* April     26, 2009: Clean up typos in submitted patch
* April      1, 2009: Made a bunch of small fixes
* February  22, 2009: Beefed up the section on cache_stores
* December  27, 2008: Typo fixes
* November  23, 2008: Incremental updates with various suggested changes and formatting cleanup
* September 15, 2008: Initial version by Aditya Chadha
J
JudeArasu 已提交
419