27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

452 CHAPTER 16 Basic performance enhancements<br />

with this line:<br />

caches_action :show<br />

For this change to take effect, you only need to refresh the page at http://localhost<br />

:3000/projects/1 or actually visit it again if you’ve closed the browser since the last<br />

time. If you switch over to the terminal where your server is running, you won’t see the<br />

line that says this:<br />

Write page /.../ticketee/public/projects/1.html (0.3ms)<br />

Rather, you’ll see this line instead:<br />

Write fragment views/localhost:3000/projects/1 (40.3ms)<br />

This time, Rails has written a fragment rather than writing a page. In this case, the fragment<br />

is actually the entire page, but it is the page available only for this user. When<br />

you request this page again, you’ll see this line in the server’s output:<br />

Read fragment views/localhost:3000/projects/1 (0.3ms)<br />

Upon the second request here, Rails has found the fragment pertaining to this<br />

request and served that instead. Rather than saving these files into the public directory,<br />

Rails instead saves them to the tmp/cache directory. Files that are in the public<br />

directory are automatically served by your web server without hitting the Rails stack,<br />

but cached responses in tmp/cache are served by the Rails stack itself. This may seem<br />

counterintuitive at first, but it’s really helpful if you want to alter what cache fragments<br />

are served to what user.<br />

Currently, the fragment is written to a file such as tmp/cache/CC6/080/<br />

views%2Flocalhost%3A3000%2Fprojects%2F1. This location is simply a location in the<br />

tmp/cache folder with a hashed path, followed by the escaped name of views/<br />

localhost:3000/projects/1. It’s with this name that Rails can retrieve this fragment and<br />

show it again.<br />

But you’re still going to have the problem that both of your users are going to see<br />

the same page. Sign out of your current user, and sign in as the other one. Once you<br />

visit this page again, you’ll see you’re still signed in as the first user! It’s doing the<br />

same darn thing as caches_page!<br />

As stated before, caches_action is different. It runs the before_filters of your<br />

controller and has one more special benefit: you can change the path of where this<br />

file is cached by using the cache_path option passed to caches_action. You can then<br />

set this option to be a Proc object, which means it will be evaluated before every<br />

request made to the action (or actions) you are caching. In this Proc object you’ll<br />

have access to the current controller instance, meaning you’ll have access to<br />

current_user. With this access, you’ll be able to customize the path where the cache<br />

is kept so that you can cache the same page for different users.<br />

To do this, change your caches_action line in your controller to these lines:

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

Saved successfully!

Ooh no, something went wrong!