Setting up Memcached for HTML::Mason

Posted by Michael Palmer on Monday, August 29, 2011

Updated the corporate website today to include memcached. It was hitting our legacy application’s MSSQL database (which we have to still use), a ton, and slowing down the choke windows application.

Anyways, memcached saved the day! Way less hits on the database, and only took a few simple hooks to implement! I know I could have used Mason’s cache, but it isn’t distributed across servers that were not on this web server.

We use HTML::Mason for the site, so just a few simple hooks did the job.

  1. Preloaded the Cache::Memcached module into my mod_perl.
  2. Most of the website is driven off part number lookup.

Even non parts are actually parts in our database, but just have content associated with them. So in the part retrieve Mason page, I added a line to load up memcached.

1
2
3
4
# Load up a new object for Memcached.
my $memd = new Cache::Memcached {
'servers' => [ "10.10.1.44:11211", "10.10.1.40:11211" ],
};
1
2
# I get a $pn variable in from all other places so I check for its existence in the cache.
$mPart = $memd->get($pn);
1
2
3
4
5
6
7
# Then just add a hook around my standard DB call and then a set after the pull and assign if we hit an else.
if (!$mPart) {
$partList = $dbh->selectrow_arrayref("SELECT blablabla from priceBook WHERE itemID = '$pn'");
$memd->set($pn,$partList,600); # Expire cache at 10 minutes (600 seconds).
} else {
$partList = $mPart;
}

comments powered by Disqus