27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

448 CHAPTER 16 Basic performance enhancements<br />

16.3 Page and action caching<br />

Rails has several methods of caching pages. The first of these methods serves a request<br />

and then stores the output of that page in the public folder of your application so that<br />

it can be served without going through the Rails stack by the web server. This is known<br />

as page caching<br />

You’d cache a page if that page took a long time to process, or if there were a lot of<br />

requests to it. If either of these situations happens, the performance of the web server<br />

can be degraded and requests can end up piling up.<br />

By caching a page, you take the responsibility of processing and serving it off your<br />

Rails stack and put it on the (usually) more-than-capable web server. 8<br />

The first time a page is requested, you store it as a file in your application. The<br />

next time the request is made, that static page will be served rather than having the<br />

action processed again.<br />

This first type of caching is great for pages that don’t require authentication. For<br />

pages that do require authentication you’ll need to use a different kind of caching<br />

called action caching. This type of caching runs the before filters on a request before it<br />

serves the cached page, and you’ll see a great example of this in this section.<br />

Let’s take a look at the first kind of caching, plain ol’ page caching.<br />

16.3.1 Caching a page<br />

You’re going to cache the page that’s rendered when a user looks at Projects-<br />

Controller’s show action. By caching this particular page, Rails will serve the first<br />

request to this file and then save the output of the request to a new file at public/<br />

projects/:id.html. This public/projects directory will be created by Rails automatically.<br />

This process is shown in figure 16.4.<br />

On the next request, due to how the web server is configured, it will serve the file<br />

rather than hit the Rails stack, as shown in figure 16.5. This is absolutely a faster<br />

request, regardless of how little goes on in an action in Rails. If a request doesn’t have<br />

to go down that extra level in the stack it’s going to save a great deal of time, and<br />

again: modern web servers are built to serve these static files.<br />

One of the downsides of this is that it will not cache the GET parameter on the<br />

request, like your page numbers. Earlier, when you used rails server to use your<br />

pagination, the URL became http://localhost:3000/projects/1?page=2. The page that’s<br />

cached doesn’t have this parameter at the end, and so it will always display the first<br />

page, because that’s what will be stored at public/projects/:id.html.<br />

Regardless of this, you’ll at least see how this method works. In your Projects-<br />

Controller, underneath the before_filter lines, you can put this method to tell<br />

Rails to cache the page for the show action:<br />

caches_page :show<br />

8 Such as Apache or nginx, or any other HTTP server. Not WEBrick. There are some things that Ruby’s made<br />

for, and being a fast/stable HTTP server ain’t one.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!