caching_with_rails.textile 19.1 KB
Newer Older
1 2 3 4 5 6
h2. Caching with Rails: An overview

Everyone caches. 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 those hungry web clients in the shortest time possible.

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

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

14 15 16 17 18 19 20
endprologue.

h3. Basic Caching

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

P
Pratik Naik 已提交
21 22 23 24 25
To start playing with testing 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.
26 27 28 29 30 31 32 33

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

h4. Page Caching

Page caching is a Rails mechanism which allows the request for a generated
P
Pratik Naik 已提交
34
page to be fulfilled by the webserver (i.e. apache or nginx), without ever having to go through the
35 36 37 38 39
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.

P
Pratik Naik 已提交
40
So, how do you enable this super-fast cache behavior?  Simple, let's say you
P
Pratik Naik 已提交
41
have a controller called +ProductsController+ and an +index+ action that lists all
P
Pratik Naik 已提交
42
the products
43 44 45 46

<ruby>
class ProductsController < ActionController

47
  caches_page :index
48

49
  def index
P
Pratik Naik 已提交
50 51
    @products = Products.all
  end
52 53 54 55

end
</ruby>

P
Pratik Naik 已提交
56 57 58
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.
59

P
Pratik Naik 已提交
60 61
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
62
changing the configuration setting +config.action_controller.page_cache_directory+.
P
Pratik Naik 已提交
63 64
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
65 66 67
server reconfiguration to let the web server know where to serve the cached
files from.

P
Pratik Naik 已提交
68
The Page Caching mechanism will automatically add a +.html+ extension to
69
requests for pages that do not have an extension to make it easy for the
P
Pratik Naik 已提交
70
webserver to find those pages and this can be configured by changing the
71 72
configuration setting +config.action_controller.page_cache_extension+.

P
Pratik Naik 已提交
73 74
In order to expire this page when a new product is added we could extend our
example controller like this:
75 76 77 78

<ruby>
class ProductsController < ActionController

79
  caches_page :index
80

81
  def index
P
Pratik Naik 已提交
82 83
    @products = Products.all
  end
84 85

  def create
A
Aditya Chadha 已提交
86
    expire_page :action => :list
87 88 89 90 91 92 93 94
  end

end
</ruby>

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.

P
Pratik Naik 已提交
95
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!
96 97 98

h4. Action Caching

P
Pratik Naik 已提交
99 100 101 102 103 104
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
105 106
result of the output from a cached copy.

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

P
Pratik Naik 已提交
109
Let's say you only wanted authenticated users to call actions on +ProductsController+.
110 111 112 113

<ruby>
class ProductsController < ActionController

P
Pratik Naik 已提交
114 115
  before_filter :authenticate
  caches_action :index
116

P
Pratik Naik 已提交
117 118 119
  def index
    @products = Product.all
  end
120 121

  def create
P
Pratik Naik 已提交
122
    expire_action :action => :index
123 124 125 126 127
  end

end
</ruby>

P
Pratik Naik 已提交
128
You can also use +:if+ (or +:unless+) to pass a Proc that specifies when the
129
action should be cached. Also, you can use +:layout => false+ to cache without
P
Pratik Naik 已提交
130
layout so that dynamic information in the layout such as logged in user info
131 132 133
or the number of items in the cart can be left uncached. This feature is
available as of Rails 2.2.

P
Pratik Naik 已提交
134
You can modify the default action cache path by passing a +:cache_path+ option.
P
Pratik Naik 已提交
135
This will be passed directly to +ActionCachePath.path_for+.  This is handy for
P
Pratik Naik 已提交
136 137
actions with multiple possible routes that should be cached differently.  If
a block is given, it is called with the current controller instance.  
138

P
Pratik Naik 已提交
139
Finally, if you are using memcached, you can also pass +:expires_in+. In fact,
P
Pratik Naik 已提交
140
all parameters not used by +caches_action+ are sent to the underlying cache
P
Pratik Naik 已提交
141
store. 
142 143 144 145 146 147 148 149 150 151 152 153 154 155

h4. Fragment Caching

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.

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.

As an example, if you wanted to show all the orders placed on your website
P
Pratik Naik 已提交
156
in real time and didn't want to cache that part of the page, but did want
157 158 159 160 161 162 163 164 165 166
to cache the part of the page which lists all products available, you
could use this piece of code:

<ruby>
<% Order.find_recent.each do |o| %>
  <%= o.buyer.name %> bought <% o.product.name %>
<% end %>

<% cache do %>
  All available products:
P
Pratik Naik 已提交
167
  <% Product.all.each do |p| %>
168 169 170 171 172 173
    <%= link_to p.name, product_url(p) %>
  <% end %>
<% end %>
</ruby>

The cache block in our example will bind to the action that called it and is
P
Pratik Naik 已提交
174
written out to the same place as the Action Cache, which means that if you
175 176 177
want to cache multiple fragments per action, you should provide an +action_suffix+ to the cache call:

<ruby>
P
Pratik Naik 已提交
178
<% cache(:action => 'recent', :action_suffix => 'all_products') do %>
179 180 181
  All available products:
</ruby>

P
Pratik Naik 已提交
182
and you can expire it using the +expire_fragment+ method, like so:
183 184

<ruby>
P
Pratik Naik 已提交
185
expire_fragment(:controller => 'products', :action => 'recent', :action_suffix => 'all_products')
186 187
</ruby>

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

<ruby>
P
Pratik Naik 已提交
193
<% cache('all_available_products') do %>
194 195 196 197
  All available products:
<% end %>
</ruby>

P
Pratik Naik 已提交
198
This fragment is then available to all actions in the +ProductsController+ using
199 200 201
the key and can be expired the same way:

<ruby>
P
Pratik Naik 已提交
202
expire_fragment('all_available_products')
203 204 205 206 207
</ruby>

h4. Sweepers

Cache sweeping is a mechanism which allows you to get around having a ton of
P
Pratik Naik 已提交
208 209 210
+expire_{page,action,fragment}+ calls in your code.  It does this by moving all the work
required to expire cached content into a +ActionController::Caching::Sweeper+
class.  This class is an Observer and looks for changes to an object via callbacks,
P
Pratik Naik 已提交
211
and when a change occurs it expires the caches associated with that object in
212 213 214
an around or after filter.

Continuing with our Product controller example, we could rewrite it with a
P
Pratik Naik 已提交
215
sweeper like this:
216 217

<ruby>
P
Pratik Naik 已提交
218 219
class ProductSweeper < ActionController::Caching::Sweeper
  observe Product # This sweeper is going to keep an eye on the Product model
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

  # If our sweeper detects that a Product was created call this
  def after_create(product)
          expire_cache_for(product)
  end

  # If our sweeper detects that a Product was updated call this
  def after_update(product)
          expire_cache_for(product)
  end

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

  private
P
Pratik Naik 已提交
237 238 239
  def expire_cache_for(product)
    # Expire the index page now that we added a new product
    expire_page(:controller => 'products', :action => 'index')
240 241

    # Expire a fragment
P
Pratik Naik 已提交
242
    expire_fragment('all_available_products')
243 244 245 246
  end
end
</ruby>

P
Pratik Naik 已提交
247 248 249 250 251 252 253 254 255 256
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 a expire method
which specifies the page we want to expire:

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

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
257 258 259 260 261 262
list and edit actions when the create action was called, we could do the
following:

<ruby>
class ProductsController < ActionController

P
Pratik Naik 已提交
263 264 265
  before_filter :authenticate
  caches_action :index
  cache_sweeper :product_sweeper
266

P
Pratik Naik 已提交
267 268
  def index
    @products = Product.all
269 270 271 272 273 274 275 276
  end

end
</ruby>

h4. SQL Caching

Query caching is a Rails feature that caches the result set returned by each
P
Pratik Naik 已提交
277
query so that if Rails encounters the same query again for that request, it
A
Aditya Chadha 已提交
278
will use the cached result set as opposed to running the query against the
P
Pratik Naik 已提交
279
database again.
280 281 282 283 284 285

For example:

<ruby>
class ProductsController < ActionController

P
Pratik Naik 已提交
286
  def index
287
    # Run a find query
P
Pratik Naik 已提交
288
    @products = Product.all
289 290 291 292

    ...

    # Run the same query again
P
Pratik Naik 已提交
293
    @products = Product.all
294 295 296 297 298
  end

end
</ruby>

P
Pratik Naik 已提交
299 300 301
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.
302

P
Pratik Naik 已提交
303 304 305
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.
306

A
Aditya Chadha 已提交
307
h3. Cache stores
308

P
Pratik Naik 已提交
309
Rails (as of 2.1) provides different stores for the cached data created by action and
P
Pratik Naik 已提交
310 311
fragment caches. Page caches are always stored on disk.

P
Pratik Naik 已提交
312
Rails 2.1 and above provide +ActiveSupport::Cache::Store+ which can be used to
P
Pratik Naik 已提交
313 314 315 316
cache strings. Some cache store implementations, like MemoryStore, are able to
cache arbitrary Ruby objects, but don't count on every cache store to be able
to do that.

P
Pratik Naik 已提交
317
The default cache stores provided with Rails include:
P
Pratik Naik 已提交
318

A
Aditya Chadha 已提交
319
1) ActiveSupport::Cache::MemoryStore: A cache store implementation which stores
P
Pratik Naik 已提交
320 321 322 323 324 325 326
everything into memory in the same process. If you're running multiple Ruby on
Rails server processes (which is the case if you're using mongrel_cluster or
Phusion Passenger), then this means that your Rails server process instances
won't be able to share cache data with each other. If your application never
performs manual cache item expiry (e.g. when you‘re using generational cache
keys), then using +MemoryStore+ is ok. Otherwise, consider carefully whether you
should be using this cache store.  
327

A
Aditya Chadha 已提交
328
+MemoryStore+  is not only able to store strings, but also arbitrary Ruby objects.
329

A
Aditya Chadha 已提交
330
+MemoryStore+  is not thread-safe. Use +SynchronizedMemoryStore+ instead if you need thread-safety.
P
Pratik Naik 已提交
331
                                      
332 333 334 335
<ruby>
ActionController::Base.cache_store = :memory_store
</ruby>

A
Aditya Chadha 已提交
336
2) ActiveSupport::Cache::FileStore: Cached data is stored on the disk, this is
P
Pratik Naik 已提交
337 338 339 340
the default store and the default path for this store is: /tmp/cache. Works
well for all types of environments and allows all processes running from the
same application directory to access the cached content. If /tmp/cache does not
exist, the default store becomes MemoryStore.
341

P
Pratik Naik 已提交
342

343 344 345 346
<ruby>
ActionController::Base.cache_store = :file_store, "/path/to/cache/directory"
</ruby>

A
Aditya Chadha 已提交
347
3) ActiveSupport::Cache::DRbStore: Cached data is stored in a separate shared
P
Pratik Naik 已提交
348 349 350 351
DRb process that all servers communicate with. This works for all environments
and only keeps one cache around for all processes, but requires that you run
and manage a separate DRb process.

P
Pratik Naik 已提交
352

353 354 355 356
<ruby>
ActionController::Base.cache_store = :drb_store, "druby://localhost:9192"
</ruby>

A
Aditya Chadha 已提交
357
4) MemCached store: Works like DRbStore, but uses Danga's MemCache instead.
P
Pratik Naik 已提交
358 359 360 361
Rails uses the bundled memcached-client gem by default. This is currently the
most popular cache store for production websites.

Special features:
A
Aditya Chadha 已提交
362

A
Aditya Chadha 已提交
363
* Clustering and load balancing. One can specify multiple memcached servers, and MemCacheStore will load balance between all available servers. If a server goes down, then MemCacheStore will ignore it until it goes back online.
A
Aditya Chadha 已提交
364 365
* Time-based expiry support. See +write+ and the +:expires_in+ option.
* Per-request in memory cache for all communication with the MemCache server(s).
P
Pratik Naik 已提交
366 367 368

It also accepts a hash of additional options:

A
Aditya Chadha 已提交
369 370 371
* +:namespace+- specifies a string that will automatically be prepended to keys when accessing the memcached store.
* +:readonly+- a boolean value that when set to true will make the store read-only, with an error raised on any attempt to write.
* +:multithread+ - a boolean value that adds thread safety to read/write operations - it is unlikely you'll need to use this option as the Rails threadsafe! method offers the same functionality.
P
Pratik Naik 已提交
372 373

The read and write methods of the MemCacheStore accept an options hash too.
P
Pratik Naik 已提交
374
When reading you can specify +:raw => true+ to prevent the object being marshaled
P
Pratik Naik 已提交
375
(by default this is false which means the raw value in the cache is passed to
P
Pratik Naik 已提交
376
+Marshal.load+ before being returned to you.)
P
Pratik Naik 已提交
377

P
Pratik Naik 已提交
378 379
When writing to the cache it is also possible to specify +:raw => true+ means
the value is not passed to +Marshal.dump+ before being stored in the cache (by
P
Pratik Naik 已提交
380 381 382 383 384 385 386
default this is false). 

The write method also accepts an +:unless_exist+ flag which determines whether
the memcached add (when true) or set (when false) method is used to store the
item in the cache and an +:expires_in+ option that specifies the time-to-live
for the cached item in seconds.

387 388 389 390 391

<ruby>
ActionController::Base.cache_store = :mem_cache_store, "localhost"
</ruby>

A
Aditya Chadha 已提交
392
5) ActiveSupport::Cache::SynchronizedMemoryStore: Like ActiveSupport::Cache::MemoryStore but thread-safe.
P
Pratik Naik 已提交
393 394 395 396 397 398


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

A
Aditya Chadha 已提交
399
6) ActiveSupport::Cache::CompressedMemCacheStore: Works just like the regular
P
Pratik Naik 已提交
400 401 402 403 404 405 406
MemCacheStore but uses GZip to decompress/compress on read/write.


<ruby>
ActionController::Base.cache_store = :compressed_mem_cache_store, "localhost"
</ruby>

A
Aditya Chadha 已提交
407
7) Custom store: You can define your own cache store (new in Rails 2.1)
P
Pratik Naik 已提交
408

409 410 411 412 413

<ruby>
ActionController::Base.cache_store = MyOwnStore.new("parameter")
</ruby>

P
Pratik Naik 已提交
414 415 416
+Note: +config.cache_store+ can be used in place of
+ActionController::Base.cache_store+ in your +Rails::Initializer.run+ block in
+environment.rb+
417

P
Pratik Naik 已提交
418
In addition to all of this, Rails also adds the +ActiveRecord::Base#cache_key+
P
Pratik Naik 已提交
419
method that generates a key using the class name, +id+ and +updated_at+ timestamp (if available).
P
Pratik Naik 已提交
420

P
Pratik Naik 已提交
421
You can access these cache stores at a low level for storing queries and other objects. Here's an example:
P
Pratik Naik 已提交
422 423 424 425 426 427 428

<ruby>
Rails.cache.read("city")   # => nil
Rails.cache.write("city", "Duckburgh")
Rails.cache.read("city")   # => "Duckburgh"
</ruby>

P
Pratik Naik 已提交
429
h3. Conditional GET support
430

P
Pratik Naik 已提交
431
Conditional GETs are a feature of the HTTP specification that provide a way for web
P
Pratik Naik 已提交
432
servers to tell browsers that the response to a GET request hasn't changed
433 434
since the last request and can be safely pulled from the browser cache.

P
Pratik Naik 已提交
435 436 437 438 439 440
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.
441

P
Pratik Naik 已提交
442
It is the server's (i.e. our) responsibility to look for a last modified
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
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:

<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
    # :not_modified.  So that's it, you're done.
end
</ruby>

P
Pratik Naik 已提交
468 469
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
470 471 472 473 474 475 476 477 478 479
yourself) then you’ve got an easy helper in fresh_when:

<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 已提交
480
    fresh_when :last_modified => @product.published_at.utc, :etag => @product
481 482 483 484 485 486 487 488
  end
end
</ruby>

h3. Advanced Caching

Along with the built-in mechanisms outlined above, a number of excellent
plugins exist to help with finer grained control over caching. These include
P
Pratik Naik 已提交
489 490
Chris Wanstrath's excellent cache_fu plugin (more info "here": http://errtheblog.com/posts/57-kickin-ass-w-cachefu) and Evan Weaver's
interlock plugin (more info "here": http://blog.evanweaver.com/articles/2007/12/13/better-rails-caching/). Both
491 492
of these plugins play nice with memcached and are a must-see for anyone
seriously considering optimizing their caching needs.
P
Pratik Naik 已提交
493 494 495 496 497

Also the new "Cache money":http://github.com/nkallen/cache-money/tree/master plugin is supposed to be mad cool. 

h3. References

P
Pratik Naik 已提交
498
* "Scaling Rails Screencasts":http://railslab.newrelic.com/scaling-rails
499
* "RailsEnvy, Rails Caching Tutorial, Part 1":http://www.railsenvy.com/2007/2/28/rails-caching-tutorial
A
Aditya Chadha 已提交
500
* "RailsEnvy, Rails Caching Tutorial, Part 2":http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2
501 502
* "ActiveSupport::Cache documentation":http://api.rubyonrails.org/classes/ActiveSupport/Cache.html
* "Rails 2.1 integrated caching tutorial":http://thewebfellas.com/blog/2008/6/9/rails-2-1-now-with-better-integrated-caching
P
Pratik Naik 已提交
503

504

P
Pratik Naik 已提交
505
h3. Changelog
P
Pratik Naik 已提交
506 507
"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/10-guide-to-caching

A
Aditya Chadha 已提交
508 509 510 511 512 513 514
* 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