/**
  * Constructor.
  *
  * Available parameters are:
  *   - servers:             The list of IP:port combinations holding the memcached servers.
  *   - debug:               Whether to set the debug flag in the underlying client.
  *   - persistent:          Whether to use a persistent connection
  *   - compress_threshold:  The minimum size an object must be before it is compressed
  *   - timeout:             The read timeout in microseconds
  *   - connect_timeout:     The connect timeout in seconds
  *
  * @param array $params
  */
 function __construct($params)
 {
     parent::__construct($params);
     $params = $this->applyDefaultParams($params);
     $this->client = new MemCachedClientforWiki($params);
     $this->client->set_servers($params['servers']);
     $this->client->set_debug($params['debug']);
 }
Exemplo n.º 2
0
 /**
  * Constructor
  *
  * Available parameters are:
  *   - servers:             The list of IP:port combinations holding the memcached servers.
  *   - persistent:          Whether to use a persistent connection
  *   - compress_threshold:  The minimum size an object must be before it is compressed
  *   - timeout:             The read timeout in microseconds
  *   - connect_timeout:     The connect timeout in seconds
  *   - retry_timeout:       Time in seconds to wait before retrying a failed connect attempt
  *   - server_failure_limit:  Limit for server connect failures before it is removed
  *   - serializer:          May be either "php" or "igbinary". Igbinary produces more compact
  *                          values, but serialization is much slower unless the php.ini option
  *                          igbinary.compact_strings is off.
  *   - use_binary_protocol  Whether to enable the binary protocol (default is ASCII) (boolean)
  * @param array $params
  * @throws InvalidArgumentException
  */
 function __construct($params)
 {
     parent::__construct($params);
     $params = $this->applyDefaultParams($params);
     if ($params['persistent']) {
         // The pool ID must be unique to the server/option combination.
         // The Memcached object is essentially shared for each pool ID.
         // We can only reuse a pool ID if we keep the config consistent.
         $this->client = new Memcached(md5(serialize($params)));
         if (count($this->client->getServerList())) {
             $this->logger->debug(__METHOD__ . ": persistent Memcached object already loaded.");
             return;
             // already initialized; don't add duplicate servers
         }
     } else {
         $this->client = new Memcached();
     }
     if ($params['use_binary_protocol']) {
         $this->client->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
     }
     if (isset($params['retry_timeout'])) {
         $this->client->setOption(Memcached::OPT_RETRY_TIMEOUT, $params['retry_timeout']);
     }
     if (isset($params['server_failure_limit'])) {
         $this->client->setOption(Memcached::OPT_SERVER_FAILURE_LIMIT, $params['server_failure_limit']);
     }
     // The compression threshold is an undocumented php.ini option for some
     // reason. There's probably not much harm in setting it globally, for
     // compatibility with the settings for the PHP client.
     ini_set('memcached.compression_threshold', $params['compress_threshold']);
     // Set timeouts
     $this->client->setOption(Memcached::OPT_CONNECT_TIMEOUT, $params['connect_timeout'] * 1000);
     $this->client->setOption(Memcached::OPT_SEND_TIMEOUT, $params['timeout']);
     $this->client->setOption(Memcached::OPT_RECV_TIMEOUT, $params['timeout']);
     $this->client->setOption(Memcached::OPT_POLL_TIMEOUT, $params['timeout'] / 1000);
     // Set libketama mode since it's recommended by the documentation and
     // is as good as any. There's no way to configure libmemcached to use
     // hashes identical to the ones currently in use by the PHP client, and
     // even implementing one of the libmemcached hashes in pure PHP for
     // forwards compatibility would require MemcachedClient::get_sock() to be
     // rewritten.
     $this->client->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
     // Set the serializer
     switch ($params['serializer']) {
         case 'php':
             $this->client->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP);
             break;
         case 'igbinary':
             if (!Memcached::HAVE_IGBINARY) {
                 throw new InvalidArgumentException(__CLASS__ . ': the igbinary extension is not available ' . 'but igbinary serialization was requested.');
             }
             $this->client->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY);
             break;
         default:
             throw new InvalidArgumentException(__CLASS__ . ': invalid value for serializer parameter');
     }
     $servers = [];
     foreach ($params['servers'] as $host) {
         if (preg_match('/^\\[(.+)\\]:(\\d+)$/', $host, $m)) {
             $servers[] = [$m[1], (int) $m[2]];
             // (ip, port)
         } elseif (preg_match('/^([^:]+):(\\d+)$/', $host, $m)) {
             $servers[] = [$m[1], (int) $m[2]];
             // (ip or path, port)
         } else {
             $servers[] = [$host, false];
             // (ip or path, port)
         }
     }
     $this->client->addServers($servers);
 }