/** * Connects to the server * * @return boolean */ function connect() { if (!empty($this->config['servers'])) { $persistant = isset($this->config['persistant']) ? (bool) $this->config['persistant'] : false; foreach ((array) $this->config['servers'] as $server) { list($ip, $port) = explode(':', $server); $this->_memcache->addServer(trim($ip), (int) trim($port), $persistant); } } else { return false; } if (!empty($this->config['compress_threshold'])) { $this->_memcache->setCompressThreshold((int) $this->config['compress_threshold']); } return true; }
function __construct($host, $port = 11211) { $mc = new Memcache(); $mc->addServer($host, intval($port)); $mc->setCompressThreshold(self::CompressThreshold); $this->_memcache = $mc; }
protected function initialize($prefix, DataSourceMetaData $datasource = NULL) { $result = TRUE; if (class_exists('Memcache')) { $this->memcache = new Memcache(); $successfulRegistrationCount = $unsuccessfulRegistrationCount = 0; // adding servers if (isset($datasource->host)) { $serverResult = $this->registerServer($datasource->host, $datasource->port); if ($serverResult) { $successfulRegistrationCount++; } else { $unsuccessfulRegistrationCount++; } } if (isset($datasource->servers)) { foreach ($datasource->servers as $server) { $serverResult = $this->registerServer($server->host, $server->port); if ($serverResult) { $successfulRegistrationCount++; } else { $unsuccessfulRegistrationCount++; } } } if ($successfulRegistrationCount == 0) { $this->memcache = NULL; } else { $this->memcache->setCompressThreshold(self::$DEFAULT__COMPRESSION_THRESHOLD, self::$DEFAULT__COMPRESSION_SAVINGS_MIN); } if ($unsuccessfulRegistrationCount > 0) { $result = FALSE; } } return $result; }
/** * Starts the cache server connection * Private method to call from other static methods of the class */ private static function _init() { if (self::$connected) { return; } if (self::$driver == 'memcache') { $conn = new Memcache(); $conn->connect(self::$host, self::$port); $conn->setCompressThreshold(1000, 0.2); self::$conn = $conn; } }
/** * constructor * * @param array $config */ function __construct($config) { parent::__construct($config); $this->_memcache = new Memcache(); if (!empty($config['servers'])) { $persistant = isset($config['persistant']) ? (bool) $config['persistant'] : false; foreach ((array) $config['servers'] as $server) { if (substr($server, 0, 5) == 'unix:') { $this->_memcache->addServer(trim($server), 0, $persistant); } else { list($ip, $port) = explode(':', $server); $this->_memcache->addServer(trim($ip), (int) trim($port), $persistant); } } } else { return false; } if (!empty($config['compress_threshold'])) { $this->_memcache->setCompressThreshold((int) $config['compress_threshold']); } return true; }
function rs_memcache_init() { global $memcache; global $memcacheservers; //Set in config if (!isset($memcache)) { //Init cache instance on first hit $memcache = new Memcache(); foreach ($memcacheservers as $server) { $memcache->addServer($server); } $memcache->setCompressThreshold(1000, 0.2); } }
protected function connect() { if (!isset($this->instance)) { $this->memcache = new PhpMemcache(); $this->memcache->addServer($this->configuration['host'], $this->configuration['port']); $status = @$this->memcache->getExtendedStats(); // unset $status[$server] or $status[$server] === false if (empty($status[$this->server])) { throw new Exception('Memcache connect failed about cache source "{0}"', [$this->source]); } // if ($this->memcache->getServerStatus($this->config['host'], $this->config['port']) === 0) { // if ($this->config['pconnect']) // $conn = @$this->memcache->pconnect($this->config['host'], $this->config['port']); // else // $conn = @$this->memcache->connect($this->config['host'], $this->config['port']); // if ($conn === false) // throw new Exception('Memcache connect failure about cache source "{0}"', [$this->name]); // } if ($this->configuration['compressThreshold'] > 0 && $this->configuration['compressRatio'] > 0) { $this->memcache->setCompressThreshold($this->configuration['compressThreshold'], $this->configuration['compressRatio']); } } return $this; }
/** * Get memcache object * * @return object */ public function getMemcacheObj() { static $memObj; if(!$memObj){ if (!is_array($this->hosts) || empty($this->hosts)){ return null; } $memcache = new Memcache(); foreach($this->hosts as $host){ if(isset($host[1])){ $memcache->addServer($host[0], $host[1]); } else { $memcache->addServer($host[0], MEMSERVER_DEFAULT_PORT); } } $memcache->setCompressThreshold(10000, 0.2); $memObj = $memcache; } return $memObj; }
public static function getMcConnectionObj() { static $memcache = NULL; if (isset($memcache)) { return $memcache; } //$memcache_servers = \OLOG\ConfWrapper::value(\OLOG\Model\ModelConstants::MODULE_CONFIG_ROOT_KEY . '.memcache_servers'); $memcache_servers = CacheConfig::getServersObjArr(); if (!$memcache_servers) { return null; } // Memcached php extension not supported - slower, rare, extra features not needed /** @var \Memcache $memcache */ $memcache = new \Memcache(); /** @var MemcacheServerSettings $server_settings_obj */ foreach ($memcache_servers as $server_settings_obj) { \OLOG\Assert::assert($memcache->addServer($server_settings_obj->getHost(), $server_settings_obj->getPort())); $memcache->setCompressThreshold(5000, 0.2); } return $memcache; }
/** * Do initialization. * * @throws Horde_Memcache_Exception */ public function _init() { $this->_memcache = new Memcache(); for ($i = 0, $n = count($this->_params['hostspec']); $i < $n; ++$i) { $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 (!empty($this->_params['c_threshold'])) { $this->_memcache->setCompressThreshold($this->_params['c_threshold']); } // Force consistent hashing ini_set('memcache.hash_strategy', 'consistent'); if (isset($this->_params['logger'])) { $this->_logger = $this->_params['logger']; $this->_logger->log('Connected to the following memcache servers:' . implode($this->_servers, ', '), 'DEBUG'); } }
/** * 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'); } }
/** * 加载所有的memcache 服务 */ private function _getAllMemcache() { $memcacheArray = array(); foreach ($this->_node as $key => $weight) { list($host, $port) = explode(":", $key); $_memcache_host_key = $host . '_' . $port; if (!self::$_memcache[$_memcache_host_key]) { $memcache = new Memcache(); if (!$memcache->connect($host, $port)) { self::$_memcache[$_memcache_host_key] = ''; } else { $memcache->setCompressThreshold(409600, 0.2); self::$_memcache[$_memcache_host_key] = $memcache; } } $memcacheArray[$_memcache_host_key] = self::$_memcache[$_memcache_host_key]; } return $memcacheArray; }
/** * @return MemcacheSource * @throws Exception\RuntimeException */ public function getResource() { if (!$this->resource) { throw new Exception\RuntimeException('Memcache resource must be set'); } if (!$this->resource instanceof MemcacheSource) { $resource = new MemcacheSource(); if (!$resource->addserver($this->resource['host'], $this->resource['port'], $this->resource['persistent'], $this->resource['weight'])) { throw new Exception\RuntimeException(sprintf('Cannot connect to memcache server on %s:%d', $this->resource['host'], $this->resource['port'])); } $resource->setCompressThreshold(self::DEFAULT_COMPRESSTHRESHOLD); $this->resource = $resource; } return $this->resource; }
/** * Set compress threshold on a Memcache resource * * @param MemcacheResource $resource * @param array $libOptions */ protected function setResourceAutoCompressThreshold(MemcacheResource $resource, $threshold, $minSavings) { if (!isset($threshold)) { return; } if (isset($minSavings)) { $resource->setCompressThreshold($threshold, $minSavings); } else { $resource->setCompressThreshold($threshold); } }
<?php $memcache = new Memcache(); if (!$memcache->connect('127.0.0.1', 11211)) { DebugMessage('Cannot connect to memcached!', E_USER_ERROR); } $memcache->setCompressThreshold(50 * 1024); function MCGet($key) { global $memcache; return $memcache->get('rp_' . $key); } function MCSet($key, $val, $expire = 10800) { global $memcache; return $memcache->set('rp_' . $key, $val, false, $expire); } function MCAdd($key, $val, $expire = 10800) { global $memcache; return $memcache->add('rp_' . $key, $val, false, $expire); } function MCDelete($key) { global $memcache; return $memcache->delete('rp_' . $key); }
$mc->setCompressThreshold(10000, 0.2); } } else { if (defined('DB_USE_QCACHE')) { if (!defined('DB_USE_MEMCACHE')) { define('DB_USE_MEMCACHE', false); } } else { if (!isset($config)) { $config = new Config(KB_SITE); } define('DB_USE_QCACHE', (bool) config::get('cfg_qcache')); if (!DB_USE_QCACHE && (bool) config::get('cfg_memcache')) { if (!method_exists('Memcache', 'pconnect')) { $boardMessage = "ERROR: Memcache extension not installed. memcaching disabled."; define("DB_USE_MEMCACHE", false); } else { $mc = new Memcache(); if (!@$mc->pconnect(config::get('cfg_memcache_server'), config::get('cfg_memcache_port'))) { $boardMessage = "ERROR: Unable to connect to memcached server, disabling memcached. Please check your settings (server, port) and make sure the memcached server is running"; define("DB_USE_MEMCACHE", false); } else { define("DB_USE_MEMCACHE", true); $mc->setCompressThreshold(20000, 0.2); } } } else { define("DB_USE_MEMCACHE", false); } } }