[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/objectcache/ -> MemcachedPeclBagOStuff.php (source)

   1  <?php
   2  /**
   3   * Object caching using memcached.
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @file
  21   * @ingroup Cache
  22   */
  23  
  24  /**
  25   * A wrapper class for the PECL memcached client
  26   *
  27   * @ingroup Cache
  28   */
  29  class MemcachedPeclBagOStuff extends MemcachedBagOStuff {
  30  
  31      /**
  32       * Constructor
  33       *
  34       * Available parameters are:
  35       *   - servers:             The list of IP:port combinations holding the memcached servers.
  36       *   - persistent:          Whether to use a persistent connection
  37       *   - compress_threshold:  The minimum size an object must be before it is compressed
  38       *   - timeout:             The read timeout in microseconds
  39       *   - connect_timeout:     The connect timeout in seconds
  40       *   - retry_timeout:       Time in seconds to wait before retrying a failed connect attempt
  41       *   - server_failure_limit:  Limit for server connect failures before it is removed
  42       *   - serializer:          May be either "php" or "igbinary". Igbinary produces more compact
  43       *                          values, but serialization is much slower unless the php.ini option
  44       *                          igbinary.compact_strings is off.
  45       * @param array $params
  46       */
  47  	function __construct( $params ) {
  48          $params = $this->applyDefaultParams( $params );
  49  
  50          if ( $params['persistent'] ) {
  51              // The pool ID must be unique to the server/option combination.
  52              // The Memcached object is essentially shared for each pool ID.
  53              // We can only reuse a pool ID if we keep the config consistent.
  54              $this->client = new Memcached( md5( serialize( $params ) ) );
  55              if ( count( $this->client->getServerList() ) ) {
  56                  wfDebug( __METHOD__ . ": persistent Memcached object already loaded.\n" );
  57                  return; // already initialized; don't add duplicate servers
  58              }
  59          } else {
  60              $this->client = new Memcached;
  61          }
  62  
  63          if ( !isset( $params['serializer'] ) ) {
  64              $params['serializer'] = 'php';
  65          }
  66  
  67          if ( isset( $params['retry_timeout'] ) ) {
  68              $this->client->setOption( Memcached::OPT_RETRY_TIMEOUT, $params['retry_timeout'] );
  69          }
  70  
  71          if ( isset( $params['server_failure_limit'] ) ) {
  72              $this->client->setOption( Memcached::OPT_SERVER_FAILURE_LIMIT, $params['server_failure_limit'] );
  73          }
  74  
  75          // The compression threshold is an undocumented php.ini option for some
  76          // reason. There's probably not much harm in setting it globally, for
  77          // compatibility with the settings for the PHP client.
  78          ini_set( 'memcached.compression_threshold', $params['compress_threshold'] );
  79  
  80          // Set timeouts
  81          $this->client->setOption( Memcached::OPT_CONNECT_TIMEOUT, $params['connect_timeout'] * 1000 );
  82          $this->client->setOption( Memcached::OPT_SEND_TIMEOUT, $params['timeout'] );
  83          $this->client->setOption( Memcached::OPT_RECV_TIMEOUT, $params['timeout'] );
  84          $this->client->setOption( Memcached::OPT_POLL_TIMEOUT, $params['timeout'] / 1000 );
  85  
  86          // Set libketama mode since it's recommended by the documentation and
  87          // is as good as any. There's no way to configure libmemcached to use
  88          // hashes identical to the ones currently in use by the PHP client, and
  89          // even implementing one of the libmemcached hashes in pure PHP for
  90          // forwards compatibility would require MWMemcached::get_sock() to be
  91          // rewritten.
  92          $this->client->setOption( Memcached::OPT_LIBKETAMA_COMPATIBLE, true );
  93  
  94          // Set the serializer
  95          switch ( $params['serializer'] ) {
  96              case 'php':
  97                  $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP );
  98                  break;
  99              case 'igbinary':
 100                  if ( !Memcached::HAVE_IGBINARY ) {
 101                      throw new MWException( __CLASS__ . ': the igbinary extension is not available ' .
 102                          'but igbinary serialization was requested.' );
 103                  }
 104                  $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY );
 105                  break;
 106              default:
 107                  throw new MWException( __CLASS__ . ': invalid value for serializer parameter' );
 108          }
 109          $servers = array();
 110          foreach ( $params['servers'] as $host ) {
 111              $servers[] = IP::splitHostAndPort( $host ); // (ip, port)
 112          }
 113          $this->client->addServers( $servers );
 114      }
 115  
 116      /**
 117       * @param string $key
 118       * @param float $casToken [optional]
 119       * @return mixed
 120       */
 121  	public function get( $key, &$casToken = null ) {
 122          wfProfileIn( __METHOD__ );
 123          $this->debugLog( "get($key)" );
 124          $result = $this->client->get( $this->encodeKey( $key ), null, $casToken );
 125          $result = $this->checkResult( $key, $result );
 126          wfProfileOut( __METHOD__ );
 127          return $result;
 128      }
 129  
 130      /**
 131       * @param string $key
 132       * @param mixed $value
 133       * @param int $exptime
 134       * @return bool
 135       */
 136  	public function set( $key, $value, $exptime = 0 ) {
 137          $this->debugLog( "set($key)" );
 138          return $this->checkResult( $key, parent::set( $key, $value, $exptime ) );
 139      }
 140  
 141      /**
 142       * @param float $casToken
 143       * @param string $key
 144       * @param mixed $value
 145       * @param int $exptime
 146       * @return bool
 147       */
 148  	public function cas( $casToken, $key, $value, $exptime = 0 ) {
 149          $this->debugLog( "cas($key)" );
 150          return $this->checkResult( $key, parent::cas( $casToken, $key, $value, $exptime ) );
 151      }
 152  
 153      /**
 154       * @param string $key
 155       * @param int $time
 156       * @return bool
 157       */
 158  	public function delete( $key, $time = 0 ) {
 159          $this->debugLog( "delete($key)" );
 160          $result = parent::delete( $key, $time );
 161          if ( $result === false && $this->client->getResultCode() === Memcached::RES_NOTFOUND ) {
 162              // "Not found" is counted as success in our interface
 163              return true;
 164          } else {
 165              return $this->checkResult( $key, $result );
 166          }
 167      }
 168  
 169      /**
 170       * @param string $key
 171       * @param int $value
 172       * @param int $exptime
 173       * @return mixed
 174       */
 175  	public function add( $key, $value, $exptime = 0 ) {
 176          $this->debugLog( "add($key)" );
 177          return $this->checkResult( $key, parent::add( $key, $value, $exptime ) );
 178      }
 179  
 180      /**
 181       * @param string $key
 182       * @param int $value
 183       * @return mixed
 184       */
 185  	public function incr( $key, $value = 1 ) {
 186          $this->debugLog( "incr($key)" );
 187          $result = $this->client->increment( $key, $value );
 188          return $this->checkResult( $key, $result );
 189      }
 190  
 191      /**
 192       * @param string $key
 193       * @param int $value
 194       * @return mixed
 195       */
 196  	public function decr( $key, $value = 1 ) {
 197          $this->debugLog( "decr($key)" );
 198          $result = $this->client->decrement( $key, $value );
 199          return $this->checkResult( $key, $result );
 200      }
 201  
 202      /**
 203       * Check the return value from a client method call and take any necessary
 204       * action. Returns the value that the wrapper function should return. At
 205       * present, the return value is always the same as the return value from
 206       * the client, but some day we might find a case where it should be
 207       * different.
 208       *
 209       * @param string $key The key used by the caller, or false if there wasn't one.
 210       * @param mixed $result The return value
 211       * @return mixed
 212       */
 213  	protected function checkResult( $key, $result ) {
 214          if ( $result !== false ) {
 215              return $result;
 216          }
 217          switch ( $this->client->getResultCode() ) {
 218              case Memcached::RES_SUCCESS:
 219                  break;
 220              case Memcached::RES_DATA_EXISTS:
 221              case Memcached::RES_NOTSTORED:
 222              case Memcached::RES_NOTFOUND:
 223                  $this->debugLog( "result: " . $this->client->getResultMessage() );
 224                  break;
 225              default:
 226                  $msg = $this->client->getResultMessage();
 227                  if ( $key !== false ) {
 228                      $server = $this->client->getServerByKey( $key );
 229                      $serverName = "{$server['host']}:{$server['port']}";
 230                      $msg = "Memcached error for key \"$key\" on server \"$serverName\": $msg";
 231                  } else {
 232                      $msg = "Memcached error: $msg";
 233                  }
 234                  wfDebugLog( 'memcached-serious', $msg );
 235                  $this->setLastError( BagOStuff::ERR_UNEXPECTED );
 236          }
 237          return $result;
 238      }
 239  
 240      /**
 241       * @param array $keys
 242       * @return array
 243       */
 244  	public function getMulti( array $keys ) {
 245          wfProfileIn( __METHOD__ );
 246          $this->debugLog( 'getMulti(' . implode( ', ', $keys ) . ')' );
 247          $callback = array( $this, 'encodeKey' );
 248          $result = $this->client->getMulti( array_map( $callback, $keys ) );
 249          wfProfileOut( __METHOD__ );
 250          $result = $result ?: array(); // must be an array
 251          return $this->checkResult( false, $result );
 252      }
 253  
 254      /**
 255       * @param array $data
 256       * @param int $exptime
 257       * @return bool
 258       */
 259  	public function setMulti( array $data, $exptime = 0 ) {
 260          wfProfileIn( __METHOD__ );
 261          foreach ( $data as $key => $value ) {
 262              $encKey = $this->encodeKey( $key );
 263              if ( $encKey !== $key ) {
 264                  $data[$encKey] = $value;
 265                  unset( $data[$key] );
 266              }
 267          }
 268          $this->debugLog( 'setMulti(' . implode( ', ', array_keys( $data ) ) . ')' );
 269          $result = $this->client->setMulti( $data, $this->fixExpiry( $exptime ) );
 270          wfProfileOut( __METHOD__ );
 271          return $this->checkResult( false, $result );
 272      }
 273  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1