O'Reilly logo

Ajax on Rails by Scott Raymond

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Output Caching

Output caching refers to storing the output of Rails’ views so that the next request requires less overhead to recreate. Rails provides output caching at three levels of granularity, each useful for different purposes:

Page caching

Writes the complete response body to a static file in the public directory, so that subsequent requests are served directly by the web server

Action caching

Caches the complete response body, but still processes each request through Rails

Fragment caching

Stores subpage level snippets of output

Not everything is a candidate for caching—highly dynamic applications that deal with ever-changing data may not benefit much or at all. But many pages will get a dramatic speedup from caching, especially high-traffic pages that summarize a large amount of data and don’t need up-to-the-second freshness.

By default, output caching isn’t performed in the development environment. To enable it for debugging, edit config/environments/development.rb and enable caching with this setting:

config.action_controller.perform_caching = true

Page Caching

The first type of output caching, and the bluntest, is page caching. Page caches store the output of an entire action at once, and subsequent requests to the page bypass filters—in fact, they bypass Rails entirely.

Page caching relies on how the web server in front of Rails (such as Apache or lighttpd) is configured. Typically, when the web server receives a request for a URL in a Rails application, it will first check ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required