Memcache

We call memcache an enterprise level caching system for it's ability to store cache on multiple servers. This also implies that your server needs to have memcache already installed on your server. If it is not, contact your server guy. It's really not a hard install. Since Eden cannot install this, we'll just assume that it is installed. Eden uses Memcached vs Memcache simply because in our experience we had better results installing and running it in the past. Just because Memcache is a robust and scalable solution, doesn't mean it's hard to learn. To get started, you'll need to create a cache manager.

Figure 1. Setting up memcache
$cache = eden('memcache', 'localhost', 11211);

If you wanted to setup a server pool, this can be accomplished by simply adding new servers as in Figure 2

Figure 2. Adding more servers
$cache->addServer('localhost', 11211);	
$cache->addServer('localhost', 11211, true, 20); //persistent connection and weight set to 20

Once your server/s are all configured you can now call the typical setters and getters found on most cache managers.

Figure 3. Typical calls
$cache->set('test', 'test data', 1, 3600); 	// set data in the cache by keyword with a 
											// flag of 1 set to expire an hour from now
											
$cache->get('test');				//--> test data
$cache->remove('test');				// invalidates this cache
$cache->clear();					// invalidates all cache objects

So now that we understand all the main methods of the cache manager. Let's show how we can form a simple cache process.

Figure 4. Simple Cache Example
$test = $cache->get('test');
if(!trim($test)) {							//if there is no data
	$test = 'something data '.time();		//form the data
	$cache->set('test', $test, 3600);	//cache the data
}

//either way echo test
echo $test;

Figure 4 shows that we are using a cache object called test which is set to rebuild every hour. For something in between simple file cache and enterprise memcaching. PHP also made available a system called APC. We will cover APC in our next section APC Cache.


© 2012 Openovate Labs. All rights reserved.