/** * Construct the adapter, giving an array of servers. * @example * array( * 'prefix' => '', * 'persistent_id' => '', * 'servers' => array( * array ( * 'host' => 'cache1.example.com', * 'port' => 11211, * 'weight' => 1 * ), * array( * 'host' => 'cache2.example.com', * 'port' => 11211, * 'weight' => 2 * ) * ) * ) * @param array $config */ public function __construct(array $config = array()) { try { if (array_key_exists('prefix', $config)) { $this->prefix = $config['prefix']; } if (array_key_exists('persistent_id', $config) && !empty($config['persistent_id'])) { // @codeCoverageIgnoreStart $this->memcached = new \Memcached($config['persistent_id']); } else { // @codeCoverageIgnoreEnd $this->memcached = new \Memcached(); } foreach ($config['servers'] as $server) { $this->memcached->addserver($server['host'], $server['port'], $server['weight']); } if (array_key_exists('options', $config)) { foreach ($config['options'] as $optionKey => $optionValue) { $this->memcached->setOption($optionKey, $optionValue); } } } catch (\Exception $e) { // @codeCoverageIgnoreStart $this->memcached = null; // @codeCoverageIgnoreEnd } }
/** * Initializes the identifier prefix * * @return void * @throws Exception */ public function initializeObject() { if (empty($this->servers)) { throw new Exception('No servers were given to Memcache', 1213115903); } $memcachedPlugin = '\\' . ucfirst($this->usedPeclModule); $this->memcache = new $memcachedPlugin(); $defaultPort = $this->usedPeclModule === 'memcache' ? ini_get('memcache.default_port') : 11211; foreach ($this->servers as $server) { if (substr($server, 0, 7) === 'unix://') { $host = $server; $port = 0; } else { if (substr($server, 0, 6) === 'tcp://') { $server = substr($server, 6); } if (strpos($server, ':') !== false) { list($host, $port) = explode(':', $server, 2); } else { $host = $server; $port = $defaultPort; } } $this->memcache->addserver($host, $port); } if ($this->usedPeclModule === 'memcached') { $this->memcache->setOption(\Memcached::OPT_COMPRESSION, $this->getCompression()); } }
/** * Sets up and returns the CacheProvider * @param $config * @return \Doctrine\Common\Cache\CacheProvider */ protected function initialize($config) { $memcached = new \Memcached(); $memcached->addserver($config['host'], $config['port']); $cache = new MemcachedCache(); $cache->setMemcached($memcached); return $cache; }
/** * Initializes usage of memcache or memcached */ private function __construct() { $ini = eZINI::instance('merck.ini'); if ( $ini->hasSection('MemcacheSettings') ) { $this->_host = $ini->variable('MemcacheSettings', 'Host'); $this->_port = $ini->variable('MemcacheSettings', 'Port'); } elseif( strpos(ini_get('session.save_handler'), 'memcache') === 0 && preg_match('#^(?:tcp://)?(?P<memcacheHost>[^:]+):(?P<memcachePort>[^?]+)#', ini_get('session.save_path'), $m) ){ // No memcache settings set, we try to use the session handler one $this->_host = $m['memcacheHost']; $this->_port = $m['memcachePort']; } if ( $this->_host ) { if ( extension_loaded('memcached') ) { $this->_memcache = new Memcached(); $this->_type = self::TYPE_MEMCACHED; $this->_isValid = $this->_memcache->addserver( $this->_host, $this->_port ); } elseif( extension_loaded( 'memcache') ) { $this->_memcache = new Memcache(); $this->_type = self::TYPE_MEMCACHE; $this->_isValid = $this->_memcache->connect( $this->_host, $this->_port ); } } if ( !$this->isValid() ) { eZDebug::writeError('Could not find any valid memcache configuration or memcache module', 'MemcacheTool'); // do not break. Any memcache w/r should be fault tolerant in case memcache clears its cache by itself. } }