Article: Caching Hat-trick: Varnish, Memcached and PHP libraries

An excerpt from http://www.sitepoint.com/caching-hat-trick-varnish-memcached-and-php-libraries/, by Wern Ancheta

Previously, we looked at some common caching mechanisms we can easily utilize to make our apps faster. In this part, we’ll walk through some of the additional software that we can use with PHP for caching.

Memcached

Memcached is an in-memory key-value store. You can use it to store things like strings, numeric values, objects and arrays.

Installing Memcached

You can execute the command below to install memcached on ubuntu or other debian based OS:

sudo apt-get install memcached

To make it work with PHP, you also need to install the PHP extension:

sudo apt-get install php5-memcached

To check if memcached is working, look for ‘memcached’ in the output returned when you call the phpinfo() method from a page. You should see something similar to the following:

Using Memcached

To use memcached, first we create a new instance of the Memcached class. We then specify which server we want to connect to by calling the addServer method. $memcached_host is the IP or domain name of the server and the $memcached_port is the port where the memcached server runs. The default is 11211.

$mem = new Memcached();
$memcached_host = '127.0.0.1';
$memcached_port = 11211;
$mem->addServer($memcached_host, $memcached_port);

Once that’s done, you can use the set method to cache a specific key-value pair. The set method accepts a unique key as its first argument, and the data that you want to cache as the second, while the third is the duration of the data’s lifetime, in seconds:

$id = 23;
$my_data = array('name' => 'gon', 'occupation' => 'hunter');
$ttl = 60;
$mem->set($id, $my_data, $ttl);

If you want to get the data back, you can use the get method. Just use the unique key as the query:

$my_data = $mem->get(23);
if($my_data){
    return $my_data;
}else{
    //fetch data from database
}

To further optimize memcached, you can configure its settings. You can do that by editing the memcached configuration file: /etc/memcached.conf.

Here are some of the options that you might find useful. Just uncomment them or edit the existing values:

-v – if you want to show more information while memcached is running.
-vv – if you can’t find what you’re looking for in verbose mode, you can see even more information when you set this option. Very useful when debugging.
-m – the maximum amount of memory that memcached can use. By default this is set to 64mb.
-M – tells memcached to return an error when the maximum amount of memory is already exhausted. By default this option is not set. Instead, it automatically removes items from the cache.
-c – the maximum number of simultaneous connections allowed. The default is 1024.

You can also install phpMemcachedAdmin on your memcached server. It will show you things like the total number of current connections, connection errors, current items, the total amount of memory used and more data that you might find useful. Here’s a screenshot:


Continue reading this article on SitePoint!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.