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.

454 CHAPTER 16 Basic performance enhancements<br />

to this:<br />

project_path(params[:id]) + "/#{current_user.id}/#{params[:page] || 1}"<br />

The next time you request this page, it will again save a new version of it, this time outputting<br />

a line like this:<br />

Write fragment views/localhost:3000/projects/1/1/1<br />

The first 1 here represents the project’s id, the second represents the user’s, and the<br />

third represents the page number. This file is saved to a path such as tmp/cache/E62/<br />

3E0/views%2Flocalhost%3A3000%2Fprojects%2F1%2F1%2F1.<br />

So in this section you’ve fixed the problem where all people would see that they<br />

were signed in as the first person who requested the page, as well as the case where<br />

only one page of your tickets was available. Now what happens when you update this<br />

page and the tickets change? These pages will still be cached, and your new tickets or<br />

updates to them will not be shown!<br />

You’re going to need a way to clear this cache, to expire the fragments that are created<br />

when these events happen. Right now, the number-one situation where that’s<br />

going to happen is when you create a new ticket for a project. You can trigger this<br />

event to clear your cache by using a feature in Rails known as cache sweepers.<br />

16.3.3 Cache sweepers<br />

Cache sweepers are much like the observers you used back in chapter 12. In fact, the<br />

ActionController::Caching::Sweeper class inherits from ActiveRecord::Observer,<br />

effectively making them the same thing. The difference here is that you refer to the<br />

sweeper in the controller, telling it to run after certain actions have completed. 9<br />

In this case, whenever a ticket is created, updated, or destroyed in a project, you’ll<br />

want your application to clear out the cached pages because they would be out of date<br />

at that point. This is precisely what you can use a sweeper for. To call this sweeper, put<br />

this line underneath the before_filter calls in TicketsController:<br />

cache_sweeper :tickets_sweeper, :only => [:create, :update, :destroy]<br />

You put this line in your TicketsController because you want it to run after the<br />

create, update, and destroy actions.<br />

Now when you go to a project in your application and attempt to create a new<br />

ticket on it, you’ll get this error:<br />

uninitialized constant TicketsSweeper<br />

Rails is looking for the TicketsSweeeper constant, which is supposed to define the<br />

cache sweeping behavior for your TicketsController, but can’t find it because you<br />

haven’t defined it yet. To define this, create a new folder at app/sweepers for this<br />

9 It uses after_filter to do this, which can also be used to run other actions after a controller’s action has<br />

been processed, just like a before_filter can be used to run actions before a controller’s action runs.

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

Saved successfully!

Ooh no, something went wrong!