Categories
PHP

PHP wrapper class for cache engines

Make your life lot easier dealing with different cache engines by just using this PHP wrapper class. Perfect if you want to change the cache engine of your app almost without touching code 😉

Currently supports: fileCache, memcache, APC, Xcache and eaccelerator

Example:

//creating new instance singleton
$cache = wrapperCache::GetInstance('filecache',36,'cache/');//last 2 params are optional
//or set it in auto and the class will choose the cache engine for you ;)
$cache = wrapperCache::GetInstance('auto');

//caching values
$cache->thevar='I'm the bar from wrapper cache using filecache ';//overload of set and get methods
//or:
$cache->cache('thevar','test values for the var!!!!');

//retrieving value for a key
echo $cache->thevar;//get
//or:
echo $cache->cache('thevar');//get

//isset and unset are overloaded
var_dump(isset($cache->thevar));
unset($cache->thevar);

//displays availables cache engines in your system:
echo $cache->getAvailableCache();

//flush all cache
$cache->clearCache();


Memcache initiation example:

//example to add multiple servers of memcache
$memcache_servers =
		array (
				array('host'=>'localhost','port'=>11211),
				array('host'=>'172.16.0.3')
		);

$cache = wrapperCache::GetInstance('memcache',30, $memcache_servers );//creating new instance singleton

//or simpler way:
$cache = wrapperCache::GetInstance('memcache',30,array(array('host'=>'localhost')));

if ($cache->site==null){
	echo 'inside';
	$cache->cache('site',strip_tags(file_get_contents("http://yahoo.com")),100);//time overload
}
echo $cache->site;

Download