/** * Settings the memcached instance * * @throws CacheException when the Memcached extension is not built with the desired serializer engine * @return void */ protected function _setOptions() { $this->_Memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true); $serializer = strtolower($this->settings['serialize']); if (!isset($this->_serializers[$serializer])) { throw new CacheException( __d('cake_dev', '%s is not a valid serializer engine for Memcached', $serializer) ); } if ($serializer !== 'php' && !constant('Memcached::HAVE_' . strtoupper($serializer))) { throw new CacheException( __d('cake_dev', 'Memcached extension is not compiled with %s support', $serializer) ); } $this->_Memcached->setOption(Memcached::OPT_SERIALIZER, $this->_serializers[$serializer]); // Check for Amazon ElastiCache instance if (defined('Memcached::OPT_CLIENT_MODE') && defined('Memcached::DYNAMIC_CLIENT_MODE')) { $this->_Memcached->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE); } $this->_Memcached->setOption(Memcached::OPT_COMPRESSION, (bool)$this->settings['compress']); }
/** * 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()); } }
/** * set consistent-hash * 每次set会自动摘除问题服务器;每次get,前两次失败后,会成功查出问题服务器 * @param void * @return void */ protected function _setOption($switch) { if ($this->_memcache && $switch) { $this->_memcache->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT); $this->_memcache->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true); $this->_memcache->setOption(Memcached::OPT_REMOVE_FAILED_SERVERS, true); } }
/** * Settings the memcached instance * */ protected function _setOptions() { $this->_Memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true); $this->_Memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); if (Memcached::HAVE_IGBINARY) { $this->_Memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY); } $this->_Memcached->setOption(Memcached::OPT_COMPRESSION, (bool) $this->settings['compress']); }
/** * Setter for compression flags bit * * @param boolean $useCompression * @return void * @api */ protected function setCompression($useCompression) { if ($this->memcache instanceof \Memcached) { $this->memcache->setOption(\Memcached::OPT_COMPRESSION, $useCompression); } else { if ($useCompression === true) { $this->flags ^= MEMCACHE_COMPRESSED; } else { $this->flags &= ~MEMCACHE_COMPRESSED; } } }
/** * Initializes */ private function initialize($config) { if (empty($config['servers'])) { return false; } if (defined('\\Memcached::OPT_REMOVE_FAILED_SERVERS')) { $this->_memcache->setOption(\Memcached::OPT_REMOVE_FAILED_SERVERS, true); } if (isset($config['aws_autodiscovery']) && $config['aws_autodiscovery'] && defined('\\Memcached::OPT_CLIENT_MODE') && defined('\\Memcached::DYNAMIC_CLIENT_MODE')) { $this->_memcache->setOption(\Memcached::OPT_CLIENT_MODE, \Memcached::DYNAMIC_CLIENT_MODE); } foreach ((array) $config['servers'] as $server) { if (substr($server, 0, 5) == 'unix:') { $this->_memcache->addServer(trim($server), 0); } else { list($ip, $port) = explode(':', $server); $this->_memcache->addServer(trim($ip), (int) trim($port)); } } if (isset($config['username']) && !empty($config['username']) && method_exists($this->_memcache, 'setSaslAuthData') && ini_get('memcached.use_sasl')) { $this->_memcache->setSaslAuthData($config['username'], $config['password']); } return true; }
/** * 获得Memcache连接 * * @return Memcache Memcache连接 */ public static function get_memcached() { if (Cache::$memcached === null) { Cache::$memcached = new Memcached('ocs'); if (count(Cache::$memcached->getServerList()) == 0) { /*建立连接前,先判断*/ /*所有option都要放在判断里面,因为有的option会导致重连,让长连接变短连接!*/ Cache::$memcached->setOption(Memcached::OPT_COMPRESSION, false); Cache::$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true); /* addServer 代码必须在判断里面,否则相当于重复建立’ocs’这个连接池,可能会导致客户端php程序异常*/ foreach (Conf::$memcached as $k => $memconf) { Cache::$memcached->addServer($memconf[0], $memconf[1]); } if (Conf::$memcached_un) { Cache::$memcached->setSaslAuthData(Conf::$memcached_un, Conf::$memcached_pwd); } } return Cache::$memcached; } else { return Cache::$memcached; } }