Merge "Title::moveToInternal doesn't return anything, but it does throw an exception"
[lhc/web/wiklou.git] / includes / objectcache / MemcachedPeclBagOStuff.php
1 <?php
2
3 /**
4 * A wrapper class for the PECL memcached client
5 *
6 * @ingroup Cache
7 */
8 class MemcachedPeclBagOStuff extends MemcachedBagOStuff {
9
10 /**
11 * Constructor
12 *
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.
22 */
23 function __construct( $params ) {
24 $params = $this->applyDefaultParams( $params );
25
26 if ( $params['persistent'] ) {
27 $this->client = new Memcached( __CLASS__ );
28 } else {
29 $this->client = new Memcached;
30 }
31
32 if ( !isset( $params['serializer'] ) ) {
33 $params['serializer'] = 'php';
34 }
35
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'] );
40
41 // Set timeouts
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 );
46
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
52 // rewritten.
53 $this->client->setOption( Memcached::OPT_LIBKETAMA_COMPATIBLE, true );
54
55 // Set the serializer
56 switch ( $params['serializer'] ) {
57 case 'php':
58 $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP );
59 break;
60 case 'igbinary':
61 if ( !Memcached::HAVE_IGBINARY ) {
62 throw new MWException( __CLASS__.': the igbinary extension is not available ' .
63 'but igbinary serialization was requested.' );
64 }
65 $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY );
66 break;
67 default:
68 throw new MWException( __CLASS__.': invalid value for serializer parameter' );
69 }
70 foreach ( $params['servers'] as $host ) {
71 list( $ip, $port ) = IP::splitHostAndPort( $host );
72 $this->client->addServer( $ip, $port );
73 }
74 }
75
76 /**
77 * @param $key string
78 * @return Mixed
79 */
80 public function get( $key ) {
81 $this->debugLog( "get($key)" );
82 return $this->checkResult( $key, parent::get( $key ) );
83 }
84
85 /**
86 * @param $key string
87 * @param $value
88 * @param $exptime int
89 * @return bool
90 */
91 public function set( $key, $value, $exptime = 0 ) {
92 $this->debugLog( "set($key)" );
93 return $this->checkResult( $key, parent::set( $key, $value, $exptime ) );
94 }
95
96 /**
97 * @param $key string
98 * @param $time int
99 * @return bool
100 */
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
106 return true;
107 } else {
108 return $this->checkResult( $key, $result );
109 }
110 }
111
112 /**
113 * @param $key string
114 * @param $value int
115 * @return Mixed
116 */
117 public function add( $key, $value, $exptime = 0 ) {
118 $this->debugLog( "add($key)" );
119 return $this->checkResult( $key, parent::add( $key, $value, $exptime ) );
120 }
121
122 /**
123 * @param $key string
124 * @param $value int
125 * @param $exptime
126 * @return Mixed
127 */
128 public function replace( $key, $value, $exptime = 0 ) {
129 $this->debugLog( "replace($key)" );
130 return $this->checkResult( $key, parent::replace( $key, $value, $exptime ) );
131 }
132
133 /**
134 * @param $key string
135 * @param $value int
136 * @return Mixed
137 */
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 );
142 }
143
144 /**
145 * @param $key string
146 * @param $value int
147 * @return Mixed
148 */
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 );
153 }
154
155 /**
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
160 * different.
161 *
162 * @param $key The key used by the caller, or false if there wasn't one.
163 * @param $result The return value
164 */
165 protected function checkResult( $key, $result ) {
166 if ( $result !== false ) {
167 return $result;
168 }
169 switch ( $this->client->getResultCode() ) {
170 case Memcached::RES_SUCCESS:
171 break;
172 case Memcached::RES_DATA_EXISTS:
173 case Memcached::RES_NOTSTORED:
174 case Memcached::RES_NOTFOUND:
175 $this->debugLog( "result: " . $this->client->getResultMessage() );
176 break;
177 default:
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";
183 } else {
184 $msg = "Memcached error: $msg";
185 }
186 wfDebugLog( 'memcached-serious', $msg );
187 }
188 return $result;
189 }
190
191 /**
192 * @param $keys Array
193 * @return Array
194 */
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 );
200 }
201
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
204 * SqlBagOStuff.
205 */
206 }