• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List

E:/E/GEAMP/www/openbiz/openbiz/others/Zend/Cache/Backend/Memcached.php

00001 <?php
00027 require_once 'Zend/Cache/Backend/ExtendedInterface.php';
00028 
00032 require_once 'Zend/Cache/Backend.php';
00033 
00034 
00041 class Zend_Cache_Backend_Memcached extends Zend_Cache_Backend implements Zend_Cache_Backend_ExtendedInterface
00042 {
00046     const DEFAULT_HOST = '127.0.0.1';
00047     const DEFAULT_PORT =  11211;
00048     const DEFAULT_PERSISTENT = true;
00049     const DEFAULT_WEIGHT  = 1;
00050     const DEFAULT_TIMEOUT = 1;
00051     const DEFAULT_RETRY_INTERVAL = 15;
00052     const DEFAULT_STATUS = true;
00053     const DEFAULT_FAILURE_CALLBACK = null;
00054 
00058     const TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND = 'Zend_Cache_Backend_Memcached::clean() : tags are unsupported by the Memcached backend';
00059     const TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND =  'Zend_Cache_Backend_Memcached::save() : tags are unsupported by the Memcached backend';
00060 
00091     protected $_options = array(
00092         'servers' => array(array(
00093             'host' => self::DEFAULT_HOST,
00094             'port' => self::DEFAULT_PORT,
00095             'persistent' => self::DEFAULT_PERSISTENT,
00096             'weight'  => self::DEFAULT_WEIGHT,
00097             'timeout' => self::DEFAULT_TIMEOUT,
00098             'retry_interval' => self::DEFAULT_RETRY_INTERVAL,
00099             'status' => self::DEFAULT_STATUS,
00100             'failure_callback' => self::DEFAULT_FAILURE_CALLBACK
00101         )),
00102         'compression' => false,
00103         'compatibility' => false,
00104     );
00105 
00111     protected $_memcache = null;
00112 
00120     public function __construct(array $options = array())
00121     {
00122         if (!extension_loaded('memcache')) {
00123             Zend_Cache::throwException('The memcache extension must be loaded for using this backend !');
00124         }
00125         parent::__construct($options);
00126         if (isset($this->_options['servers'])) {
00127             $value= $this->_options['servers'];
00128             if (isset($value['host'])) {
00129                 // in this case, $value seems to be a simple associative array (one server only)
00130                 $value = array(0 => $value); // let's transform it into a classical array of associative arrays
00131             }
00132             $this->setOption('servers', $value);
00133         }
00134         $this->_memcache = new Memcache;
00135         foreach ($this->_options['servers'] as $server) {
00136             if (!array_key_exists('port', $server)) {
00137                 $server['port'] = self::DEFAULT_PORT;
00138             }
00139             if (!array_key_exists('persistent', $server)) {
00140                 $server['persistent'] = self::DEFAULT_PERSISTENT;
00141             }
00142             if (!array_key_exists('weight', $server)) {
00143                 $server['weight'] = self::DEFAULT_WEIGHT;
00144             }
00145             if (!array_key_exists('timeout', $server)) {
00146                 $server['timeout'] = self::DEFAULT_TIMEOUT;
00147             }
00148             if (!array_key_exists('retry_interval', $server)) {
00149                 $server['retry_interval'] = self::DEFAULT_RETRY_INTERVAL;
00150             }
00151             if (!array_key_exists('status', $server)) {
00152                 $server['status'] = self::DEFAULT_STATUS;
00153             }
00154             if (!array_key_exists('failure_callback', $server)) {
00155                 $server['failure_callback'] = self::DEFAULT_FAILURE_CALLBACK;
00156             }
00157             if ($this->_options['compatibility']) {
00158                 // No status for compatibility mode (#ZF-5887)
00159                 $this->_memcache->addServer($server['host'], $server['port'], $server['persistent'],
00160                                         $server['weight'], $server['timeout'],
00161                                         $server['retry_interval']);
00162             } else {
00163                 $this->_memcache->addServer($server['host'], $server['port'], $server['persistent'],
00164                                         $server['weight'], $server['timeout'],
00165                                         $server['retry_interval'],
00166                                         $server['status'], $server['failure_callback']);
00167             }
00168         }
00169     }
00170 
00178     public function load($id, $doNotTestCacheValidity = false)
00179     {
00180         $tmp = $this->_memcache->get($id);
00181         if (is_array($tmp) && isset($tmp[0])) {
00182             return $tmp[0];
00183         }
00184         return false;
00185     }
00186 
00193     public function test($id)
00194     {
00195         $tmp = $this->_memcache->get($id);
00196         if (is_array($tmp)) {
00197             return $tmp[1];
00198         }
00199         return false;
00200     }
00201 
00214     public function save($data, $id, $tags = array(), $specificLifetime = false)
00215     {
00216         $lifetime = $this->getLifetime($specificLifetime);
00217         if ($this->_options['compression']) {
00218             $flag = MEMCACHE_COMPRESSED;
00219         } else {
00220             $flag = 0;
00221         }
00222         // #ZF-5702 : we try add() first becase set() seems to be slower
00223         if (!($result = $this->_memcache->add($id, array($data, time(), $lifetime), $flag, $lifetime))) {
00224             $result = $this->_memcache->set($id, array($data, time(), $lifetime), $flag, $lifetime);
00225         }
00226         if (count($tags) > 0) {
00227             $this->_log("Zend_Cache_Backend_Memcached::save() : tags are unsupported by the Memcached backend");
00228         }
00229         return $result;
00230     }
00231 
00238     public function remove($id)
00239     {
00240         return $this->_memcache->delete($id);
00241     }
00242 
00258     public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
00259     {
00260         switch ($mode) {
00261             case Zend_Cache::CLEANING_MODE_ALL:
00262                 return $this->_memcache->flush();
00263                 break;
00264             case Zend_Cache::CLEANING_MODE_OLD:
00265                 $this->_log("Zend_Cache_Backend_Memcached::clean() : CLEANING_MODE_OLD is unsupported by the Memcached backend");
00266                 break;
00267             case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
00268             case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
00269             case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
00270                 $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND);
00271                 break;
00272                default:
00273                 Zend_Cache::throwException('Invalid mode for clean() method');
00274                    break;
00275         }
00276     }
00277 
00283     public function isAutomaticCleaningAvailable()
00284     {
00285         return false;
00286     }
00287 
00295     public function setDirectives($directives)
00296     {
00297         parent::setDirectives($directives);
00298         $lifetime = $this->getLifetime(false);
00299         if ($lifetime > 2592000) {
00300             // #ZF-3490 : For the memcached backend, there is a lifetime limit of 30 days (2592000 seconds)
00301             $this->_log('memcached backend has a limit of 30 days (2592000 seconds) for the lifetime');
00302         }
00303         if ($lifetime === null) {
00304             // #ZF-4614 : we tranform null to zero to get the maximal lifetime
00305             parent::setDirectives(array('lifetime' => 0));
00306         }
00307     }
00308 
00314     public function getIds()
00315     {
00316         $this->_log("Zend_Cache_Backend_Memcached::save() : getting the list of cache ids is unsupported by the Memcache backend");
00317         return array();
00318     }
00319 
00325     public function getTags()
00326     {
00327         $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
00328         return array();
00329     }
00330 
00339     public function getIdsMatchingTags($tags = array())
00340     {
00341         $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
00342         return array();
00343     }
00344 
00353     public function getIdsNotMatchingTags($tags = array())
00354     {
00355         $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
00356         return array();
00357     }
00358 
00367     public function getIdsMatchingAnyTags($tags = array())
00368     {
00369         $this->_log(self::TAGS_UNSUPPORTED_BY_SAVE_OF_MEMCACHED_BACKEND);
00370         return array();
00371     }
00372 
00379     public function getFillingPercentage()
00380     {
00381         $mems = $this->_memcache->getExtendedStats();
00382 
00383         $memSize = 0;
00384         $memUsed = 0;
00385         foreach ($mems as $key => $mem) {
00386             if ($mem === false) {
00387                 Zend_Cache::throwException('can\'t get stat from ' . $key);
00388             } else {
00389                 $eachSize = $mem['limit_maxbytes'];
00390                 if ($eachSize == 0) {
00391                     Zend_Cache::throwException('can\'t get memory size from ' . $key);
00392                 }
00393 
00394                 $eachUsed = $mem['bytes'];
00395                 if ($eachUsed > $eachSize) {
00396                     $eachUsed = $eachSize;
00397                 }
00398 
00399                 $memSize += $eachSize;
00400                 $memUsed += $eachUsed;
00401             }
00402         }
00403 
00404         return ((int) (100. * ($memUsed / $memSize)));
00405     }
00406 
00418     public function getMetadatas($id)
00419     {
00420         $tmp = $this->_memcache->get($id);
00421         if (is_array($tmp)) {
00422             $data = $tmp[0];
00423             $mtime = $tmp[1];
00424             if (!isset($tmp[2])) {
00425                 // because this record is only with 1.7 release
00426                 // if old cache records are still there...
00427                 return false;
00428             }
00429             $lifetime = $tmp[2];
00430             return array(
00431                 'expire' => $mtime + $lifetime,
00432                 'tags' => array(),
00433                 'mtime' => $mtime
00434             );
00435         }
00436         return false;
00437     }
00438 
00446     public function touch($id, $extraLifetime)
00447     {
00448         if ($this->_options['compression']) {
00449             $flag = MEMCACHE_COMPRESSED;
00450         } else {
00451             $flag = 0;
00452         }
00453         $tmp = $this->_memcache->get($id);
00454         if (is_array($tmp)) {
00455             $data = $tmp[0];
00456             $mtime = $tmp[1];
00457             if (!isset($tmp[2])) {
00458                 // because this record is only with 1.7 release
00459                 // if old cache records are still there...
00460                 return false;
00461             }
00462             $lifetime = $tmp[2];
00463             $newLifetime = $lifetime - (time() - $mtime) + $extraLifetime;
00464             if ($newLifetime <=0) {
00465                 return false;
00466             }
00467             // #ZF-5702 : we try replace() first becase set() seems to be slower
00468             if (!($result = $this->_memcache->replace($id, array($data, time(), $newLifetime), $flag, $newLifetime))) {
00469                 $result = $this->_memcache->set($id, array($data, time(), $newLifetime), $flag, $newLifetime);
00470             }
00471             return $result;
00472         }
00473         return false;
00474     }
00475 
00490     public function getCapabilities()
00491     {
00492         return array(
00493             'automatic_cleaning' => false,
00494             'tags' => false,
00495             'expired_read' => false,
00496             'priority' => false,
00497             'infinite_lifetime' => false,
00498             'get_list' => false
00499         );
00500     }
00501 
00502 }

Generated on Thu Apr 19 2012 17:01:16 for openbiz by  doxygen 1.7.2