Caching
Caching allows the web server to use previous results for more efficiently handling of requests. With caching, the server may be able to respond with a previously created page and avoid the cost of database access and template rendering.
Unless you specify otherwise, Rails only caches results when working in the production environment.
Page Caching
Page caching can bring the greatest performance in efficiency. With the first request, the resulting page is stored as an HTML file (by default in the public folder). A subsequent request simply accesses the HTML file and bypasses all controller processing and database accesses.
You can indicate that a page from a controller action should be cached by adding this statement in your controller:
caches_page :index
This would cache all pages from the index action in the controller.
If you need to refresh the cached page to accomodate updates in the data model, issuing this command would cause a new page to be dynamically created by running the action:
expire_page :action => 'index'
Action Caching
Sometimes you can't cache an entire page because you need to check for authentication using a filter. In this case, you can still cache the results of the controller (which will still run any filter methods):
caches_action :index
Partial Caching
It's also possible to designate segments of a template for caching, although we will not review the details for this class.