Powerful PHP cache type (eaccelerator, APC, XCache, memcache module cache)

Source: Internet
Author: User
Tags apc
Powerful PHP Cache Class (Eaccelerator, APC, XCache, memcache module cache)
 Cache_expire= $exp _time;        $this->cache_params= $params;    $this->setcachetype ($type);    } public Function __destruct () {unset ($this->cache_external); }//Prevent users to clones the instance public function __clone () {$this->cacheerror (' Clone is not a    llowed. '); }//deletes cache from folder Public Function ClearCache () {switch ($this->cache_type) {CAs                E ' eaccelerator ': @eaccelerator_clean ();            @eaccelerator_clear ();             Break            Case ' APC ': Apc_clear_cache (' user ');             Break            Case ' XCache ': Xcache_clear_cache (Xc_type_var, 0);             Break            Case ' memcache ': @ $this->cache_external->flush ();                         Break            Case ' Filecache ': $this->cache_external->deletecache ();        Break }}//writes or reads thE Cache Public Function cache ($key, $value = ', $ttl = ') {if ($value! = ") {//wants to write if ($ttl = ="            ) $ttl = $this->cache_expire;        $this->put ($key, $value, $ttl); } else return $this->get ($key);//reading value}//creates new cache files with the given data, $key = = Name of the cache, data the info/values to store private function put ($key, $data, $ttl = ") {if ($ttl = =") $ttl                 = $this->cache_expire; Switch ($this->cache_type) {case ' eaccelerator ': Eaccelerator_put ($key, Serialize ($data), $ttl            );             Break            Case ' APC ': Apc_store ($key, $data, $ttl);             Break            Case ' XCache ': Xcache_set ($key, Serialize ($data), $ttl);             Break                Case ' memcache ': $data =serialize ($data); if (! $this->cache_external->replace ($key, $data, False, $ttl)) $this->cAche_external->set ($key, $data, False, $ttl);                         Break            Case ' Filecache ': $this->cache_external->cache ($key, $data);        Break            }}//returns cache for the given key private function get ($key) {switch ($this->cache_type) {            Case ' eaccelerator ': $data = @unserialize (Eaccelerator_get ($key));             Break            Case ' APC ': $data = Apc_fetch ($key);             Break            Case ' XCache ': $data = @unserialize (Xcache_get ($key));             Break            Case ' memcache ': $data = @unserialize ($this->cache_external->get ($key));                         Break            Case ' Filecache ': $data = $this->cache_external->cache ($key);        Break }/*echo '
--returnning data for key: '. $key; Var_dump ($data); */return $data; }//delete key from the cache public function Delete ($key) {switch ($this->cache_type) {case ' E Accelerator ': Eaccelerator_rm ($key); Break Case ' APC ': Apc_delete ($key); Break Case ' XCache ': Xcache_unset ($key); Break Case ' memcache ': $this->cache_external->delete ($key); Break Case ' Filecache ': $this->cache_external->delete ($key); Break }}//overloading for the application variables and automatically cached public function __set ($name, $value) {$this->put ($name, $value, $this->cache_expire); The Public Function __get ($name) {return $this->get ($name); } Public Function __isset ($key{//echo "is ' $name ' set?\n" if ($this->get ($key)!== false) return true; else return false; } Public Function __unset ($name) {//echo ' unsetting ' $name ' \ n '; $this->delete ($name); }//end overloads Public Function Getcachetype () {return $this $this->cache_type; }//sets the cache if its installed if not triggers error public function Setcachetype ($type) {$this-& Gt;cache_type=strtolower ($type); Switch ($this->cache_type) {case ' eaccelerator ': if (function_exists (' Eaccelerator_get ')) $thi S->cache_type = ' eaccelerator '; else $this->cacheerror (' eaccelerator not found '); Break Case ' APC ': if (function_exists (' Apc_fetch ')) $this->cache_type = ' APC '; else $this->cacheerror (' APC not found '); Break Case ' XCache ': if(Function_exists (' Xcache_get ')) $this->cache_type = ' XCache '; else $this->cacheerror (' Xcache not found '); Break Case ' memcache ': if (class_exists (' memcache ')) $this->init_memcache (); else $this->cacheerror (' memcache not found '); Break Case ' Filecache ': if (class_exists (' Filecache ')) $this->init_filecache (); else $this->cacheerror (' Filecache not found '); Break Case ' auto '://try to Auto Select a cache system if (function_exists (' Eaccelerator_get ')) $this->cache _type = ' eaccelerator '; ElseIf (function_exists (' Apc_fetch ')) $this->cache_type = ' APC '; ElseIf (function_exists (' Xcache_get ')) $this->cache_type = ' XCache '; ElseIf (class_exists (' Memcache ')) $this->init_memcache (); ElseIf (class_exists (' Filecache ')) $this->init_filecache (); else $this->cacheerror (' Not any compatible cache is found '); Break Default://not any cache selected or wrong one selected if (Isset ($type)) $msg = ' Unrecognized cache type Sele Cted '. $type. ' '; else $msg = ' Not any cache type selected '; $this->cacheerror ($msg); Break }} Private Function Init_memcache () {//get instance of the Memcache class if (Is_array ($this->c Ache_params) {$this->cache_type = ' memcache '; $this->cache_external = new Memcache; foreach ($this->cache_params as $server) {$server [' port '] = isset ($server [' Port '])? (int) $server [' Port ']: Ini_get (' Memcache.default_port '); $server [' persistent '] = Isset ($server [' persistent '])? (bool) $server [' Persistent ']: true; $this->cache_external->addserver ($server [' Host '], $server [' Port '], $server [' persistent ']; }} else $this->cacheerror (' memcache needs an array, Example:wrappercache::getinstanc E (\ ' memcache\ ', 30,array (array (\ ' host\ ' =>\ ' localhost\ ')); } Private Function Init_filecache ({//get instance of the Filecache class $this->cache_type = ' Filecache '; $this->cache_external = filecache::getinstance ($this->cache_expire, $this->cache_params); } Public Function Getavailablecache ($return _format= ' html ') {//returns the available cache $avCaches = Arra Y (); $avCaches [] = Array (' Eaccelerator ', function_exists (' Eaccelerator_get ')); $avCaches [] = Array (' APC ', function_exists (' Apc_fetch ')); $avCaches [] = Array (' XCache ', function_exists (' Xcache_get ')); $avCaches [] = Array (' Memcache ', class_exists (' memcache ')); $avCaches [] = Array (' Filecache ', class_exists (' Filecache ')); if ($return _format== ' html ') {$ret = '
      '; foreach ($avCaches as $c) {$ret. = '
    • '. $c [0]. '-'; if ($c [1]) $ret. = ' found/compatible '; else $ret. = ' Not found/incompatible '; $ret. = '; } return $ret. '
'; } else return $avCaches; } Private Function Cacheerror ($msg) {//triggers error trigger_error ('
Wrappercache Error: '. $msg. '
If you want your can try with \ ' auto\ ' for Auto Select a compatible cache.
Or Choose a supported cache from list: '. $this->getavailablecache (), e_user_error); }}?>

? file-based

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.