The journal of a software developer who has a fondness of cheese
Simple Caching for Blog Posts.
I maintain a website for my local soccer team called Knights Army which is written in rails3 using a basic blog system I wrote myself. I’m using the
RedCloth gem to process textile for blog posts.
The website only gets a very small amount of visitors a day, so performance
isn’t a concern of mine at all. The website does feel very snappy when
navigating already. I was looking over the code that I had written a few months
back and noticed that for each post (can be up to 10 shown per page) I was
running RedCloth over the body contents like this:
# app/models/post.rb# Yes i realise post is a really bad name for a modeldefbody_htmlRedCloth.new(self.body).to_html.html_safeend
I had a simple idea to improve the performance of loading the posts. I created
a new text database column called cached_body on the posts table, then added
some logic so that it only runs RedCloth when the post contents change, or when
there is no cached_body already.
I ran some benchmarks using the apache benchmark tool for a rough guide of the performance gains. I ran the command ab -c 100 -n 1000 http://www.knightsarmy.net a total of 5 times and then took the 3 middle scores to
make an average, here’s the results: