Tip of the Day: Ruby on Rails caching

Posted by JP Wed, 09 May 2007 17:31:00 GMT

Creating a web application is one thing. Deploying it is another.

On a shared server, resources such as memory and processing time are monitored closely so that a single application doesn't gobble up all available resources. Failure to do so can result in your hosting account being suspended, depending on your hosting provider.

Although Rails is relatively efficient, it's still not comparable to having the web server throw back a cached static page. Doing so bypasses the Ruby interpreter and relieves the database server altogether.

And it proved quite useful, as my World of Warcraft Gemfinder got picked up by World of Warcraft Insider, a well-read site in the gaming community. The mention of my site on that blog led to a peak of almost 40000 page views in a single day. Good thing I had turned on the application's caching the week before that.

Rails caching can work on three different levels. You can cache entire pages, which you can easily do by adding this to a controller:

class CutGemController < ApplicationController
    ...
    caches_page :list
    ...
end

In this example, the 'list' page will be cached along with the template. Expire your cache with the expire_page command. Make sure you do so in every action which modifies the generated page. If you don't do it, your site will serve an outdated page.

If you still need the template to be dynamic, you can cache just the action. The template will still be rendered dynamically, which is useful if you need dynamic content in the sidebar or elsewhere in your template.

Finally, you can cache page fragments, which is useful for when your application is more dynamic, but you still have parts of the page which are expensive to compute, yet seldom change.

This post isn't a howto and doesn't contain detailed instructions on how to cache using RoR: plenty of sites already explain this in greater details.

I merely wanted to get this point across: Use caching, it's easy, and you never know when you site will get hit by a tsunami of requests. It's a matter of being a good neighbor to the other sites hosted on your server and to keep your site snappy, smooth and responsive.

Tags , , , , ,  | 3 comments

Useful Regular Expressions

Posted by JP Tue, 03 Apr 2007 21:00:00 GMT

I use Regular Expressions quite often as possible because they are the most efficient (code-wise) way to validate and extract information from text strings.

Here's a few of my favorites. Feel free to use them and let me know if you find a glitch:

Date validation/extraction in "2007-12-31" format. This regexp makes sure that the number of days in the month is valid (february always allows 29 days regardless of the year, which is good enough for most applications):

/^([1-9]\d{3})-(?:
(02)-(0[1-9]|[12]\d)
|(0[469]|11)-(0[1-9]|[12]\d|30)
|(0[13578]|1[02])-(0[1-9]|[12]\d|3[01]))$/

Time of day validation/extration in 24h format, from 00:00 (midnight) to 23:59. Hours' leading zero is optional:

/^([0-1]?\d|2[0-3]):([0-5][0-9])$/

Canadian postal code validation, case insensitive:

/^[a-z]\d[a-z] \d[a-z]\d$/i

Phone number validation, with mandatory area code and optional trailing extension in any format (e.g: "514-555-1234 x303"):

/^(\d{3}-){2}\d{4}( .+)?$/

Note that the above regexps should not contain any line breaks. Remove them as you copy/paste them in your code.

Enjoy!

Tags , , , , ,  | no comments