Transparent Caching: Another Self-Proclaimed Genius Moment.
June 27, 2003, 10:06 pmLately, I've been looking closely at our framework, Enterport. I'm concerned about performance and resource efficiency. I know a lot of the base stuff (that gets included every request) could be written better. (Yeah, extensions like php accelerator/turck mmcache/zend performance suite/&c. help this problem (and we use these – they're awesome and everyone should use them), but I don't enjoy feeling like a wasteful programmer.)
I only recently discovered the query log in MySQl. It's like looking at an Apache access_log; you can see every query that's made. So I've been sitting here running tail -f /var/log/mysql.log and thinking, “Do we really need to query the database every request to get the list of categories a user can read?”
Obviously, the answer is no.
So it started with me rewritting a function that gets called pretty often, GetUserCategories(), such that the categories a user can read are cached in their session. But now I'm thinking, why not cache more of these functions?
Much of our framework is template driven, which is really flexible, but can be resource expensive. One of our functions that's called pretty often and can involve a lot of data is GetArticles(), which surprisingly will retrieve articles based on different criteria (a particular category or all categories the user can view, number of articles to show, offset (show articles 5 through 10), sort order, &c.). Each time you look at my blog or my index (or the index on nearly all of our customer's websites), the database must be queried, the template file must be loaded, and the template populated/formatted.
Obviously, the content on my index page doesn't change much (in fact, the content on my blog doesn't change that much). Why not cache this result inside the function? This way, any instance of GetArticles() will be cached.
After maybe fifteen minutes, it works like a champ. It's awesome. I'm totally going to rewrite my RSS feed-providing functions to use this. I'm even considering adding functionality to the Content Management tools so that if articles are added/edited, the caches for that category are deleted (to be recreated the next time someone requests them) yielding no wait for new content.
Of course, none of that is implemented yet, so once I post this, it won't show up for six hours. Or I'll just delete the cache manually because I'm an impatient geek.