Practical eCommerce

 

Expiring Cached Content in Rails - Part 2

Author: Brian Getting
Publish Date: October 22, 2007
Blog: Developers' Corner
Tags: ruby, rails, caching, Rails Envy

avatar

Well, back to the caching stuff for Rails. The first method I wanted to tackle in this post is the use of cache sweepers to clear cache files for a particular model when it is updated. To use our article example from before, if I made a change to the article model, such as creating a new article, editing an existing article, or deleting an article, I would then need to expire any cached files that display this model data. The reason being that since the model was updated, the cache file no longer reflects the current data in the model.

To clear cache files based on when a model is updated, sweepers are the way to go. Rails cache sweepers simply observe a model, and anytime that it is updated the sweeper jumps into action by expiring any cache files that need to be expired. They are a nice way of ensuring that your caches are up to date simply and easily.

As I mentioned in the last post, I found a much better way of using cache sweepers than the method that I had been using. Essentially, it is the same in that it uses before and after filters on a Rails model to run cache removal functions. However, the boys at Rails Envy have a more organized, and much more DRY way of doing things than me. So, I'm going to refer people to their (new) site, since they do a great job of explaining it. Essentially, they use the Rails cache_sweeper helpers, and also do a but of nice housecleaning by putting all of the cache sweepers into a directory called "sweepers" in the Rails application. This is a great way to keep code organized, and also to know where to look when you are trying to get at your cache sweepers.

What can I say, their way was better, and I'm glad I found that. So I am left with the last option for getting rid of cache files in Rails, which works with just about every server-side scripting technology. I am referring to using a CRON job to remove cache files periodically based on time.

Go into creating a CRON job to remove cache files every so often. By doing this, you can update cache files based on time rather than anything else. To be honest, there are very few times that you would want to use this over a sweeper, but let's say that you have a model that updates very frequently, and expiring the cache files on every update would nearly defeat the purpose of caching. In that case, you may want your cache files to update every 10 minutes, or one hour, for example.

CRON is a Unix application that is used to schedule tasks. If you are on a Windows web server there is probably an alternative, but I am not sure what it is or what it is called. For the most part, Rails apps live on Unix servers, so odds are CRON will be available. Depending on your hosting company you may have an interface for creating CRON jobs, or you may have to schedule CRON jobs using a Terminal application.

In the case of expiring cache files with a CRON job, you would simply create an action that eliminates caches files (see my previous post about manually expiring cache files) and then have CRON run that action at a given interval. Alternatively, and probably much more secure, is to create a shell script that will go through and eliminate the cache files from the files system, and then to run that script periodically using CRON. The reason that this is ideal is that if you create an action, the cache expiring is available online and that is probably not a good idea. Additionally, a shell script runs much more efficiently on a server because Rails does not need to get involved etc.

Hopefully some of this helps someone out there. I've been doing a lot of work with frameworks lately, and would like to start doing more posts about how developers can take advantage of frameworks to get a LOT more done. Perhaps we'll start with the Prototype JavaScript framework.

Add a Bookmark: Add 'Expiring Cached Content in Rails - Part 2' to Del.icio.us Digg 'Expiring Cached Content in Rails - Part 2' on Digg.com Submit 'Expiring Cached Content in Rails - Part 2' to reddit.com Blink 'Expiring Cached Content in Rails - Part 2' Add 'Expiring Cached Content in Rails - Part 2' to dzone Seed 'Expiring Cached Content in Rails - Part 2' on Newsvine Add 'Expiring Cached Content in Rails - Part 2' to Furl Add 'Expiring Cached Content in Rails - Part 2' to Spurl Add 'Expiring Cached Content in Rails - Part 2' on simpy.com Add 'Expiring Cached Content in Rails - Part 2' to fark.com BlogMark 'Expiring Cached Content in Rails - Part 2' Add 'Expiring Cached Content in Rails - Part 2' to Yahoo! myweb2 Add 'Expiring Cached Content in Rails - Part 2' to wists.com Stumble It!

0 Comments

Bloggers Wanted

We’re looking for merchants and other ecommerce professionals to share their experiences with our readers. If this interests you, we invite you to contact us.

Inside Practical eCommerce