MemCached: Quick Guide to Faster Sites

There comes a time in the life of every web application and the developers who make them, when it’s just not fast enough – traffic is growing (which is good) but page load times are dropping (which is bad) and you’ve already optimized your SQL queries and your code as much as you can. So what do you do next? You could call your web hosting company to see about getting a more powerful server to run your site but that will cost you more money. Far better to implement a caching system which will make sure pages load much faster and you can delay that call to your web host for a while. 

So which cache should you use? Well there are a number of cache options you could look into but one of the most popular is MemCached which is used by the likes of Twitter, WordPress, Youtube and many more.

“Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.”

Using it in your code is very simple and there are clients available for many languages. In this case I will use Perl as an example..

First install the MemCached lib on your server, assuming you use Centos/RHEL just do this..

yum install memcached 

Then install the perl module Cache::Memcached, again with yum (or from CPAN if you prefer)..

yum install perl-Cache-Memcached

That is all it takes to install. Now just start the memcached service..

service memcached start

Now it is ready to use, you don’t even need to configure anything (although you can if you want – see /etc/sysconfig/memcached) and by default it runs on port 11211 but you can change that and even run multiple copies on different ports.

Now comes the (not very) tricky part of actually implementing it in your code!

First add the perl module and then define the connection..

[code]use Cache::Memcached;

my $Cache = new Cache::Memcached {
‘servers’ => [ “localhost:11211” ],
‘debug’ => 0,
‘compress_threshold’ => 10_000,
};
[/code]

Replace localhost and 11211 if you are running it on another server or port.

Now you just need to read and write to the cache, which is simple. Lets say you want to cache the index page..

[code]my $IndexKey = $SomeID . “_index”;   # create any unique key you want for the page you want to cache.
my $CachedIndex = $Cache->get($IndexKey);  # now you have the cached version ready to use

# if the page wasn’t already cached then create it as usual and then add to cache for next time..
unless($CachedIndex)
{
# generate page content in the usual way..
$IndexPage = MakePage(); #select from db or whatever

# now add it to cache for next time..
$Cache->set($IndexKey,$IndexPage,3600);   # 3600 is the time to keep it cached
}
[/code]
That’s it – simples!

You can see how the cache is configured and performing with these commands..

[code]echo “stats settings” | nc localhost 11211[/code]

[code]echo “stats” | nc localhost 11211[/code]

 

Dead simple really – just add some data to the cache with a key and read it back with that key any time you want. Do it for a whole page or for any part of a page or any big/slow selects or functions.

 

 

 

You may also like...