예제 #1
0
 public function __construct($prefix = '')
 {
     parent::__construct($prefix);
     if (is_null(self::$cache)) {
         self::$cache = new \Memcached();
         $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
         if (!$servers) {
             $server = \OC::$server->getSystemConfig()->getValue('memcached_server');
             if ($server) {
                 $servers = array($server);
             } else {
                 $servers = array(array('localhost', 11211));
             }
         }
         self::$cache->addServers($servers);
         $defaultOptions = [\Memcached::OPT_CONNECT_TIMEOUT => 50, \Memcached::OPT_RETRY_TIMEOUT => 50, \Memcached::OPT_SEND_TIMEOUT => 50, \Memcached::OPT_RECV_TIMEOUT => 50, \Memcached::OPT_POLL_TIMEOUT => 50, \Memcached::OPT_COMPRESSION => true, \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, \Memcached::OPT_BINARY_PROTOCOL => true];
         // by default enable igbinary serializer if available
         if (\Memcached::HAVE_IGBINARY) {
             $defaultOptions[\Memcached::OPT_SERIALIZER] = \Memcached::SERIALIZER_IGBINARY;
         }
         $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
         if (is_array($options)) {
             $options = $options + $defaultOptions;
             self::$cache->setOptions($options);
         } else {
             throw new HintException("Expected 'memcached_options' config to be an array, got {$options}");
         }
     }
 }
예제 #2
0
 public static function setUpBeforeClass()
 {
     self::$memcached = new \Memcached();
     self::$memcached->setOptions(array(\Memcached::OPT_TCP_NODELAY => true, \Memcached::OPT_NO_BLOCK => true, \Memcached::OPT_CONNECT_TIMEOUT => 1000));
     if (count(self::$memcached->getServerList()) == 0) {
         self::$memcached->addServers(array(array(MEMCACHED_HOST, MEMCACHED_PORT)));
     }
 }
예제 #3
0
파일: Memcached.php 프로젝트: itkg/core
 /**
  * Configure options
  *
  * @return void
  */
 protected function setOptions()
 {
     $options = $this->defaultOptions;
     if (array_key_exists('options', $this->config)) {
         $options = array_merge($options, $this->config['options']);
     }
     $this->connection->setOptions($options);
 }
예제 #4
0
파일: Memcached.php 프로젝트: necrogami/zf2
    /**
     * Constructor
     *
     * @param  null|array|Traversable|MemcachedOptions $options
     * @throws Exception\ExceptionInterface
     * @return void
     */
    public function __construct($options = null)
    {
        if (static::$extMemcachedMajorVersion === null) {
            $v = (string) phpversion('memcached');
            static::$extMemcachedMajorVersion = ($v !== '') ? (int)$v[0] : 0;
        }

        if (static::$extMemcachedMajorVersion < 1) {
            throw new Exception\ExtensionNotLoadedException('Need ext/memcached version >= 1.0.0');
        }

        parent::__construct($options);

        // It's ok to init the memcached instance as soon as possible because
        // ext/memcached auto-connects to the server on first use
        $this->memcached = new MemcachedResource();
        $options = $this->getOptions();

        // set lib options
        if (static::$extMemcachedMajorVersion > 1) {
            $this->memcached->setOptions($options->getLibOptions());
        } else {
            foreach ($options->getLibOptions() as $k => $v) {
                $this->memcached->setOption($k, $v);
            }
        }

        $servers = $options->getServers();
        if (!$servers) {
            $options->addServer('127.0.0.1', 11211);
            $servers = $options->getServers();
        }
        $this->memcached->addServers($servers);

        // get notified on change options
        $memc   = $this->memcached;
        $memcMV = static::$extMemcachedMajorVersion;
        $this->events()->attach('option', function ($event) use ($memc, $memcMV) {
            $params = $event->getParams();

            if (isset($params['lib_options'])) {
                if ($memcMV > 1) {
                    $memc->setOptions($params['lib_options']);
                } else {
                    foreach ($params['lib_options'] as $k => $v) {
                        $memc->setOption($k, $v);
                    }
                }
            }

            // TODO: update on change/add server(s)
        });
    }
예제 #5
0
 /**
  * Get Mamcached Handler
  *
  * @return \Memcached
  */
 public function getHandler()
 {
     if (!$this->handler) {
         $persistentId = isset($this->settings['persistent']) ? $this->settings['persistent'] : null;
         $this->handler = new \Memcached($persistentId);
         if (!$this->handler->getServerList()) {
             $this->handler->addServers($this->settings['servers']);
         }
         if (isset($this->settings['options'])) {
             $this->handler->setOptions($this->settings['options']);
         }
     }
     return $this->handler;
 }
예제 #6
0
 public function open($savePath, $sessID)
 {
     $options = [\Memcached::OPT_CONNECT_TIMEOUT => $this->timeout, \Memcached::OPT_DISTRIBUTION, \Memcached::DISTRIBUTION_CONSISTENT];
     $this->handle = new \Memcached();
     $this->handle->setOptions($options);
     if (isset($this->servers['host'])) {
         $this->handle->addServer($this->servers['host'], $this->servers['port'], isset($this->servers['weight']) ? $this->servers['weight'] : null);
     } else {
         foreach ($this->servers as $server) {
             $this->handle->addServer($server['host'], $server['port'], isset($server['weight']) ? $server['weight'] : null);
         }
     }
     return true;
 }
예제 #7
0
 /**
  * @param float $requestTimeout time in seconds
  *
  * @return PeclMemcached
  */
 public function setTimeout($requestTimeout)
 {
     $this->ensureTriedToConnect();
     $this->requestTimeout = $requestTimeout;
     $this->instance->setOptions([\Memcached::OPT_SEND_TIMEOUT => $requestTimeout * 1000, \Memcached::OPT_RECV_TIMEOUT => $requestTimeout * 1000]);
     return $this;
 }
 /**
  * @param \Memcached $memcached
  * @return $this
  */
 private function addOptions(\Memcached $memcached)
 {
     if (is_array($this->options) && !empty($this->options)) {
         foreach ($this->options as $option) {
             $memcached->setOption(constant('\\Memcached::' . $option[MemcacheConfigConstants::OPTION_NAME]), $option[MemcacheConfigConstants::OPTION_VALUE]);
         }
         $memcached->setOptions($this->options);
     }
     return $this;
 }
예제 #9
0
 /**
  * 初始化servers
  */
 private function initServers()
 {
     if (empty($this->config['servers'])) {
         $servers = [['host' => '127.0.0.1', 'port' => 11211, 'weight' => 1]];
     } else {
         $servers = $this->config['servers'];
     }
     foreach ($servers as $server) {
         $host = isset($server['host']) ? $server['host'] : '127.0.0.1';
         $port = isset($server['port']) ? $server['port'] : 11211;
         $weight = isset($server['weight']) ? $server['weight'] : 0;
         $this->servers["{$host}:{$port}"] = [$host, $port, $weight];
         $this->handler->addserver($host, $port, $weight);
     }
     if (!empty($this->config['options'])) {
         $this->handler->setOptions($this->config['options']);
     }
     $this->handler->getStats();
 }
예제 #10
0
 /**
  * @return \Memcached
  * @throws InvalidConfigException
  */
 public function getMemcached()
 {
     if (null === $this->instance) {
         if (!extension_loaded('memcached')) {
             throw new InvalidConfigException(__CLASS__ . ' requires PHP `memcached` extension to be loaded.');
         }
         $this->instance = new \Memcached($this->persistentId);
         // SASL authentication
         // @see http://php.net/manual/en/memcached.setsaslauthdata.php
         if (ini_get('memcached.use_sasl') && (null !== $this->username || null !== $this->password)) {
             if (method_exists($this->instance, 'setSaslAuthData')) {
                 $this->instance->setSaslAuthData($this->username, $this->password);
             }
         }
         if (!empty($this->options)) {
             $this->instance->setOptions($this->options);
         }
     }
     return $this->instance;
 }
예제 #11
0
 protected function getConfiguredDriver(array $config)
 {
     $memcached = new \Memcached();
     foreach ($config[Config::INDEX_SERVERS] as $server) {
         $memcached->addServer($server[Config::INDEX_HOST], $server[Config::INDEX_PORT]);
     }
     if (!empty($config[Config::INDEX_OPTIONS])) {
         $memcached->setOptions($config[Config::INDEX_OPTIONS]);
     }
     return $memcached;
 }
예제 #12
0
 /**
  * @param FrontendInterface $frontend
  * @param array $options
  */
 public function __construct($frontend, $options = array())
 {
     $this->_backend = new MemcachedDriver(self::DRIVER_PERSISTENCE_ID);
     if (!isset($options['servers'])) {
         $options['servers'] = array(array('host' => self::DEFAULT_HOST));
     }
     foreach ($options['servers'] as $server) {
         if (!array_key_exists('port', $server)) {
             $server['port'] = self::DEFAULT_PORT;
         }
         if (!array_key_exists('weight', $server)) {
             $server['weight'] = self::DEFAULT_WEIGHT;
         }
         $this->_backend->addServer($server['host'], $server['port'], $server['weight']);
     }
     if (isset($options['client']) && is_array($options['client'])) {
         $this->_backend->setOptions($options['client']);
     }
     unset($options['servers'], $options['client']);
     parent::__construct($frontend, $options);
 }
 /**
  * @param ContainerInterface $container
  * @return MemcachedSnapshotAdapter
  */
 public function __invoke(ContainerInterface $container)
 {
     $config = $container->get('config');
     $config = $this->options($config)['snapshot_adapter']['options'];
     if (isset($config['memcached_connection_alias'])) {
         $memcached = $container->get($config['memcached_connection_alias']);
     } else {
         $memcached = new \Memcached($config['persistent_id']);
         $memcached->addServers($config['servers']);
         $memcached->setOptions($config['memcached_options']);
     }
     return new MemcachedSnapshotAdapter($memcached);
 }
예제 #14
0
 /**
  * 初始化servers
  */
 private function initServers()
 {
     if (empty($this->config['servers'])) {
         $servers = [['host' => '127.0.0.1', 'port' => 11211, 'weight' => 1]];
     } else {
         $servers = $this->config['servers'];
     }
     foreach ($servers as $server) {
         $host = isset($server['host']) ? $server['host'] : '127.0.0.1';
         $port = isset($server['port']) ? $server['port'] : 11211;
         $weight = isset($server['weight']) ? $server['weight'] : 0;
         $this->handler->addserver($host, $port, $weight);
     }
     if (!empty($this->config['options'])) {
         $this->handler->setOptions($this->config['options']);
     }
     //如果获取服务器池的统计信息返回false,说明服务器池中有不可用服务器
     if ($this->handler->getStats() === false) {
         $this->isConnected = false;
     } else {
         $this->isConnected = true;
     }
 }
예제 #15
0
파일: memcached.php 프로젝트: lryl/Lysine2
 /**
  * 连接memcached
  *
  * @return \Memcached
  */
 public function connect()
 {
     if ($this->handler) {
         return $this->handler;
     }
     $servers = isset($this->config['servers']) ? $this->config['servers'] : array(array('127.0.0.1', 11211));
     $handler = new \Memcached();
     if (!$handler->addServers($servers)) {
         throw new \Lysine\Service\ConnectionException('Cannot connect memcached');
     }
     if (isset($config['options'])) {
         $handler->setOptions($config['options']);
     }
     return $this->handler = $handler;
 }
예제 #16
0
 /**
  * @param array $options
  */
 public function __construct(array $options = [])
 {
     $memcached = new \Memcached($options["persistent_id"]);
     $memcached->addServers($options["servers"]);
     //过期时间
     if (isset($options["lifetime"])) {
         $this->_lifetime = $options["lifetime"];
     }
     $options = [Memcached::OPT_LIBKETAMA_COMPATIBLE => true, Memcached::OPT_COMPRESSION => true];
     $options = array_merge($options, (array) $options["options"]);
     $memcached->setOptions($options);
     $this->_mem = $memcached;
     session_set_save_handler([$this, "open"], [$this, "close"], [$this, "read"], [$this, "write"], [$this, "destroy"], [$this, "gc"]);
     parent::__construct($options);
 }
예제 #17
0
 public function connect()
 {
     if ($this->memcached) {
         return $this->memcached;
     }
     $memcached = new \Memcached($this->getConfig('persistent_id'));
     $servers = $this->getConfig('servers') ?: [['127.0.0.1', 11211]];
     if (!$memcached->addServers($servers)) {
         throw new \Owl\Service\Exception('Memcached connect failed!');
     }
     if ($options = $this->getConfig('options')) {
         $memcached->setOptions($options);
     }
     return $this->memcached = $memcached;
 }
예제 #18
0
파일: MC.php 프로젝트: sunpaolo/slim3Demo
 private static function getInstance()
 {
     if (!empty(self::$instance)) {
         return self::$instance;
     }
     $config = Config::loadConfig('cache');
     $driver = $config['driver'];
     if (!extension_loaded($driver)) {
         throw new Exception("error driver [{$driver}]");
     }
     $servers = $config['servers'] ?: [];
     $options = $config['options'] ?: [];
     $mc = new Memcached();
     $mc->setOptions($options);
     $mc->addServers($servers);
     return self::$instance = $mc;
 }
예제 #19
0
 /**
  * @return \Memcached
  * @throws \Exception
  */
 public function getCache()
 {
     if ($this->cache == null) {
         if (!extension_loaded('memcached')) {
             throw new \Exception('Requires PHP memcached extension to be loaded');
         }
         $this->cache = $this->persistentId !== null ? new \Memcached($this->persistentId) : new \Memcached();
         if ($this->username !== null || $this->password !== null) {
             $this->cache->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
             $this->cache->setSaslAuthData($this->username, $this->password);
         }
         if (!empty($this->options)) {
             $this->cache->setOptions($this->options);
         }
     }
     return $this->cache;
 }
예제 #20
0
파일: Cache.php 프로젝트: ohworkit/Sirius
 /**
  * @param string $namespace
  * @return \Memcached
  * @throws \Exception
  */
 public static function connection($namespace = "default")
 {
     if (!isset(static::$instance[$namespace]) || !static::$instance[$namespace]) {
         $config = Config::get("cache.memcache");
         if (!isset($config[$namespace])) {
             throw new \Exception("cache config has not key {$namespace}", 1);
         }
         $config = $config[$namespace];
         $memcached = new \Memcached($config["persistent_id"]);
         $memcached->addServers($config["servers"]);
         $options = [\Memcached::OPT_LIBKETAMA_COMPATIBLE => true, \Memcached::OPT_COMPRESSION => true];
         $options = array_merge($options, (array) $config["options"]);
         $memcached->setOptions($options);
         static::$instance[$namespace] = $memcached;
     }
     return static::$instance[$namespace];
 }
 public function connect()
 {
     if ($this->_connected) {
         //throw new MemcacheException('Do not connect twice');
         return;
     }
     $m = new \Memcached($this->getHandlerKey());
     $ss = $m->getServerList();
     if (isset($this->_options[ICacher::OPT_PREFIX_KEY])) {
         $this->setOption(\Memcached::OPT_PREFIX_KEY, $this->_options[ICacher::OPT_PREFIX_KEY]);
     }
     if (empty($ss)) {
         $options = $this->getOptions();
         unset($options[ICacher::OPT_PREFIX_KEY]);
         $m->setOptions($options);
         $m->addServers(array_values($this->_servers));
     }
     $this->_connected = true;
     $this->_handler = $m;
 }
예제 #22
0
 /**
  * Create internal connection to memcached
  */
 public function _connect()
 {
     $options = $this->_options;
     if (!isset($options['servers'])) {
         throw new Exception('Servers must be an array');
     }
     if (!is_array($options['servers'])) {
         throw new Exception('Servers must be an array');
     }
     $memcache = new \Memcached();
     if (!$memcache->addServers($servers)) {
         throw new Exception('Cannot connect to Memcached server');
     }
     if (isset($options['client'])) {
         if (!is_array($options['client'])) {
             throw new Exception('Client options must be instance of array');
         }
         $memcache->setOptions($client);
     }
     $this->_memcache = $memcache;
 }
예제 #23
0
 /**
  * Tests that memcached cache store flushes entire cache when it is using a dedicated cache.
  */
 public function test_dedicated_cache()
 {
     if (!cachestore_memcached::are_requirements_met() || !defined('TEST_CACHESTORE_MEMCACHED_TESTSERVERS')) {
         $this->markTestSkipped('Could not test cachestore_memcached. Requirements are not met.');
     }
     $definition = cache_definition::load_adhoc(cache_store::MODE_APPLICATION, 'cachestore_memcached', 'phpunit_test');
     $cachestore = $this->create_test_cache_with_config($definition, array('isshared' => false));
     $connection = new Memcached(crc32(__METHOD__));
     $connection->addServers($this->get_servers(TEST_CACHESTORE_MEMCACHED_TESTSERVERS));
     $connection->setOptions(array(Memcached::OPT_COMPRESSION => true, Memcached::OPT_SERIALIZER => Memcached::SERIALIZER_PHP, Memcached::OPT_PREFIX_KEY => 'phpunit_', Memcached::OPT_BUFFER_WRITES => false));
     // We must flush first to make sure nothing is there.
     $connection->flush();
     // Test the cachestore.
     $this->assertFalse($cachestore->get('test'));
     $this->assertTrue($cachestore->set('test', 'cachestore'));
     $this->assertSame('cachestore', $cachestore->get('test'));
     // Test the connection.
     $this->assertFalse($connection->get('test'));
     $this->assertEquals(Memcached::RES_NOTFOUND, $connection->getResultCode());
     $this->assertTrue($connection->set('test', 'connection'));
     $this->assertSame('connection', $connection->get('test'));
     // Test both again and make sure the values are correct.
     $this->assertSame('cachestore', $cachestore->get('test'));
     $this->assertSame('connection', $connection->get('test'));
     // Purge the cachestore and check the connection was also purged.
     $this->assertTrue($cachestore->purge());
     $this->assertFalse($cachestore->get('test'));
     $this->assertFalse($connection->get('test'));
 }