Cache Rules Everything Around Me
shoemoney
·
·
3 min read
Dillsmack went to Italy for a bit so I am playing with code on my own. (expect downtime). Anyway I been playing with magpierss, wp-cache, and memcached and woah.
The Wu-Tang clan knew what they were talking about with C.R.E.A.M
wp-cache is the plugin for wordpress which uses file level caching. This works really good for most things but its kind of touch and go. I really am kind of puzzled why wordpress does not include this incorporate this in wordpress. It takes a HUGE load off of your database server and will keep you up if you get slash doted or dugg.
memcached - caching at ram level wooooot. This is pretty badass and I am having a lot of fun with it. My friend Shawn Hogan tried to turn me onto it a while ago but I just could not wrap my head around it. While I use wp-cache for most of this blog the custom plugins i wrote I use a 512mb memcache to store the results for 60 seconds. The code I wrote for them is really bad but the cool thing about memcache is it makes up for your bad code. I just cache the output of the functions and whola lightning fast..as ram!
Magpie RSS. Magpie rss is what I used to build my spider-bot that crawls my friends RSS feeds and all the forums to produce the marketplace feed. I memcache the results on the marketplace ever 30 seconds incase it gets hammered (and it has been getting hammered pretty good). Anyway magpie has built in caching so I dont hammer my friends rss feeds to bad.
EDIT:
Here is an example of my code for the top commentors on the side bar that I wrote and the memcache that caches in information:
function posse()
{
#start cache
$cache_key = "posse";
$memcache = new Memcache;
$memcache->addServer('127.0.0.1', 11211,0,1,1,1);
$output = $memcache->get($cache_key);
if (!$output) {
ob_start();
#end Start Cache
global $wpdb, $tablecomments;
print "
";
$commenters = $wpdb->get_results("SELECT COUNT(comment_author) AS commentcount, comment_author, comment_author_url FROM $tablecomments
WHERE unix_timestamp(comment_date)>unix_timestamp(NOW())-(60*60*24*7)
AND comment_author_url !=''
AND comment_author !='ShoeMoney'
AND comment_author != 'ddn'
AND comment_approved = '1'
AND comment_type =''
GROUP BY comment_author
ORDER BY commentcount DESC
LIMIT 20;");
foreach($commenters as $commenter)
{
print " ";
}
print "
";
#End of Cache Code
$output = ob_get_contents();
$memcache->set($cache_key, $output, false, 60); // 60 second cache
ob_end_flush();
} else {
echo $output;
return;
}
#End of Cache Code
}
So for those who understand php you can see this would be a pretty huge load for the 65,000+** hits this recieves a day... but by caching this for 60 seconds you drastically lower that ;)
Cacheing rocks!
**hits refers to total pageloads not unique visitors. This site only gets between 10,000 and 15,000 unqiues per day