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.

Page and action caching<br />

To make the show action in TicketsController conditionally send back the 304<br />

Not Modified, put this at the bottom of the show method in app/controllers/<br />

tickets_controller.rb:<br />

fresh_when :last_modified => @ticket.updated_at,<br />

:etag => @ticket.to_s + current_user.id.to_s<br />

The last_modified option here sends back another header to the client: the Last-<br />

Modified header. This header is used by a browser to detect when the page was last<br />

updated, which provides a near-identical purpose to an ETag. A browser sends an If-<br />

Modified-Since header that contains the last Last-Modified time. If the server sees<br />

that the Last-Modified time is later than the If-Modified-Since, it will send a new<br />

copy of the page. Otherwise, it will send a 304 Not Modified header.<br />

The :etag option tells fresh_when to generate a new ETag for the resource. Until<br />

this resource changes, the ETag generated will be the same for each user. This<br />

wouldn’t be the case if you didn’t pass through the current_user.id.to_s to the<br />

ETag, but only for two user accounts accessed on the same computer. By using the<br />

current_user’s id attribute to seed the etag option, the tag will be different between<br />

users. How this ETag is generated differs from implementation to implementation; in<br />

Rails it’s an MD5 hash, which is guaranteed uniqueness.<br />

Even though these two options are nearly identical, some browsers may support<br />

one or the other. It’s more of a way to cover your bases to pass through both headers,<br />

and it’s a worthwhile thing to cover.<br />

You can see this in action now if you attempt to visit a ticket’s page. Your first<br />

request will have a final line that says something like this:<br />

Completed 200 OK in 486ms (Views: 200.4ms | ActiveRecord: 5.6ms)<br />

In this instance, the views have been rendered and the entire procedure has taken<br />

486ms. Rather than refreshing the page (because in some browsers, this triggers them<br />

to not send the If-Modified-Since or If-None-Match headers), you’ll go back to the<br />

project’s page and then click back on the same ticket again. This time in the server<br />

output you’ll see this output:<br />

Completed 304 Not Modified in 267ms<br />

The server has sent back a 304 Not Modified response in a slightly quicker time than<br />

your original request, mainly because it didn’t have to re-render the views for the<br />

application and send back all that HTML.<br />

This is another way to ease the load on your server, by getting the browser to deal<br />

with the page caching and serving, rather than the server.<br />

That wraps up this section. You’ve made a small change here you should probably<br />

commit. You can do that by typing these commands into the terminal:<br />

git add .<br />

git commit -m "Add ETag and Last-Modified<br />

support to ticket show page"<br />

459

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

Saved successfully!

Ooh no, something went wrong!