4 * A wrapper class for the PECL memcached client
8 class MemcachedPeclBagOStuff
extends MemcachedBagOStuff
{
13 * Available parameters are:
14 * - servers: The list of IP:port combinations holding the memcached servers.
15 * - persistent: Whether to use a persistent connection
16 * - compress_threshold: The minimum size an object must be before it is compressed
17 * - timeout: The read timeout in microseconds
18 * - connect_timeout: The connect timeout in seconds
19 * - serializer: May be either "php" or "igbinary". Igbinary produces more compact
20 * values, but serialization is much slower unless the php.ini option
21 * igbinary.compact_strings is off.
23 function __construct( $params ) {
24 $params = $this->applyDefaultParams( $params );
26 if ( $params['persistent'] ) {
27 $this->client
= new Memcached( __CLASS__
);
29 $this->client
= new Memcached
;
32 if ( !isset( $params['serializer'] ) ) {
33 $params['serializer'] = 'php';
36 // The compression threshold is an undocumented php.ini option for some
37 // reason. There's probably not much harm in setting it globally, for
38 // compatibility with the settings for the PHP client.
39 ini_set( 'memcached.compression_threshold', $params['compress_threshold'] );
42 $this->client
->setOption( Memcached
::OPT_CONNECT_TIMEOUT
, $params['connect_timeout'] * 1000 );
43 $this->client
->setOption( Memcached
::OPT_SEND_TIMEOUT
, $params['timeout'] );
44 $this->client
->setOption( Memcached
::OPT_RECV_TIMEOUT
, $params['timeout'] );
45 $this->client
->setOption( Memcached
::OPT_POLL_TIMEOUT
, $params['timeout'] / 1000 );
47 // Set libketama mode since it's recommended by the documentation and
48 // is as good as any. There's no way to configure libmemcached to use
49 // hashes identical to the ones currently in use by the PHP client, and
50 // even implementing one of the libmemcached hashes in pure PHP for
51 // forwards compatibility would require MWMemcached::get_sock() to be
53 $this->client
->setOption( Memcached
::OPT_LIBKETAMA_COMPATIBLE
, true );
56 switch ( $params['serializer'] ) {
58 $this->client
->setOption( Memcached
::OPT_SERIALIZER
, Memcached
::SERIALIZER_PHP
);
61 if ( !Memcached
::HAVE_IGBINARY
) {
62 throw new MWException( __CLASS__
.': the igbinary extension is not available ' .
63 'but igbinary serialization was requested.' );
65 $this->client
->setOption( Memcached
::OPT_SERIALIZER
, Memcached
::SERIALIZER_IGBINARY
);
68 throw new MWException( __CLASS__
.': invalid value for serializer parameter' );
70 foreach ( $params['servers'] as $host ) {
71 list( $ip, $port ) = IP
::splitHostAndPort( $host );
72 $this->client
->addServer( $ip, $port );
80 public function get( $key ) {
81 $this->debugLog( "get($key)" );
82 return $this->checkResult( $key, parent
::get( $key ) );
91 public function set( $key, $value, $exptime = 0 ) {
92 $this->debugLog( "set($key)" );
93 return $this->checkResult( $key, parent
::set( $key, $value, $exptime ) );
101 public function delete( $key, $time = 0 ) {
102 $this->debugLog( "delete($key)" );
103 $result = parent
::delete( $key, $time );
104 if ( $result === false && $this->client
->getResultCode() === Memcached
::RES_NOTFOUND
) {
105 // "Not found" is counted as success in our interface
108 return $this->checkResult( $key, $result );
117 public function add( $key, $value, $exptime = 0 ) {
118 $this->debugLog( "add($key)" );
119 return $this->checkResult( $key, parent
::add( $key, $value, $exptime ) );
128 public function replace( $key, $value, $exptime = 0 ) {
129 $this->debugLog( "replace($key)" );
130 return $this->checkResult( $key, parent
::replace( $key, $value, $exptime ) );
138 public function incr( $key, $value = 1 ) {
139 $this->debugLog( "incr($key)" );
140 $result = $this->client
->increment( $key, $value );
141 return $this->checkResult( $key, $result );
149 public function decr( $key, $value = 1 ) {
150 $this->debugLog( "decr($key)" );
151 $result = $this->client
->decrement( $key, $value );
152 return $this->checkResult( $key, $result );
156 * Check the return value from a client method call and take any necessary
157 * action. Returns the value that the wrapper function should return. At
158 * present, the return value is always the same as the return value from
159 * the client, but some day we might find a case where it should be
162 * @param $key The key used by the caller, or false if there wasn't one.
163 * @param $result The return value
165 protected function checkResult( $key, $result ) {
166 if ( $result !== false ) {
169 switch ( $this->client
->getResultCode() ) {
170 case Memcached
::RES_SUCCESS
:
172 case Memcached
::RES_DATA_EXISTS
:
173 case Memcached
::RES_NOTSTORED
:
174 case Memcached
::RES_NOTFOUND
:
175 $this->debugLog( "result: " . $this->client
->getResultMessage() );
178 $msg = $this->client
->getResultMessage();
179 if ( $key !== false ) {
180 $server = $this->client
->getServerByKey( $key );
181 $serverName = "{$server['host']}:{$server['port']}";
182 $msg = "Memcached error for key \"$key\" on server \"$serverName\": $msg";
184 $msg = "Memcached error: $msg";
186 wfDebugLog( 'memcached-serious', $msg );
195 public function getMulti( array $keys ) {
196 $this->debugLog( 'getMulti(' . implode( ', ', $keys ) . ')' );
197 $callback = array( $this, 'encodeKey' );
198 $result = $this->client
->getMulti( array_map( $callback, $keys ) );
199 return $this->checkResult( false, $result );
202 /* NOTE: there is no cas() method here because it is currently not supported
203 * by the BagOStuff interface and other BagOStuff subclasses, such as