Tag Archives: ram

A Memcached Zend_Cache

Zend_Cache

Usually Zend_Cache is used to store cache files on the file system, which can be really fast and useful in most of the cases. However there’s a faster cache mechanism and hopefully it’s supported by Zend_Cache as well. This is the Memcached backend.

A Faster Cache

Memcached is a really powerful tool to cache directly into the RAM. First, this tool has nothing to do primary with Zend Framework. It’s a server, usually started on some port, that can be called to store and get things from the memory. This of course is very fast, way faster than the cache in the hard drives.

Zend_Cache and Memcached

Zend_Cache has an interface to work with Memcached which is great as usual. The PHP example of Memcache (note that there are two things Memcache and Memcached, which are slight different) can be found here and as it says:

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
 
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
 
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
 
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
 
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
 
var_dump($get_result);

However this can be coded into a Zend Framework style like that:

$frontend = array('caching' => true, 'lifetime' => 1800, 'automatic_serialization' => true);
 
$backend = array(
    'servers' =>array(
        array('host' => '127.0.0.1', 'port' => 11211)
    ),
    'compression' => false
);
 
$cache = Zend_Cache::factory('Core', 'Memcached', $frontend, $backend);

Note that you don’t have the typical “cache_dir”, just because everything’s cached into the memory.

Now you can call the cache as it’s called with the “File” backend interface:

$key = 'mykey';
 
if (($result = $cache->load($key)) === false) {
	// call the slow database query here ...
	// save in $result
 
	$cache->save($result, $key);	
}
 
echo $result

Theory of caching. Zend_Cache & Zend Optimizer.

Although it may sound strange I’ll say why I’m using caching. Because it speeds up the site! Yeah there’s no other reason that may be so important as this is.

The caching is not a process invented in the last year, it’s useless to describe why do you need it, but however my intention is to describe how to make it work with Zend Framework.

The docs page of Zend_Cache is pretty clear about everything and I’ll describe the most important part of the cache setup.

First setup the cache back and frontend options. You need to decide where to save the cache files, how to cache them, and what lifetime do they need.

The most used cache configuration is, as you may guess, this one described into the doc page. Setup the front and backend options is just as simple as:

$frontendOptions = array(
     'lifetime' => 7200, // cache lifetime of 2 hours
     'automatic_serialization' => true
);

$backendOptions = array(
   'cache_dir' => './tmp/'
   // Directory where to put the cache files
);

// getting a Zend_Cache_Core object
$cache = Zend_Cache::factory('Core',
                             'File',
                              $frontendOptions,
                              $backendOptions);

Because all this is well described into the doc page, which you can found here I won’t talk about it. But I’ll talk only about one sole thing. How much the cache must live? What should be its lifetime.

The answer is simple. If you have some basic statistic of your site traffic and you don’t change much the data you can prefer longer cache period. But imagine the site’s making 1000 visits per minute! Than even if you cache every minute and the cache expires every minute will be a success and the user perception will be really great. From those 1000 visitors only few will wait until the cache’s built and the other hundreds will receive really fast response!

Be careful when you cache only because you can overflow the directory where you cache, which will be difficult though.

In other way many people are thinking about Zend Optimizer and its work with Zend_Cache. These are really different things. Zend Optimizer has nothing to do with your code it is a PHP caching tool. As you may know PHP interpreter mingles the code into “machine like” code, which in turn can be cached into the RAM with the help of Zend Optimizer. Checkout more here.