Exemple #1
0
 /**
  * Do initialization.
  *
  * @throws Horde_Memcache_Exception
  */
 public function _init()
 {
     if (class_exists('Memcached')) {
         if (empty($this->_params['persistent'])) {
             $this->_memcache = new Memcached();
         } else {
             $this->_memcache = new Memcached('horde_memcache');
         }
         $this->_params['large_items'] = false;
         $this->_memcache->setOptions(array(Memcached::OPT_COMPRESSION => $this->_params['compression'], Memcached::OPT_DISTRIBUTION => Memcached::DISTRIBUTION_CONSISTENT, Memcached::OPT_HASH => Memcached::HASH_MD5, Memcached::OPT_LIBKETAMA_COMPATIBLE => true, Memcached::OPT_PREFIX_KEY => $this->_params['prefix']));
     } else {
         // Force consistent hashing
         ini_set('memcache.hash_strategy', 'consistent');
         $this->_memcache = new Memcache();
     }
     for ($i = 0, $n = count($this->_params['hostspec']); $i < $n; ++$i) {
         if ($this->_memcache instanceof Memcached) {
             $res = $this->_memcache->addServer($this->_params['hostspec'][$i], empty($this->_params['port'][$i]) ? 0 : $this->_params['port'][$i], !empty($this->_params['weight'][$i]) ? $this->_params['weight'][$i] : 0);
         } else {
             $res = $this->_memcache->addServer($this->_params['hostspec'][$i], empty($this->_params['port'][$i]) ? 0 : $this->_params['port'][$i], !empty($this->_params['persistent']), !empty($this->_params['weight'][$i]) ? $this->_params['weight'][$i] : 1, 1, 15, true, array($this, 'failover'));
         }
         if ($res) {
             $this->_servers[] = $this->_params['hostspec'][$i] . (!empty($this->_params['port'][$i]) ? ':' . $this->_params['port'][$i] : '');
         }
     }
     /* Check if any of the connections worked. */
     if (empty($this->_servers)) {
         throw new Horde_Memcache_Exception('Could not connect to any defined memcache servers.');
     }
     if ($this->_memcache instanceof Memcache && !empty($this->_params['c_threshold'])) {
         $this->_memcache->setCompressThreshold($this->_params['c_threshold']);
     }
     if (isset($this->_params['logger'])) {
         $this->_logger = $this->_params['logger'];
         $this->_logger->log('Connected to the following memcache servers:' . implode($this->_servers, ', '), 'DEBUG');
     }
 }