public function __construct(array $config = null) { $config = $config ?: \Kalibri::config()->get('cache'); $this->_memcache = new \Memcache(); foreach ($config['servers'] as $server) { $this->_memcache->addserver($server['host'], $server['port'], true, isset($server['weight']) ? $server['weight'] : 1); } }
/** * Initialize memcache * * @return \Magelight\Cache\Adapter\Memcached */ public function init() { $this->memcached = new \Memcache(); foreach ((array) $this->config->xpath('servers/server') as $server) { $this->memcached->addserver((string) $server->host, (int) $server->port); } return $this; }
public function __construct() { if (!class_exists('\\Memcache')) { throw new \PHPUnit_Framework_SkippedTestError('Test cannot be ran, you need Memcache'); } $this->cache = new \Memcache(); $this->cache->addserver("localhost"); }
/** * Constructor * @param array $options cache store storage option */ public function __construct($options = array()) { $this->connect = new \Memcache(); $this->prefix = $options['prefix']; $this->default_ttl = $options['default_ttl']; $this->default_flag = isset($options['default_flag']) ? $options['default_flag'] : 0; foreach ($options['servers'] as $server) { $this->connect->addserver($server[0], $server[1]); } }
/** * {@inheritDoc} */ public function init(array $connections) { if (!class_exists("\\Memcache")) { throw new \Exception("'Memcache' class does not exist. Please install it to be able to use the Memcache connections"); } $this->memcache = new \Memcache(); foreach ($connections as $connection) { $this->memcache->addserver($connection["host"], $connection["port"]); } }
protected function addServer(array $server) { if (!array_key_exists('host', $server)) { throw new \InvalidArgumentException('host key must be set'); } $server['port'] = isset($server['port']) ? (int) $server['port'] : 11211; $server['timeout'] = isset($server['timeout']) ? (int) $server['timeout'] : 1; $server['persistent'] = isset($server['persistent']) ? (bool) $server['persistent'] : false; $server['weight'] = isset($server['weight']) ? (int) $server['weight'] : 1; $server['retry_interval'] = isset($server['retry_interval']) ? (int) $server['retry_interval'] : 15; $this->memcache->addserver($server['host'], $server['port'], $server['persistent'], $server['weight'], $server['timeout'], $server['retry_interval']); }
/** * Construct the adapter, giving an array of servers. * @example * array( * array( * 'host' => 'cache1.example.com', * 'port' => 11211, * 'weight' => 1, * 'timeout' => 60 * ), * array( * 'host' => 'cache2.example.com', * 'port' => 11211, * 'weight' => 2, * 'timeout' => 60 * ) * ) * @param array $servers */ public function __construct(array $servers) { try { $this->memcache = new \Memcache(); foreach ($servers as $server) { $this->memcache->addserver($server['host'], $server['port'], null, $server['weight'], $server['timeout']); } } catch (\Exception $e) { // @codeCoverageIgnoreStart $this->memcache = null; // @codeCoverageIgnoreEnd } }
/** * @param $cache * @param $period * @param string $cachePrefix * @throws \Exception */ public function __construct($cache, $period, $cachePrefix = 'informers') { $this->period = $period; $this->cachePrefix = $cachePrefix; if ($cache instanceof \Memcache) { $this->cache = $cache; } elseif (is_array($cache)) { $this->cache = new \Memcache(); $this->cache->addserver($cache['host'], $cache['port']); } else { throw new ClientException('Incorrect cache configuration'); } }
/** * Construct the Memcache based cache provider * @param array $objOptionsArray array of server options. Each item in the array contains an associative * arrays with options for the server to add to memcache */ public function __construct($objOptionsArray) { $this->objMemcache = new Memcache(); foreach ($objOptionsArray as $objServerOptions) { $host = $objServerOptions["host"]; $port = array_key_exists("port", $objServerOptions) ? $objServerOptions["port"] : 11211; $persistent = array_key_exists("persistent", $objServerOptions) ? $objServerOptions["persistent"] : true; $weight = array_key_exists("weight", $objServerOptions) ? $objServerOptions["weight"] : 10; $timeout = array_key_exists("timeout", $objServerOptions) ? $objServerOptions["timeout"] : 1; $retry_interval = array_key_exists("retry_interval", $objServerOptions) ? $objServerOptions["retry_interval"] : 15; $status = array_key_exists("status", $objServerOptions) ? $objServerOptions["status"] : true; $failure_callback = array_key_exists("failure_callback", $objServerOptions) ? $objServerOptions["failure_callback"] : null; $this->objMemcache->addserver($host, $port, $persistent, $weight, $timeout, $retry_interval, $status, $failure_callback); } }
/** * Конструктор кеша */ public function __construct() { //Проверяем ключи в конфиге if (!isset(Boot::getInstance()->config->memcache->host) || !isset(Boot::getInstance()->config->memcache->port)) { throw new Boot_Exception('Не указаны настройки Memcache', 500); } //Добавляем префикс if (isset(Boot::getInstance()->config->memcache->prefix)) { $this->prefix = Boot::getInstance()->config->memcache->prefix . "::"; } //Инициализируем класс кеша $this->m = new Memcache(); //Добавляем сервер $this->m->addserver(Boot::getInstance()->config->memcache->host, Boot::getInstance()->config->memcache->port); }
public function register(Application $app) { $app->setParameter('cache', ['namespace' => null, 'type' => 'array']); $app->singleton('cache', function () use($app) { $cache = null; $type = $app->getParameter('cache.type', 'array'); if ($type == 'array') { $cache = new ArrayCache(); } elseif ($type == 'apc') { $cache = new ApcCache(); } elseif ($type == 'xcache') { $cache = new XcacheCache(); } elseif ($type == 'memcache') { $cache = new MemcacheCache(); $memcache = new \Memcache(); $memcache->addserver($app->getParameter('cache.memcached.host', '127.0.0.1'), $app->getParameter('cache.memcached.port', 11211)); $cache->setMemcache($memcache); } elseif ($type == 'memcached') { $cache = new MemcachedCache(); $memcached = new \Memcached(); $memcached->addServer($app->getParameter('cache.memcached.host', '127.0.0.1'), $app->getParameter('cache.memcached.port', 11211)); $cache->setMemcached($memcached); } $cache->setNamespace($app->getParameter('cache.namespace')); return $cache; }); }
/** * 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()); } }
/** * Initializes the identifier prefix * * @return void * @throws \TYPO3\CMS\Core\Cache\Exception */ public function initializeObject() { if (empty($this->servers)) { throw new \TYPO3\CMS\Core\Cache\Exception('No servers were given to Memcache', 1213115903); } $this->memcache = new \Memcache(); $defaultPort = ini_get('memcache.default_port'); 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); } }
public function testSetInstanceSuccess() { $driver = $this->getDriver(); $client = new \Memcache(); $client->addserver('localhost'); $driver->setInstance($client); }
/** * Construct the adapter, giving an array of servers. * @example * array( * 'prefix' => '', * 'servers' => array( * array ( * 'host' => 'cache1.example.com', * 'port' => 11211, * 'weight' => 1, * 'timeout' => 60 * ), * array( * 'host' => 'cache2.example.com', * 'port' => 11211, * 'weight' => 2, * 'timeout' => 60 * ) * ) * ) * @param array $config */ public function __construct(array $config = array()) { try { if (array_key_exists('prefix', $config)) { $this->prefix = $config['prefix']; } $this->memcache = new \Memcache(); foreach ($config['servers'] as $server) { $this->memcache->addserver($server['host'], $server['port'], null, $server['weight'], $server['timeout']); } } catch (\Exception $e) { // @codeCoverageIgnoreStart $this->memcache = null; // @codeCoverageIgnoreEnd } }
/** * Create the Memcache connection * * @return void * * @since 11.1 * @throws RuntimeException */ protected function getConnection() { $config = JFactory::getConfig(); $this->_persistent = $config->get('memcache_persist', true); $this->_compress = $config->get('memcache_compress', false) == false ? 0 : MEMCACHE_COMPRESSED; // Create the memcache connection static::$_db = new Memcache(); static::$_db->addserver($config->get('memcache_server_host', 'localhost'), $config->get('memcache_server_port', 11211), $this->_persistent); $memcachetest = @static::$_db->connect($server['host'], $server['port']); if ($memcachetest == false) { throw new RuntimeException('Could not connect to memcache server', 404); } // Memcahed has no list keys, we do our own accounting, initialise key index if (static::$_db->get($this->_hash . '-index') === false) { static::$_db->set($this->_hash . '-index', array(), $this->_compress, 0); } return; }
/** * @param string $host * @param int $port * @throws BadMethodCallException * * @return \Doctrine\Common\Cache\MemcacheCache */ private static function configureMemcacheCache($host = '127.0.0.1', $port = 11211) { if (!extension_loaded('memcache')) { throw new \BadMethodCallException('MemcacheCache configured but module \'memcache\' not loaded.'); } $memcache = new \Memcache(); $memcache->addserver($host, $port); $cache = new \Doctrine\Common\Cache\MemcacheCache(); $cache->setMemcache($memcache); return $cache; }
/** * */ public function connectServer() { $server = $this->config['memcache']; if (count($server) < 1) { $server = array(array('127.0.0.1', 11211)); } foreach ($server as $s) { $name = $s[0] . "_" . $s[1]; if (!isset($this->checked[$name])) { try { if (!$this->instant->addserver($s[0], $s[1])) { $this->fallback = true; } $this->checked[$name] = 1; } catch (\Exception $e) { $this->fallback = true; } } } }
public function testMemcacheLocked() { $this->setExpectedException('Packaged\\Mutex\\Exceptions\\LockFailedException', 'Failed to lock'); $memcache = new \Memcache(); $memcache->addserver('127.0.0.1'); $provider1 = new MemcacheMutexProvider($memcache); $provider2 = new MemcacheMutexProvider($memcache); $mutex1 = Mutex::create($provider1, 'CacheLock')->lock(); $mutex2 = Mutex::create($provider2, 'CacheLock')->lock(); $this->assertTrue($mutex1->isLocked()); $this->assertFalse($mutex2->isLocked()); }
public function memcache_connect() { //memcache init if (count(Config::get('memcache_config')) > 0) { $memcache_config = Config::get('memcache_config'); $memcache = new Memcache(); foreach ($memcache_config as $key => $value) { $memcache->addserver($memcache_config[$key]['host'], $memcache_config[$key]['port'], $memcache_config[$key]['persistent'], $memcache_config[$key]['weight'], $memcache_config[$key]['timeout']); } return $memcache; } }
/** * 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. } }
protected function getResource() { if (!$this->resource) { throw new Exception\RuntimeException('Memcache resource must be set'); } if (!$this->resource instanceof MemcacheResource) { $resource = new MemcacheResource(); if (!$resource->addserver($this->resource['host'], $this->resource['port'], $this->resource['persistent'], $this->resource['weight'])) { throw new Exception\RuntimeException(sprintf('Cannot connect to beansdb server on %s:%d', $this->resource['host'], $this->resource['port'])); } $this->resource = $resource; } return $this->resource; }
/** * Internal convenience method that returns the instance of the Memcache. * * @return \Memcache * * @throws \RuntimeException */ protected function getMemcache() { if (null === $this->memcache) { if (!preg_match('#^memcache://(?(?=\\[.*\\])\\[(.*)\\]|(.*)):(.*)$#', $this->dsn, $matches)) { throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use Memcache with an invalid dsn "%s". The expected format is "memcache://[host]:port".', $this->dsn)); } $host = $matches[1] ?: $matches[2]; $port = $matches[3]; $memcache = new \Memcache(); $memcache->addserver($host, $port); $this->memcache = $memcache; } return $this->memcache; }
/** * Open memcached server connection * @param type $config * @return boolean */ public function connect($config = array()) { if (!INSTALL_DONE) { return false; } if (!empty($config)) { $this->set_config($config); } $failure_callback = function ($host, $port) { log_message('error', 'Can\'t connect to memcached server at ' . $host . ':' . $port); }; $this->is_connected = (bool) parent::addserver($this->_config['server'], $this->_config['port'], true, 1, 1, 15, true, $failure_callback); return $this->is_connected; }
/** * return memcache instance */ protected function memcache() { $arrConfig = $this->_arrConfig; if (!extension_loaded('memcache')) { throw_exception(__CLASS__ . '/' . __FUNCTION__ . ":Memcache EXTENSION NOT INSTALLED!"); return false; } $m = new Memcache(); if (count($arrConfig['machines'])) { foreach ($arrConfig['machines'] as $value) { $m->addserver($value[0], $value[1], false, $value[2]); } } return $m; }
public function register(Application $app) { // defaults (these can be overridden in the container) $app['be_cache.type'] = 'memcache'; $app['be_cache.host'] = '127.0.0.1'; $app['be_cache.port'] = '11211'; $app['be_cache.stats'] = true; $app['be_cache.prefix'] = ''; // register the client $app['be_cache.client'] = $app->share(function () use($app) { $client = null; switch (strtolower($app['be_cache.type'])) { default: throw new \Exception('Invalid be_cache.type'); break; case 'file': case 'filecache': if (!$app['be_cache.path'] || !is_writable($app['be_cache.path'])) { throw new \Exception('$app[\'be_cache.path\'] must be writable.'); } $client = new FilecacheClient($app['be_cache.path']); if ($app['be_cache.stats']) { $stats = new FilecacheStatisticsTracker($app['be_cache.path']); $client->setStatisticsTracker($stats); } break; case 'memcache': $memcache = new \Memcache(); $memcache->addserver($app['be_cache.host'], $app['be_cache.port']); $client = new MemcacheClient($memcache); break; case 'apc': $client = new APCClient(); break; } return $client; }); //register the service $app['be_cache'] = $app->share(function () use($app) { $cache = new Cache($app['be_cache.client']); $cache->setPrefix($app['be_cache.prefix']); return $cache; }); }
public function setUp() { parent::setUp(); if (!class_exists('\\Memcache')) { $this->markTestSkipped('Memcache not installed'); } $options = array('servers' => array(array('localhost', 11211)), 'expiry' => 600); // Try to connect to Memcache, skip test politely if unavailable. try { $connection = new \Memcache(); $connection->addserver($options['servers'][0][0], $options['servers'][0][1]); $success = $connection->set('test' . mt_rand(), 'foo', 1); if (!$success) { throw new \Exception("Couldn't store to Memcache"); } } catch (\Exception $ex) { $this->markTestSkipped($ex->getMessage()); } $this->object = new Memcache($options); }
/** * Constructs this backend * * @param mixed $options Configuration options - depends on the actual backend * @author Robert Lemke <*****@*****.**> */ public function __construct($options = array()) { if (!extension_loaded('memcache')) { throw new t3lib_cache_Exception('The PHP extension "memcached" must be installed and loaded in ' . 'order to use the Memcached backend.', 1213987706); } parent::__construct($options); $this->memcache = new Memcache(); $defaultPort = ini_get('memcache.default_port'); if (!count($this->servers)) { throw new t3lib_cache_Exception('No servers were given to Memcache', 1213115903); } foreach ($this->servers as $serverConfiguration) { if (substr($serverConfiguration, 0, 7) == 'unix://') { $host = $serverConfiguration; $port = 0; } else { if (substr($serverConfiguration, 0, 6) === 'tcp://') { $serverConfiguration = substr($serverConfiguration, 6); } if (strstr($serverConfiguration, ':') !== FALSE) { list($host, $port) = explode(':', $serverConfiguration, 2); } else { $host = $serverConfiguration; $port = $defaultPort; } } if ($this->serverConnected) { $this->memcache->addserver($host, $port); } else { // pconnect throws PHP warnings when it cannot connect! $this->serverConnected = @$this->memcache->pconnect($host, $port); } } if (!$this->serverConnected) { t3lib_div::sysLog('Unable to connect to any Memcached server', 'core', 3); } }
<?php /** * Passport * * @author Kezhao Pan <*****@*****.**> * @link https://github.com/42Team-itslove/passport * @license https://github.com/42Team-itslove/passport/blob/master/LICENSE * @copyright (c) 2003 - 2014 42Team */ /** * 清空Memcache全部缓存 */ echo "Local Memcached: Clear\n\n"; if (!in_array('memcache', get_loaded_extensions())) { die(' Memcache Extension No Load'); } $mem = new Memcache(); $mem->addserver('localhost', 11211); if ($mem->flush()) { echo " Success"; }
function getRealSkuAndNums($sku) { $mem = new Memcache(); $mem->addserver('192.168.200.222'); // var_dump($mem); $skuArr = array('sku' => array($sku => 1), 'isCombine' => 0); //默认为单料号 // echo "sku_info_" . $sku;exit; $skuInfo = $mem->get("sku_info_" . $sku); // print_r($skuInfo); if (empty($skuInfo)) { return false; } if (isset($skuInfo['sku']) && is_array($skuInfo['sku'])) { //如果为组合料号时 $tmpArr = array(); foreach ($skuInfo['sku'] as $key => $value) { //循环$skuInfo下的sku的键,找出所有真实料号及对应数量,$key为组合料号下对应的真实单料号,value为对应数量 $tmpArr[$key] = $value; } $skuArr['sku'] = $tmpArr; $skuArr['isCombine'] = 1; } return $skuArr; }