Ejemplo n.º 1
0
 /**
  * Initialise memcached storage cache.
  *
  * @param array $options optional config array, valid keys are: host, port
  */
 public function __construct(array $options = array())
 {
     $this->options = $options + $this->options;
     $this->memcached = new \Memcached();
     $this->memcached->addServer($this->options['host'], $this->options['port'], 0);
     $this->flags = $this->options['compressed'] ? MEMCACHED_COMPRESSED : 0;
 }
 public function __construct($persistent = true)
 {
     /*
      * Caching can be disabled in the config file if you aren't able to run
      * a memcached instance.  In that case this class will still get used,
      * but we will pretend that we have a perpetually empty cache.
      */
     if (defined("USE_MEMCACHED") && USE_MEMCACHED != false) {
         $servers = null;
         if ($persistent === true) {
             /*
              * Since prefix should be unique across RTK installations, we can
              * use the server hostname + port + prefix as a persistent ID.
              * Hostname + port are included because otherwise changing the
              * memcached server would NOT change the persistent connection,
              * and you'd end up using the old server.
              */
             $persistentID = MEMCACHED_HOST . ":" . MEMCACHED_PORT . "_" . MEMCACHED_PREFIX;
             $this->cache = new Memcached($persistentID);
             $serverList = $this->cache->getServerList();
             foreach ($serverList as $entry) {
                 $servers[] = $entry['host'] . ":" . $entry['port'];
             }
         } else {
             //Not running Memcached in persistent mode.
             $this->cache = new Memcached();
         }
         if ($persistent !== true || !is_array($servers) || !in_array(MEMCACHED_HOST . ":" . MEMCACHED_PORT, $servers)) {
             $this->cache->addServer(MEMCACHED_HOST, MEMCACHED_PORT);
         }
     }
 }
 public static function setUpBeforeClass()
 {
     static::$Memcached = new \Memcached();
     // MEMCACHED_TEST_SERVER defined in phpunit.xml
     $server = explode(':', MEMCACHED_TEST_SERVER);
     static::$Memcached->addServer($server[0], $server[1]);
 }
Ejemplo n.º 4
0
 /**
  * Constructor
  *
  * @access public
  *
  * @param array $config config array
  *
  * @result void
  * @throws Exception
  */
 public function __construct(array $config = [])
 {
     parent::__construct($config);
     if (empty($config['type']) || !$this->check()) {
         throw new Exception('Memcache(d) not installed or not select type');
     }
     switch (strtolower($config['type'])) {
         case 'memcached':
             $this->driver = new \Memcached();
             break;
         case 'memcache':
             $this->driver = new \Memcache();
             break;
         default:
             throw new Exception('Selected type not valid in the driver');
     }
     if (!empty($config['servers'])) {
         $this->driver->addServers($config['servers']);
     } elseif ($config['server']) {
         $conf = $config['server'];
         $server = ['hostname' => !empty($conf['hostname']) ? $conf['hostname'] : '127.0.0.1', 'port' => !empty($conf['port']) ? $conf['port'] : 11211, 'weight' => !empty($conf['weight']) ? $conf['weight'] : 1];
         if (get_class($this->driver) === 'Memcached') {
             $this->driver->addServer($server['hostname'], $server['port'], $server['weight']);
         } else {
             $this->driver->addServer($server['hostname'], $server['port'], true, $server['weight']);
         }
     } else {
         throw new Exception('Server(s) not configured');
     }
 }
 public function __construct(array $aConfig = [])
 {
     // Parent construct
     parent::__construct($aConfig);
     // Extend config
     $aConfig = ExtendedArray::extendWithDefaultValues($aConfig, ['connection_timeout' => 10, 'server_failure_limit' => 5, 'remove_failed_servers' => true, 'retry_timeout' => 1]);
     // Check Memcached is loaded
     if (!extension_loaded('memcached')) {
         throw new RuntimeException('Memcached extension is not loaded');
     }
     // Create client
     $this->oClient = new Memcached();
     $this->oClient->setOption(Memcached::OPT_CONNECT_TIMEOUT, $aConfig['connection_timeout']);
     $this->oClient->setOption(Memcached::OPT_DISTRIBUTION, Memcached::DISTRIBUTION_CONSISTENT);
     $this->oClient->setOption(Memcached::OPT_SERVER_FAILURE_LIMIT, $aConfig['server_failure_limit']);
     $this->oClient->setOption(Memcached::OPT_REMOVE_FAILED_SERVERS, $aConfig['remove_failed_servers']);
     $this->oClient->setOption(Memcached::OPT_RETRY_TIMEOUT, $aConfig['retry_timeout']);
     // Add servers
     if (array_key_exists('servers', $aConfig)) {
         $aServers = array_filter(explode(',', $aConfig['servers']));
         foreach ($aServers as $sAddress) {
             // Parse address
             list($sHost, $sPort) = array_pad(explode(':', $sAddress), 2, '');
             // Add server
             $this->oClient->addServer($sHost, intval($sPort));
         }
     }
 }
Ejemplo n.º 6
0
 public function __construct()
 {
     $this->mem = new Memcached(static::$servers_id);
     if (count($this->mem->getServerList()) == 0) {
         $this->mem->addServer("", 11211);
     }
 }
Ejemplo n.º 7
0
 private function setupMemcache()
 {
     if (is_null($this->memcache) && class_exists('Memcached', false)) {
         $this->memcache = new \Memcached();
         $this->memcache->addServer($this->host, $this->port);
     }
 }
 public function addServer($host = 'localhost', $port = 11211)
 {
     if ($this->memcached->addServer($host, $port, 1) === FALSE) {
         $error = error_get_last();
         throw new Nette\InvalidStateException("Memcached::addServer(): {$error['message']}.");
     }
 }
Ejemplo n.º 9
0
 /**
  * Constructor
  *
  * @param string $uniqId to be used to separate objects in addition to their key identifiers, for instance in a
  * multi-user scenario.
  *
  * @return \iveeCrest\MemcachedWrapper
  */
 public function __construct($uniqId)
 {
     $this->memcached = new \Memcached();
     $this->memcached->addServer(Config::getCacheHost(), Config::getCachePort());
     $this->memcached->setOption(\Memcached::OPT_PREFIX_KEY, Config::getCachePrefix());
     $this->uniqId = $uniqId;
 }
Ejemplo n.º 10
0
 /**
  * Creamos y configuramos el driver para la cache.
  * @param string $hostname Nombre del host para conectar al servidor memcached.
  * @param int $port Puerto para conectar al servidor de memcached.
  * @param int $weight Importancia del servidor frente al resto.
  */
 public function __construct($hostname = '127.0.0.1', $port = 11211, $weight = 1)
 {
     // Instanciamos memcached.
     $this->_memcached = new Memcached();
     // Configuramos el servidor.
     $this->_memcached->addServer($hostname, $port, $weight);
 }
Ejemplo n.º 11
0
 protected function init()
 {
     $this->config['host'] = $this->config['host'] ?: 'localhost';
     $this->config['port'] = $this->config['port'] ?: 11211;
     $this->store = new Memcached();
     $this->store->addServer($this->config['host'], $this->config['port'], 100);
 }
Ejemplo n.º 12
0
 /**
  * @return bool
  */
 public function connectServer()
 {
     if ($this->checkdriver() == false) {
         return false;
     }
     $s = $this->config['memcache'];
     if (count($s) < 1) {
         $s = array(array('127.0.0.1', 11211, 100));
     }
     foreach ($s as $server) {
         $name = isset($server[0]) ? $server[0] : '127.0.0.1';
         $port = isset($server[1]) ? $server[1] : 11211;
         $sharing = isset($server[2]) ? $server[2] : 0;
         $checked = $name . '_' . $port;
         if (!isset($this->checked[$checked])) {
             try {
                 if ($sharing > 0) {
                     if (!$this->instant->addServer($name, $port, $sharing)) {
                         $this->fallback = true;
                     }
                 } else {
                     if (!$this->instant->addServer($name, $port)) {
                         $this->fallback = true;
                     }
                 }
                 $this->checked[$checked] = 1;
             } catch (\Exception $e) {
                 $this->fallback = true;
             }
         }
     }
 }
Ejemplo n.º 13
0
 /**
  * Connect and initialize this handler.
  *
  * @return boolean True if successful, false on failure
  */
 function connect()
 {
     global $mybb, $error_handler;
     $this->memcached = new Memcached();
     if ($mybb->config['memcache']['host']) {
         $mybb->config['memcache'][0] = $mybb->config['memcache'];
         unset($mybb->config['memcache']['host']);
         unset($mybb->config['memcache']['port']);
     }
     foreach ($mybb->config['memcache'] as $memcached) {
         if (!$memcached['host']) {
             $message = "Please configure the memcache settings in inc/config.php before attempting to use this cache handler";
             $error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
             die;
         }
         if (!isset($memcached['port'])) {
             $memcached['port'] = "11211";
         }
         $this->memcached->addServer($memcached['host'], $memcached['port']);
         if (!$this->memcached) {
             $message = "Unable to connect to the memcached server on {$memcached['memcache_host']}:{$memcached['memcache_port']}. Are you sure it is running?";
             $error_handler->trigger($message, MYBB_CACHEHANDLER_LOAD_ERROR);
             die;
         }
     }
     // Set a unique identifier for all queries in case other forums are using the same memcache server
     $this->unique_id = md5(MYBB_ROOT);
     return true;
 }
Ejemplo n.º 14
0
 /**
  * Only connect at the last possible moment. This enables you to get the cache provider, but not connect
  * until you have business with the cache.
  */
 private function connect()
 {
     if ($this->isConnected) {
         return;
     }
     $this->memcache->addServer($this->configuration->getHost(), $this->configuration->getPort());
 }
 /**
  * Constructor
  *
  * @param array[] $servers server array
  */
 public function __construct($servers)
 {
     if (!$servers || !is_array($servers) || count($servers) < 1) {
         throw new GitPHP_MessageException('No Memcache servers defined', true, 500);
     }
     if (class_exists('Memcached')) {
         $this->memcacheObj = new Memcached();
         $this->memcacheType = GitPHP_CacheResource_Memcache::Memcached;
         $this->memcacheObj->addServers($servers);
     } else {
         if (class_exists('Memcache')) {
             $this->memcacheObj = new Memcache();
             $this->memcacheType = GitPHP_CacheResource_Memcache::Memcache;
             foreach ($servers as $server) {
                 if (is_array($server)) {
                     $host = $server[0];
                     $port = 11211;
                     if (isset($server[1])) {
                         $port = $server[1];
                     }
                     $weight = 1;
                     if (isset($server[2])) {
                         $weight = $server[2];
                     }
                     $this->memcacheObj->addServer($host, $port, true, $weight);
                 }
             }
         } else {
             throw new GitPHP_MissingMemcacheException();
         }
     }
     $this->servers = $servers;
 }
Ejemplo n.º 16
0
 /**
  * Create the Memcached connection
  *
  * @return  void
  *
  * @since   12.1
  * @throws  RuntimeException
  */
 protected function getConnection()
 {
     if (!static::isSupported()) {
         throw new RuntimeException('Memcached Extension is not available');
     }
     $config = JFactory::getConfig();
     $host = $config->get('memcached_server_host', 'localhost');
     $port = $config->get('memcached_server_port', 11211);
     // Create the memcached connection
     if ($config->get('memcached_persist', true)) {
         static::$_db = new Memcached($this->_hash);
         $servers = static::$_db->getServerList();
         if ($servers && ($servers[0]['host'] != $host || $servers[0]['port'] != $port)) {
             static::$_db->resetServerList();
             $servers = array();
         }
         if (!$servers) {
             static::$_db->addServer($host, $port);
         }
     } else {
         static::$_db = new Memcached();
         static::$_db->addServer($host, $port);
     }
     static::$_db->setOption(Memcached::OPT_COMPRESSION, $this->_compress);
     $stats = static::$_db->getStats();
     $result = !empty($stats["{$host}:{$port}"]) && $stats["{$host}:{$port}"]['pid'] > 0;
     if (!$result) {
         // Null out the connection to inform the constructor it will need to attempt to connect if this class is instantiated again
         static::$_db = null;
         throw new JCacheExceptionConnecting('Could not connect to memcached server');
     }
 }
Ejemplo n.º 17
0
 /**
  * Return memcached connection object
  *
  * @return  object   memcached connection object
  *
  * @since   12.1
  * @throws  RuntimeException
  */
 protected function getConnection()
 {
     if ((extension_loaded('memcached') && class_exists('Memcached')) != true) {
         return false;
     }
     $config = JFactory::getConfig();
     $this->_persistent = $config->get('memcache_persist', true);
     $this->_compress = $config->get('memcache_compress', false) == false ? 0 : Memcached::OPT_COMPRESSION;
     /*
      * This will be an array of loveliness
      * @todo: multiple servers
      * $servers	= (isset($params['servers'])) ? $params['servers'] : array();
      */
     $server = array();
     $server['host'] = $config->get('memcache_server_host', 'localhost');
     $server['port'] = $config->get('memcache_server_port', 11211);
     // Create the memcache connection
     if ($this->_persistent) {
         $session = JFactory::getSession();
         self::$_db = new Memcached($session->getId());
     } else {
         self::$_db = new Memcached();
     }
     $memcachedtest = self::$_db->addServer($server['host'], $server['port']);
     if ($memcachedtest == false) {
         throw new RuntimeException('Could not connect to memcached server', 404);
     }
     self::$_db->setOption(Memcached::OPT_COMPRESSION, $this->_compress);
     // Memcached has no list keys, we do our own accounting, initialise key index
     if (self::$_db->get($this->_hash . '-index') === false) {
         $empty = array();
         self::$_db->set($this->_hash . '-index', $empty, 0);
     }
     return;
 }
Ejemplo n.º 18
0
 public static function getMemcache()
 {
     if (!isset(self::$mc)) {
         self::$mc = new Memcached();
         self::$mc->addServer("localhost", 11211);
     }
     return self::$mc;
 }
Ejemplo n.º 19
0
 /**
  * {@inheritdoc}
  */
 public function connect()
 {
     foreach ($this->servers as $server) {
         //Merging default options
         $server = $server + $this->defaultServer;
         $this->driver->addServer($server['host'], $server['port'], $server['weight']);
     }
 }
Ejemplo n.º 20
0
 /**
  * Get Memcached instance.
  *
  * @return Memcached
  */
 public function getMemcached()
 {
     if (!isset($this->memcached)) {
         $this->memcached = new Memcached();
         $this->memcached->addServer($this->options['host'], $this->options['port']);
     }
     return $this->memcached;
 }
Ejemplo n.º 21
0
 /**
  * @return \Memcached
  */
 public function getInstance()
 {
     if (is_null($this->instance)) {
         $this->instance = new \Memcached();
         $this->instance->addServer("localhost", 11211);
         return $this->instance;
     }
     return $this->instance;
 }
Ejemplo n.º 22
0
 /**
  * @param BaseMemcached|null $server
  */
 public function __construct(BaseMemcached $server = null)
 {
     if ($server) {
         $this->server = $server;
         return;
     }
     $this->server = new BaseMemcached();
     $this->server->addServer('localhost', 11211);
 }
Ejemplo n.º 23
0
 public function setConfig(array $config)
 {
     $default = ['host' => '127.0.0.1', 'port' => 11211];
     $config = array_merge($default, $config);
     $this->connected = $this->handler->addServer($config['host'], $config['port']);
     if (!$this->connected) {
         throw new \RuntimeException('Connect Memcache Server Failed!');
     }
 }
 /**
  * Construct a new DocumentIndex
  * @param String $host Host setting for Memcached service
  * @param Integer $port Port setting for Memcached service
  */
 public function __construct($host = 'localhost', $port = 11211)
 {
     $this->index = new \Memcached();
     $this->index->addServer($host, $port);
     $this->size = $this->index->get('index_size');
     if ($this->size === false) {
         $this->size = 0;
     }
 }
Ejemplo n.º 25
0
 /**
  * Constructs the memcached object
  *
  * @param   array     configuration
  * @throws  Kohana_Cache_Exception
  */
 public function __construct(array $config)
 {
     // Check that memcached is loaded
     if (!extension_loaded('memcached')) {
         throw new Kohana_Cache_Exception('Memcached extension is not loaded');
     }
     parent::__construct($config);
     // Check whether this is a persistent connection
     if ($config['persistent'] == FALSE) {
         // Setup a non-persistent memcached connection
         $this->_memcached = new Memcached();
     } else {
         // Setup a persistent memcached connection
         $this->_memcached = new Memcached($this->_config['persistent_id']);
     }
     // Load servers from configuration
     $servers = Arr::get($this->_config, 'servers', NULL);
     if (!$servers) {
         // Throw exception if no servers found in configuration
         throw new Kohana_Cache_Exception('No Memcache servers defined in configuration');
     }
     // Add memcache servers
     foreach ($servers as $server) {
         if (!$this->_memcached->addServer($server['host'], $server['port'], $server['weight'])) {
             throw new Kohana_Cache_Exception('Could not connect to memcache host at \':host\' using port \':port\'', array(':host' => $server['host'], ':port' => $server['port']));
         }
     }
     // Load memcached options from configuration
     $options = Arr::get($this->_config, 'options', NULL);
     // Make sure there are options to set
     if ($options != NULL) {
         // Set the options
         foreach ($options as $key => $value) {
             // Special cases for a few options
             switch ($key) {
                 case 'serializer':
                     $value = $this->_serializer_map[$value];
                     break;
                 case 'hash':
                     $value = $this->_hash_map[$value];
                     break;
                 case 'distribution':
                     $value = $this->_distribution_map[$value];
                     break;
                 case 'prefix_key':
                     // Throw exception is key prefix is greater than 128 characters
                     if (strlen($value) > 128) {
                         throw new Kohana_Cache_Exception('Memcached prefix key cannot exceed 128 characters');
                     }
                     break;
                 default:
                     break;
             }
             $this->_memcached->setOption($this->_options_map[$key], $value);
         }
     }
 }
Ejemplo n.º 26
0
 public function __construct($prefix = '')
 {
     parent::__construct($prefix);
     if (is_null(self::$cache)) {
         self::$cache = new \Memcached();
         list($host, $port) = \OC_Config::getValue('memcached_server', array('localhost', 11211));
         self::$cache->addServer($host, $port);
     }
 }
Ejemplo n.º 27
0
 /**
  * @throws \RuntimeException
  */
 private function connect()
 {
     if (empty($this->servers)) {
         throw new \RuntimeException('Missing memcached server/s configuration');
     }
     $this->memcached = new \Memcached();
     foreach ($this->servers as $serverConfiguration) {
         $this->memcached->addServer($serverConfiguration->getHost(), $serverConfiguration->getPort(), $serverConfiguration->getWeight());
     }
 }
Ejemplo n.º 28
0
 /**
  * (non-PHPdoc)
  * @see \parallely\AbstractTransport::_prepare()
  */
 protected function _prepare()
 {
     if (empty($this->_oMemcache) === true) {
         $this->_oMemcache = new \Memcached();
         if ($this->_oMemcache->addServer($this->_aOptions['host'], $this->_aOptions['port']) !== true) {
             throw new \parallely\Exception(\parallely\Exception::SETUP_ERROR);
         }
     }
     return $this;
 }
Ejemplo n.º 29
0
 public function setUp()
 {
     parent::setUp();
     $this->memcache = new \Memcached();
     $this->memcache->addServer(self::TEST_MEMCACHE_SERVER, self::TEST_MEMCACHE_PORT);
     $this->memcache->flush();
     if (!$this->memcache->set('test', 1, time() + 100)) {
         throw new \RuntimeException('Cannot save item to memcache. ' . $this->memcache->getResultMessage());
     }
 }
 /**
  * @param \Memcached|null $memcached
  */
 public function setMemcached($memcached = null)
 {
     if ($memcached) {
         $this->memcached = $memcached;
     }
     if ($this->memcached) {
         $this->memcached->resetServerList();
         $this->memcached->addServer($this->server, $this->port);
     }
 }