function AuthMemCookie_getMemCache() { static $memcache; /* Return now if we already have a Memcache object. */ if ($memcache) { return $memcache; } $memcache = new Memcache(); /* In the config we store memcache servers as a string of host:port-pairs * separated by ','. The port number is optional if the server runs at the * default port (11211). * Example: 'localhost:23122,remote1,10.0.0.71:43232' */ $servers = AuthMemCookie_getOption('memcache_servers', '127.0.0.1'); foreach (explode(',', $servers) as $server) { $hostport = explode(':', $server); $host = $hostport[0]; $port = (int) $hostport[1]; if (!$port) { $port = 11211; } /* Add server to pool. Sets a weight of 1 and a 10-second timeout. */ $memcache->addServer($host, $port, TRUE, 1, 10); } return $memcache; }
/** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. * * @return void * @access protected */ protected function setUp() { $memcachetest = false; include_once JPATH_BASE.'/libraries/joomla/cache/storage.php'; include_once JPATH_BASE.'/libraries/joomla/cache/storage/memcache.php'; if((extension_loaded('memcache') && class_exists('Memcache')) != true ) { $this->memcacheAvailable = false; } else { $config = JFactory::getConfig(); $host = $config->get('memcache_server_host', 'localhost'); $port = $config->get('memcache_server_port',11211); $memcache = new Memcache; $memcachetest = @$memcache->connect($host, $port); } if (!$memcachetest) { $this->memcacheAvailable = false; } else $this->memcacheAvailable = true; if ($this->memcacheAvailable) { $this->object = JCacheStorage::getInstance('memcache'); } }
protected function connect() { if (class_exists("Memcache")) { if (function_exists("getMemcached")) { if ($vc6b2148720c14fb58871f480a7cc042e = getmemcached()) { $this->memcache = $vc6b2148720c14fb58871f480a7cc042e; $this->connected = true; return true; } } $vc6b2148720c14fb58871f480a7cc042e = new Memcache(); $v3a8e4c06e471595f6eb262bb9b5582d9 = mainConfiguration::getInstance(); $v67b3dba8bc6778101892eb77249db32e = $v3a8e4c06e471595f6eb262bb9b5582d9->get('cache', 'memcache.host'); if (empty($v67b3dba8bc6778101892eb77249db32e)) { $v67b3dba8bc6778101892eb77249db32e = 'localhost'; } $v901555fb06e346cb065ceb9808dcfc25 = $v3a8e4c06e471595f6eb262bb9b5582d9->get('cache', 'memcache.port'); if (is_null($v901555fb06e346cb065ceb9808dcfc25)) { $v901555fb06e346cb065ceb9808dcfc25 = '11211'; } if ($vc6b2148720c14fb58871f480a7cc042e->connect($v67b3dba8bc6778101892eb77249db32e, $v901555fb06e346cb065ceb9808dcfc25)) { $this->memcache = $vc6b2148720c14fb58871f480a7cc042e; $this->connected = true; return true; } return false; } else { return false; } }
public function __destruct() { if ($this->instance) { $this->instance->close(); unset($this->instance); } }
public function __construct($exp = 3600) { $this->expire = $exp; if (defined('CACHE_DRIVER')) { $this->cachedriver = CACHE_DRIVER; } if ($this->cachedriver == 'memcached') { $mc = new Memcache(); if ($mc->pconnect(MEMCACHE_HOSTNAME, MEMCACHE_PORT)) { $this->memcache = $mc; $this->ismemcache = true; } } if (!$this->ismemcache) { $files = glob(DIR_CACHE . 'cache.*'); if ($files) { foreach ($files as $file) { $time = substr(strrchr($file, '.'), 1); if ($time < time()) { if (file_exists($file)) { @touch($file); @unlink($file); } } } } } }
public function defaultAction() { $memcache = new Memcache(); $memcache->flush(); $usuario = new Usuario(); $daoUsuario = DAOFactory::getUsuarioDAO(); /** @var $user User */ $user = UserService::getCurrentUser(); if (isset($user)) { $usuarioBD = $daoUsuario->queryByGoogle($user->getUserId()); if (!$usuarioBD) { // No existe el usuario $usuario->google = $user->getUserId(); $usuario->correo = $user->getEmail(); $usuario->nombre = $user->getNickname(); $daoUsuario->insert($usuario); } else { $usuario = $usuarioBD; } $_SESSION['logoutUrl'] = UserService::createLogoutUrl('/index/closeSession'); $_SESSION['usuario'] = $usuario; include 'vod/index.php'; } else { $this->login(); } }
public function indexAction() { $entries = new Entry(); // test memcached if (extension_loaded('memcache')) { $mem = new Memcache(); $mem->addServer('localhost', 11211); $result = $mem->get('indexContent'); if (!$result) { $result = $entries->fetchLatest(100); $mem->set('indexContent', $result, 0, 4); } } else { $result = $entries->fetchLatest(100); } if ($result) { $this->view->entries = $result; // Zend_Paginator $page = $this->_request->getParam('page', 1); $paginator = Zend_Paginator::factory($result); $paginator->setItemCountPerPage(4); $paginator->setCurrentPageNumber($page); $this->view->paginator = $paginator; } }
public function Run() { $memcache = new \Memcache(); $this->AssertEquals($memcache->connect('localhost', 11211), TRUE, 'connect'); $memcache->set('testkey', 'testvalue', MEMCACHE_COMPRESSED); $this->AssertEquals($memcache->get('testkey'), 'testvalue'); }
/** * Invalidate all the objects in the cache * @return void */ public function DeleteAll() { $this->objMemcache->flush(); // needs to wait one second after flush. // See comment on http://www.php.net/manual/ru/memcache.flush.php#81420 sleep(1); }
function get_cache($key) { //获取缓存 $m = new Memcache(); $m->connect(Cache_IP, Cache_Port); return $m->get($key); }
function getStatsFromCache($type = 'sizes', $slabib = null, $limit = 100) { $ini = parse_ini_file('scielo.def', true); $memcache = new Memcache(); $memcache->connect($ini['CACHE']['SERVER_CACHE'], $ini['CACHE']['SERVER_PORT_CACHE']); if ($slabib != null) { $arr = $memcache->getExtendedStats($type, $slabid, $limit); } else { $arr = $memcache->getExtendedStats($type); } $saida = ''; $arr = array_pop($arr); $saida .= '<table border="1">'; foreach ($arr as $key => $value) { $saida .= '<tr>'; $saida .= '<td>' . $key . '</td>'; if (is_array($value)) { $saida .= '<td><table border="1">'; foreach ($value as $k => $v) { $saida .= '<tr>'; $saida .= '<td>' . $k . '</td>'; $saida .= '<td>' . $v . '</td>'; $saida .= '</tr>'; } $saida .= '</table></td>'; } else { $saida .= '<td>' . $value . '</td>'; } $saida .= '</tr>'; } $saida .= '</table>'; $memcache->close(); return $saida; }
public function testSetInstanceSuccess() { $driver = $this->getDriver(); $client = new \Memcache(); $client->addserver('localhost'); $driver->setInstance($client); }
public function init() { $bootstrapOptions = $this->getBootstrap()->getOptions(); $options = $this->getOptions(); $memcache = null; $doctrineConfig = new \Doctrine\ORM\Configuration(); if (!empty($options['options']['metadataCache'])) { $metaCache = new $options['options']['metadataCache'](); if ($metaCache instanceof \Doctrine\Common\Cache\MemcacheCache) { $memcache = new Memcache(); $memcache->connect('localhost', 11211); $metaCache->setMemcache($memcache); } $doctrineConfig->setMetadataCacheImpl($metaCache); } if (!empty($options['options']['queryCache'])) { $queryCache = new $options['options']['queryCache'](); if ($queryCache instanceof \Doctrine\Common\Cache\MemcacheCache) { if (is_null($memcache)) { $memcache = new Memcache(); $memcache->connect('localhost', 11211); } $queryCache->setMemcache($memcache); } $doctrineConfig->setQueryCacheImpl($queryCache); } $driverImpl = $doctrineConfig->newDefaultAnnotationDriver(array($options['paths']['entities'])); $doctrineConfig->setMetadataDriverImpl($driverImpl); //$doctrineConfig->setEntityNamespaces( // $options['entitiesNamespaces']); $doctrineConfig->setProxyDir($options['paths']['proxies']); $doctrineConfig->setProxyNamespace($options['options']['proxiesNamespace']); $this->getBootstrap()->em = \Doctrine\ORM\EntityManager::create($options['connections']['doctrine'], $doctrineConfig); return $this->getBootstrap()->em; }
/** * @return CacheProvider */ protected function getCacheProvider() { $config = $this->getConfig()->get('orm.cache'); switch ($config['provider']) { case 'array': return new ArrayCache(); case 'filesystem': return new FilesystemCache($config[$config['provider']]['directory'], $config[$config['provider']]['extension']); case 'redis': $redis = new \Redis(); $redis->connect($config[$config['provider']]['host'], $config[$config['provider']]['port']); $redis->select($config[$config['provider']]['dbindex']); $cache = new RedisCache(); $cache->setRedis($redis); return $cache; case 'memcached': $memcache = new \Memcache(); $memcache->connect($config[$config['provider']]['host'], $config[$config['provider']]['port'], $config[$config['provider']]['weight']); $cache = new MemcacheCache(); $cache->setMemcache($memcache); return $cache; default: return null; } }
function elggcache_cacheinit() { global $CFG, $messages; //memcache_debug(true); if (!empty($CFG->elggcache_enabled)) { static $memcacheconn; //var_dump($memcacheconn); if (!isset($memcacheconn)) { if (!empty($CFG->elggcache_debug)) { $messages[] = 'connecting to memcache<br />'; } $memcacheconn = new Memcache(); $memcacheconn->pconnect($CFG->elggcache_memcache_host, $CFG->elggcache_memcache_port); } //var_dump($memcacheconn); if (empty($memcacheconn) || !is_resource($memcacheconn->connection)) { $CFG->elggcache_enabled = false; if (!empty($CFG->elggcache_debug)) { $messages[] = 'failed connect to memcache<br />'; } } } else { $memcacheconn = false; } //$stats = $memcacheconn->getExtendedStats(); //var_dump($stats); //var_dump($memcacheconn); return $memcacheconn; }
/** * Register the service provider. * * @return void */ public function register() { $this->package('opensolutions/doctrine2bridge', 'doctrine2bridge'); $this->app['config']->package('opensolutions/doctrine2bridge', __DIR__ . '/../config'); $this->app->singleton('d2cachebridge', function ($app) { $cacheClass = "\\Doctrine\\Common\\Cache\\" . \Config::get('doctrine2bridge::cache.type'); if (!class_exists($cacheClass)) { throw new Exception\ImplementationNotFound($cacheClass); } $cache = new $cacheClass(); if (\Config::has('doctrine2bridge::cache.namespace')) { $cache->setNamespace(\Config::get('doctrine2bridge::cache.namespace')); } switch (\Config::get('doctrine2bridge::cache.type')) { case 'MemcacheCache': $memcache = new \Memcache(); if (!\Config::has('doctrine2bridge::cache.memcache.servers') || !count(\Config::get('doctrine2bridge::cache.memcache.servers'))) { throw new Exception\Configuration('No servers defined for Doctrine2Bridge\\Doctrine2CacheBridgeServiceProvider - Memcache'); } foreach (\Config::get('doctrine2bridge::cache.memcache.servers') as $server) { $memcache->addServer($server['host'], isset($server['port']) ? $server['port'] : 11211, isset($server['persistent']) ? $server['persistent'] : false, isset($server['weight']) ? $server['weight'] : 1, isset($server['timeout']) ? $server['timeout'] : 1, isset($server['retry_int']) ? $server['retry_int'] : 15); $cache->setMemcache($memcache); } break; } return $cache; }); // Shortcut so developers don't need to add an Alias in app/config/app.php \App::booting(function () { $loader = \Illuminate\Foundation\AliasLoader::getInstance(); $loader->alias('D2Cache', 'Doctrine2Bridge\\Support\\Facades\\Doctrine2Cache'); }); }
function getMemcacheKeys() { $memcache = new Memcache(); $memcache->connect('127.0.0.1', 11211) or die("Could not connect to memcache server"); $list = array(); $allSlabs = $memcache->getExtendedStats('slabs'); $items = $memcache->getExtendedStats('items'); foreach ($allSlabs as $server => $slabs) { foreach ($slabs as $slabId => $slabMeta) { if (!is_numeric($slabId)) { continue; } $cdump = $memcache->getExtendedStats('cachedump', (int) $slabId); asort($cdump); foreach ($cdump as $keys => $arrVal) { if (!is_array($arrVal)) { continue; } foreach ($arrVal as $k => $v) { if (!$_GET['key'] || strpos($k, $_GET['key']) !== false) { print "<tr><td>" . date('H:i:s d.m.Y', $v[1] + 108955) . "</td><td><a href='?key=" . $k . "'>" . $k . "</a></td><td>" . ($v[1] - time()) . "</td></tr>\n"; } } } } } }
public static function factory(Configuration $conf) { switch (strtolower($conf['session-server']['type'])) { case 'memcache': $memcache = new \Memcache(); if (!@$memcache->connect($conf['session-server']['host'], $conf['session-server']['port'])) { throw new RuntimeException(sprintf('Unable to connect to memcache server at %s:%s', $conf['session-server']['host'], $conf['session-server']['port'])); } return new MemcacheSessionHandler($memcache); break; case 'memcached': $memcached = new \Memcached(); if (!@$memcached->addServer($conf['session-server']['host'], $conf['session-server']['port'])) { throw new RuntimeException(sprintf('Unable to connect to memcached server at %s:%s', $conf['session-server']['host'], $conf['session-server']['port'])); } $memcached->getVersion(); if (\Memcached::RES_SUCCESS !== $memcached->getResultCode()) { throw new RuntimeException(sprintf('Unable to connect to memcached server at %s:%s', $conf['session-server']['host'], $conf['session-server']['port'])); } return new MemcachedSessionHandler($memcached); break; case 'mongo': case 'pdo': throw new RuntimeException(sprintf('Session handler %s is not yet supported', $conf['session-server']['type'])); break; default: throw new RuntimeException(sprintf('Session handler %s is not a valid type', $conf['session-server']['type'])); break; } }
/** * Singleton method * @static * @param string $type * @return Zend_Cache */ public static function factory($type = 'File', $options = array()) { if (!self::$cache_object) { $front_opt = array('automatic_serialization' => true); if (self::$life_time) { $front_opt['lifeTime'] = self::$life_time; } switch ($type) { case 'Memcached': $mhost = $options['memcache_host']; $mport = $options['memcache_port']; $memcache = new Memcache(); if (!@$memcache->connect($mhost, $mport)) { require_once 'Hush/Cache/Exception.php'; throw new Hush_Cache_Exception('Can not connect to memcache server'); } $back_opt = array('servers' => array('host' => $mhost, 'port' => $mport)); break; default: if (!is_dir($options['cache_dir'])) { if (realpath(__DAT_DIR)) { mkdir(realpath(__DAT_DIR) . '/cache', 0600); $options['cache_dir'] = realpath(__DAT_DIR . '/cache'); } else { require_once 'Hush/Cache/Exception.php'; throw new Hush_Cache_Exception('Can not found cache_dir file directory'); } } $back_opt = array('cache_dir' => $options['cache_dir']); break; } self::$cache_object = Zend_Cache::factory('Core', $type, $front_opt, $back_opt); } return self::$cache_object; }
function initMemCache() { global $memcache; $memcache = new Memcache(); $memcache->connect('localhost', 11211) or die('Could not connect'); // memcache_flush($memcache); }
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; }); }
public function __construct(array $values = array()) { parent::__construct($values); $app = $this; $app->register(new UrlGeneratorServiceProvider()); $app->register(new SessionServiceProvider()); if (true === $app['debug']) { $app->register(new WhoopsServiceProvider()); } $app['session.storage.handler'] = $app->share(function ($app) { $memcache = new \Memcache(); $memcache->connect('localhost', 11211); return new MemcacheSessionHandler($memcache); }); $app['facebook'] = $app->share(function () use($app, $values) { return new \Facebook(['appId' => $values['facebook.app_id'], 'secret' => $values['facebook.secret'], 'allowSignedRequest' => false]); }); $app['user'] = function () use($app) { return $app['session']->get('user'); }; $app->before(function ($request) use($app) { $user = $app['user']; if (null === $user) { $facebook = $app['facebook']; $result = $facebook->api(array('method' => 'fql.query', 'query' => 'SELECT uid, name, pic_square, profile_url FROM user WHERE uid = me()')); if (!empty($result)) { $app['session']->set('user', $result[0]); return; } return $app->render('login.phtml', ['loginUrl' => $facebook->getLoginUrl()]); } }); }
public function testIfCacheInitsWithOpenConnection() { $raw = new \Memcache(); $raw->connect(MEMCACHE_HOST, MEMCACHE_PORT, MEMCACHE_TIMEOUT_SECONDS); $this->cache = new MemcacheCache($raw); $this->assertKeysCanBeWrittenAndRead(); }
public function __invoke($app) { $memcacheConfig = $app['config']->get('memcache'); $memcache = new \Memcache(); $memcache->connect($memcacheConfig['host'], $memcacheConfig['port']); return $memcache; }
function __construct($host, $port = 11211) { $mc = new Memcache(); $mc->addServer($host, intval($port)); $mc->setCompressThreshold(self::CompressThreshold); $this->_memcache = $mc; }
/** * 获得链接对象 */ public static function getLink($link) { if (!isset(self::$_ipArr[$link])) { $link = 'Default'; } if (!isset(self::$_memObjs[$link])) { $isMemcached = false; #表明是memcache的算法 if (isset(self::$_ipArr[$link]['memcached']) && self::$_ipArr[$link]['memcached']) { try { $mem = new Memcached(); $isMemcached = true; } catch (Exception $e) { $mem = new Memcache(); } } else { $mem = new Memcache(); } foreach (self::$_ipArr[$link]['ip'] as $value) { $mem->addServer($value, 11211, false); } self::$_memObjs[$link] = array('memcached' => $isMemcached, 'obj' => $mem); } return self::$_memObjs[$link]; }
function getKey() { $mem = new Memcache(); $mem->connect("127.0.0.1", 11211); echo $mem->get("key"); exit; }
public function handler() { if (!empty($this->storage)) { $handler = $this->handler; } else { $config = $this->_getConfig()->read(); $handlerName = $config['handler']; $handlerConfig = isset($config['config']) ? $config['config'] : array(); switch ($handlerName) { case 'memcache': if (!isset($handlerConfig['host']) || !isset($handlerConfig['port'])) { throw new RuntimeException('You must specify memcache host and port in handler config in "' . self::CONFIG_FILE . '"'); } $memcache = new \Memcache(); $memcache->addServer($handlerConfig['host'], (int) $handlerConfig['port']); $handler = new MemcacheSessionHandler($memcache); break; case 'memcached': if (!isset($handlerConfig['host']) || !isset($handlerConfig['port'])) { throw new RuntimeException('You must specify memcache host and port in handler config in "' . self::CONFIG_FILE . '"'); } $memcached = new \Memcached(); $memcached->addServer($handlerConfig['host'], (int) $handlerConfig['port']); $handler = new MemcachedSessionHandler($memcached); break; case 'native': default: $handler = new NativeSessionHandler(); } } return $handler; }
/** * @return bool false on error */ protected function reconnect() { $this->memcache = null; if ($this->connectAttempts >= self::MAX_CONNECT_ATTEMPTS) { return false; } $connectResult = false; $connStart = microtime(true); while ($this->connectAttempts < self::MAX_CONNECT_ATTEMPTS) { $this->connectAttempts++; $memcache = new Memcache(); //$memcache->setOption(Memcached::OPT_BINARY_PROTOCOL, true); // TODO: enable when moving to memcached v1.3 $curConnStart = microtime(true); $connectResult = @$memcache->connect($this->hostName, $this->port); if ($connectResult || microtime(true) - $curConnStart < 0.5) { // retry only if there's an error and it's a timeout error break; } self::safeLog("got timeout error while connecting to memcache..."); } self::safeLog("connect took - " . (microtime(true) - $connStart) . " seconds to {$this->hostName}:{$this->port} attempts {$this->connectAttempts}"); if (!$connectResult) { self::safeLog("failed to connect to memcache"); return false; } $this->memcache = $memcache; return true; }
protected function setUp() { parent::setUp(); if (\extension_loaded('memcache') && @fsockopen('localhost', 11211)) { $memcache = new \Memcache(); $memcache->addServer('localhost'); $memcache->flush(); $cacheDriver = new \Doctrine\Common\Cache\MemcacheCache(); $cacheDriver->setMemcache($memcache); $this->_em->getMetadataFactory()->setCacheDriver($cacheDriver); } else { if (\extension_loaded('apc')) { $this->_em->getMetadataFactory()->setCacheDriver(new \Doctrine\Common\Cache\ApcCache()); } else { $this->markTestSkipped('Test only works with a cache enabled.'); } } try { $this->_schemaTool->createSchema(array($this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC742User'), $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC742Comment'))); } catch (\Exception $e) { } // make sure classes will be deserialized from caches $this->_em->getMetadataFactory()->setMetadataFor(__NAMESPACE__ . '\\DDC742User', null); $this->_em->getMetadataFactory()->setMetadataFor(__NAMESPACE__ . '\\DDC742Comment', null); }