Location: PHPKode > scripts > Cache wrapper > cache.php
<?php
class Cache
{
    var $_cache_type = 'auto'; // äåôîëòíîå çíà÷åíèå
    var $_memcache = null;
    private $t,$syscach=array();
    function __constructor($options = null)
    {
	$this->Cache($options);
    }



   public function getaviable(){
   	    $this->t=array();

        foreach ($this->syscach as $key=>$v) {
        $this->t[]= "<h3> ".$v."</h3>";
		}
		return $this->t;

   }




    function Cache($options = null)
    {
	// Ïðîâåðêà ïîääåðæêè êåø-ñèñòåì
	$cache_systems = array();

	if (function_exists('eaccelerator_get'))  {
	    $cache_systems[] = 'eaccelerator';      $this->syscach[]=   'eaccelerator' ;
                                               }
	if (function_exists('apc_fetch'))     {
	    $cache_systems[] = 'apc';            $this->syscach[]=   'apc' ;
                                           }
	if (function_exists('xcache_get'))  {
	    $cache_systems[] = 'xcache';       $this->syscach[]=   'xcache' ;
                                         }
	if (class_exists('Memcache'))      {
	    $cache_systems[] = 'memcache';
  $this->syscach[]=   'memcache' ;
}

	$required_cache = isset($options['cache']) ? $options['cache'] : $this->_cache_type;




	if (count($cache_systems) && $required_cache != 'none')
	{
	    if ($required_cache == 'auto')
		$this->_cache_type = array_shift($cache_systems); // ïåðâîå çíà÷åíèå â ñïèñêå
	    else if (in_array($required_cache, $cache_systems))
		$this->_cache_type = $required_cache;
	    else
		$this->_cache_type = 'none';
	}
	else
	    $this->_cache_type = 'none';

	if ($this->_cache_type == 'memcache')
	{

	    $failed = true;

	    if (isset($options['memcache']) && is_array($options['memcache']))
	    {

		$this->_memcache = new Memcache;

		foreach ($options['memcache'] as $server)
		{


		    if (!is_array($server) || !isset($server['host'])) // host äîëæåí áûòü óêàçàí
			continue;

		    $server['port'] = isset($server['port']) ? (int) $server['port'] : ini_get('memcache.default_port');
		    $server['persistent'] = isset($server['persistent']) ? (bool) $server['persistent'] : true;

		    if ($this->_memcache->addServer($server['host'], $server['port'], $server['persistent']))
			$failed = false;
		}

		if ($failed)
		    $this->_memcache = null;
	    }

	    if ($failed)
		$this->_cache_type = 'none';
	}
    }

    function get($key)
    {
	$data = null;

	switch ($this->_cache_type)
	{
	    case 'none':
		break;
	    case 'eaccelerator':
		$data = eaccelerator_get($key);
		break;
	    case 'apc':
		$data = apc_fetch($key);
		break;
	    case 'xcache':
		$data = xcache_get($key);
		break;
	    case 'memcache':
		$data = $this->_memcache->get($key);
		break;
	}

	return $data;
    }





 public function clearcache(){

    switch($this->_cache_type){

       case 'eaccelerator':
	    eaccelerator_clean();
        break;


	    case 'apc':
	     apc_clear_cache('user');
		break;




	    case 'xcache':
         $list =$this->_memcache->getList();
  -      xcache_unset('pagecache_active_list');
	   	break;


	    case 'memcache':
	        $this->_memcache->flush();
        break;






    }

 }



    function put($key, &$data, $ttl)
    {

  	switch ($this->_cache_type)
	{
	    case 'none':
		break;
	    case 'eaccelerator':
		eaccelerator_put($key, $data, $ttl);
		break;
	    case 'apc':

		apc_store($key, $data, $ttl);
		break;
	    case 'xcache':
		xcache_set($key, $data, $ttl);
		break;
	    case 'memcache':

		// Ðåøåíèå set() ïðîáëåìû ñ íåñêîëüêèìè ñåðâåðàìè.
		// http://www.php.net/manual/ru/function.memcache-set.php#84032
		if (!$this->_memcache->replace($key, $data, MEMCACHE_COMPRESSED, $ttl))
		    $this->_memcache->set($key, $data, MEMCACHE_COMPRESSED, $ttl);

		break;
	}
    }
}











$chk=new Cache(array('cache'=>'memcache','memcache'=>array(array('host'=>"localhost",'port'=>11211))));    // auto - auto select or leave null to auto select 
//$chk=new Cache();
$rray=array("fdgd"=>"server","dfgd"=>"dfgdgf","ghhj"=>"serveer");
foreach ($chk->getaviable() as $k) echo ($k);
   $chk->put('arr',$rray,50);
print_r($chk->get('arr'));
    $chk->clearcache();







?>
Return current item: Cache wrapper