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.

462 CHAPTER 16 Basic performance enhancements<br />

added to the output, rather than re-processed. All in all, this solution is more elegant<br />

than caches_action. Another commit is in order!<br />

git add .<br />

git commit -m "Implement tidier caching for the tickets<br />

➥list on the projects page"<br />

That covers all the major methods for basic caching in controllers and views. You’ve<br />

seen ways to cache entire pages and parts of pages as cached files on the filesystem. In<br />

a Rails application there may be a lot of reading and writing to the filesystem, which<br />

can cause degradation of performance, so storing these files on the filesystem may not<br />

be the best idea. A speedier way of doing this would be to store these files in memory<br />

by switching the cache store that Rails uses. You can do this by putting this line in one<br />

of your config/environments files, probably production.rb:<br />

config.action_controller.cache_store = :memory_store<br />

Rather than storing the fragments on the file system, Rails will now store them in memory<br />

along with the code for the application. The retrieval time is faster here, but comes<br />

at the cost losing the cache if the server was ever stopped. If you want something more<br />

persistent, you may choose to use either Memcached (http://memcached.org) or<br />

Redis (http://redis.io). We won’t go into these in this chapter, as they exceed the<br />

boundaries of what would be considered basic performance enhancements.<br />

In this section you’ve learned how to use fragment caching to store parts of the<br />

view that may take a long time to process. This type of caching would store these fragments<br />

in the tmp/cache directory; they can be retrieved later on.<br />

16.4 Background workers<br />

There are other situations where requests can be slow for your application too. One of<br />

these cases would be if a ticket had a large number of watchers, and a comment was<br />

posted to that ticket. The reason for this slowdown would be because Rails would have<br />

to iterate through all the watchers and send out the update notification email to each<br />

of them individually, using the feature that you developed in chapter 12.<br />

Rather than having a user make the request to create a comment in the application,<br />

having the server process the email notifications, and then send a response back,<br />

you can take the long-running task of sending these emails and move it into a job that<br />

runs in a background.<br />

This will work by having your CommentObserver add the task of sending these<br />

emails to a job queue that runs in the background. You’ll then have a background process<br />

separate from your application that will run these jobs as it receives them. This<br />

way, the hard work is done behind the scenes and the user receives the request back<br />

almost as if nothing of consequence happened.<br />

To make this happen, you’ll use a gem called delayed_job. This gem will allow you<br />

to create a table in your database where the jobs that the background worker needs to<br />

work off will be stored. The gem will also provide you with the ability to start a worker<br />

process. To add this to your application you’ll put this line in your Gemfile:

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

Saved successfully!

Ooh no, something went wrong!