Merge "Preserve caller expectations for behaviour of sslVerifyHost"
[lhc/web/wiklou.git] / includes / objectcache / MemcachedPeclBagOStuff.php
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 * - serializer: May be either "php" or "igbinary". Igbinary produces more compact
41 * values, but serialization is much slower unless the php.ini option
42 * igbinary.compact_strings is off.
43 */
44 function __construct( $params ) {
45 $params = $this->applyDefaultParams( $params );
46
47 if ( $params['persistent'] ) {
48 // The pool ID must be unique to the server/option combination.
49 // The Memcached object is essentially shared for each pool ID.
50 // We can only resuse a pool ID if we keep the config consistent.
51 $this->client = new Memcached( md5( serialize( $params ) ) );
52 if ( count( $this->client->getServerList() ) ) {
53 wfDebug( __METHOD__ . ": persistent Memcached object already loaded.\n" );
54 return; // already initialized; don't add duplicate servers
55 }
56 } else {
57 $this->client = new Memcached;
58 }
59
60 if ( !isset( $params['serializer'] ) ) {
61 $params['serializer'] = 'php';
62 }
63
64 // The compression threshold is an undocumented php.ini option for some
65 // reason. There's probably not much harm in setting it globally, for
66 // compatibility with the settings for the PHP client.
67 ini_set( 'memcached.compression_threshold', $params['compress_threshold'] );
68
69 // Set timeouts
70 $this->client->setOption( Memcached::OPT_CONNECT_TIMEOUT, $params['connect_timeout'] * 1000 );
71 $this->client->setOption( Memcached::OPT_SEND_TIMEOUT, $params['timeout'] );
72 $this->client->setOption( Memcached::OPT_RECV_TIMEOUT, $params['timeout'] );
73 $this->client->setOption( Memcached::OPT_POLL_TIMEOUT, $params['timeout'] / 1000 );
74
75 // Set libketama mode since it's recommended by the documentation and
76 // is as good as any. There's no way to configure libmemcached to use
77 // hashes identical to the ones currently in use by the PHP client, and
78 // even implementing one of the libmemcached hashes in pure PHP for
79 // forwards compatibility would require MWMemcached::get_sock() to be
80 // rewritten.
81 $this->client->setOption( Memcached::OPT_LIBKETAMA_COMPATIBLE, true );
82
83 // Set the serializer
84 switch ( $params['serializer'] ) {
85 case 'php':
86 $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_PHP );
87 break;
88 case 'igbinary':
89 if ( !Memcached::HAVE_IGBINARY ) {
90 throw new MWException( __CLASS__.': the igbinary extension is not available ' .
91 'but igbinary serialization was requested.' );
92 }
93 $this->client->setOption( Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY );
94 break;
95 default:
96 throw new MWException( __CLASS__.': invalid value for serializer parameter' );
97 }
98 $servers = array();
99 foreach ( $params['servers'] as $host ) {
100 $servers[] = IP::splitHostAndPort( $host ); // (ip, port)
101 }
102 $this->client->addServers( $servers );
103 }
104
105 /**
106 * @param $key string
107 * @param $casToken[optional] float
108 * @return Mixed
109 */
110 public function get( $key, &$casToken = null ) {
111 wfProfileIn( __METHOD__ );
112 $this->debugLog( "get($key)" );
113 $result = $this->client->get( $this->encodeKey( $key ), null, $casToken );
114 $result = $this->checkResult( $key, $result );
115 wfProfileOut( __METHOD__ );
116 return $result;
117 }
118
119 /**
120 * @param $key string
121 * @param $value
122 * @param $exptime int
123 * @return bool
124 */
125 public function set( $key, $value, $exptime = 0 ) {
126 $this->debugLog( "set($key)" );
127 return $this->checkResult( $key, parent::set( $key, $value, $exptime ) );
128 }
129
130 /**
131 * @param $casToken float
132 * @param $key string
133 * @param $value
134 * @param $exptime int
135 * @return bool
136 */
137 public function cas( $casToken, $key, $value, $exptime = 0 ) {
138 $this->debugLog( "cas($key)" );
139 return $this->checkResult( $key, parent::cas( $casToken, $key, $value, $exptime ) );
140 }
141
142 /**
143 * @param $key string
144 * @param $time int
145 * @return bool
146 */
147 public function delete( $key, $time = 0 ) {
148 $this->debugLog( "delete($key)" );
149 $result = parent::delete( $key, $time );
150 if ( $result === false && $this->client->getResultCode() === Memcached::RES_NOTFOUND ) {
151 // "Not found" is counted as success in our interface
152 return true;
153 } else {
154 return $this->checkResult( $key, $result );
155 }
156 }
157
158 /**
159 * @param $key string
160 * @param $value int
161 * @param $exptime int
162 * @return Mixed
163 */
164 public function add( $key, $value, $exptime = 0 ) {
165 $this->debugLog( "add($key)" );
166 return $this->checkResult( $key, parent::add( $key, $value, $exptime ) );
167 }
168
169 /**
170 * @param $key string
171 * @param $value int
172 * @param $exptime
173 * @return Mixed
174 */
175 public function replace( $key, $value, $exptime = 0 ) {
176 $this->debugLog( "replace($key)" );
177 return $this->checkResult( $key, parent::replace( $key, $value, $exptime ) );
178 }
179
180 /**
181 * @param $key string
182 * @param $value int
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 $key string
193 * @param $value int
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 $key string The key used by the caller, or false if there wasn't one.
210 * @param $result Mixed 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 }
236 return $result;
237 }
238
239 /**
240 * @param $keys Array
241 * @return Array
242 */
243 public function getMulti( array $keys ) {
244 wfProfileIn( __METHOD__ );
245 $this->debugLog( 'getMulti(' . implode( ', ', $keys ) . ')' );
246 $callback = array( $this, 'encodeKey' );
247 $result = $this->client->getMulti( array_map( $callback, $keys ) );
248 wfProfileOut( __METHOD__ );
249 return $this->checkResult( false, $result );
250 }
251
252 /* NOTE: there is no cas() method here because it is currently not supported
253 * by the BagOStuff interface and other BagOStuff subclasses, such as
254 * SqlBagOStuff.
255 */
256 }