testPngNativetZtxt requires zlib extension
[lhc/web/wiklou.git] / includes / objectcache / EhcacheBagOStuff.php
1 <?php
2 /**
3 * Object caching using the Ehcache RESTful web service.
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 * Client for the Ehcache RESTful web service - http://ehcache.org/documentation/cache_server.html
26 * TODO: Simplify configuration and add to the installer.
27 *
28 * @ingroup Cache
29 */
30 class EhcacheBagOStuff extends BagOStuff {
31 var $servers, $cacheName, $connectTimeout, $timeout, $curlOptions,
32 $requestData, $requestDataPos;
33
34 var $curls = array();
35
36 /**
37 * @param $params array
38 * @throws MWException
39 */
40 function __construct( $params ) {
41 if ( !defined( 'CURLOPT_TIMEOUT_MS' ) ) {
42 throw new MWException( __CLASS__.' requires curl version 7.16.2 or later.' );
43 }
44 if ( !extension_loaded( 'zlib' ) ) {
45 throw new MWException( __CLASS__.' requires the zlib extension' );
46 }
47 if ( !isset( $params['servers'] ) ) {
48 throw new MWException( __METHOD__.': servers parameter is required' );
49 }
50 $this->servers = $params['servers'];
51 $this->cacheName = isset( $params['cache'] ) ? $params['cache'] : 'mw';
52 $this->connectTimeout = isset( $params['connectTimeout'] )
53 ? $params['connectTimeout'] : 1;
54 $this->timeout = isset( $params['timeout'] ) ? $params['timeout'] : 1;
55 $this->curlOptions = array(
56 CURLOPT_CONNECTTIMEOUT_MS => intval( $this->connectTimeout * 1000 ),
57 CURLOPT_TIMEOUT_MS => intval( $this->timeout * 1000 ),
58 CURLOPT_RETURNTRANSFER => 1,
59 CURLOPT_CUSTOMREQUEST => 'GET',
60 CURLOPT_POST => 0,
61 CURLOPT_POSTFIELDS => '',
62 CURLOPT_HTTPHEADER => array(),
63 );
64 }
65
66 /**
67 * @param $key string
68 * @param $casToken[optional] mixed
69 * @return bool|mixed
70 */
71 public function get( $key, &$casToken = null ) {
72 wfProfileIn( __METHOD__ );
73 $response = $this->doItemRequest( $key );
74 if ( !$response || $response['http_code'] == 404 ) {
75 wfProfileOut( __METHOD__ );
76 return false;
77 }
78 if ( $response['http_code'] >= 300 ) {
79 wfDebug( __METHOD__.": GET failure, got HTTP {$response['http_code']}\n" );
80 wfProfileOut( __METHOD__ );
81 return false;
82 }
83 $body = $response['body'];
84 $type = $response['content_type'];
85 if ( $type == 'application/vnd.php.serialized+deflate' ) {
86 $body = gzinflate( $body );
87 if ( !$body ) {
88 wfDebug( __METHOD__.": error inflating $key\n" );
89 wfProfileOut( __METHOD__ );
90 return false;
91 }
92 $data = unserialize( $body );
93 } elseif ( $type == 'application/vnd.php.serialized' ) {
94 $data = unserialize( $body );
95 } else {
96 wfDebug( __METHOD__.": unknown content type \"$type\"\n" );
97 wfProfileOut( __METHOD__ );
98 return false;
99 }
100
101 $casToken = $body;
102
103 wfProfileOut( __METHOD__ );
104 return $data;
105 }
106
107 /**
108 * @param $key string
109 * @param $value mixed
110 * @param $expiry int
111 * @return bool
112 */
113 public function set( $key, $value, $expiry = 0 ) {
114 wfProfileIn( __METHOD__ );
115 $expiry = $this->convertExpiry( $expiry );
116 $ttl = $expiry ? $expiry - time() : 2147483647;
117 $blob = serialize( $value );
118 if ( strlen( $blob ) > 100 ) {
119 $blob = gzdeflate( $blob );
120 $contentType = 'application/vnd.php.serialized+deflate';
121 } else {
122 $contentType = 'application/vnd.php.serialized';
123 }
124
125 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
126
127 if ( $code == 404 ) {
128 // Maybe the cache does not exist yet, let's try creating it
129 if ( !$this->createCache( $key ) ) {
130 wfDebug( __METHOD__.": cache creation failed\n" );
131 wfProfileOut( __METHOD__ );
132 return false;
133 }
134 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
135 }
136
137 $result = false;
138 if ( !$code ) {
139 wfDebug( __METHOD__.": PUT failure for key $key\n" );
140 } elseif ( $code >= 300 ) {
141 wfDebug( __METHOD__.": PUT failure for key $key: HTTP $code\n" );
142 } else {
143 $result = true;
144 }
145
146 wfProfileOut( __METHOD__ );
147 return $result;
148 }
149
150 /**
151 * @param $casToken mixed
152 * @param $key string
153 * @param $value mixed
154 * @param $exptime int
155 * @return bool
156 */
157 public function cas( $casToken, $key, $value, $exptime = 0 ) {
158 // Not sure if we can implement CAS for ehcache. There appears to be CAS-support per
159 // http://ehcache.org/documentation/get-started/consistency-options#cas-cache-operations,
160 // but I can't find any docs for our current implementation.
161 throw new MWException( "CAS is not implemented in " . __CLASS__ );
162 }
163
164 /**
165 * @param $key string
166 * @param $time int
167 * @return bool
168 */
169 public function delete( $key, $time = 0 ) {
170 wfProfileIn( __METHOD__ );
171 $response = $this->doItemRequest( $key,
172 array( CURLOPT_CUSTOMREQUEST => 'DELETE' ) );
173 $code = isset( $response['http_code'] ) ? $response['http_code'] : 0;
174 if ( !$response || ( $code != 404 && $code >= 300 ) ) {
175 wfDebug( __METHOD__.": DELETE failure for key $key\n" );
176 $result = false;
177 } else {
178 $result = true;
179 }
180 wfProfileOut( __METHOD__ );
181 return $result;
182 }
183
184 /**
185 * @see BagOStuff::merge()
186 * @return bool success
187 */
188 public function merge( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
189 return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
190 }
191
192 /**
193 * @param $key string
194 * @return string
195 */
196 protected function getCacheUrl( $key ) {
197 if ( count( $this->servers ) == 1 ) {
198 $server = reset( $this->servers );
199 } else {
200 // Use consistent hashing
201 $hashes = array();
202 foreach ( $this->servers as $server ) {
203 $hashes[$server] = md5( $server . '/' . $key );
204 }
205 asort( $hashes );
206 reset( $hashes );
207 $server = key( $hashes );
208 }
209 return "http://$server/ehcache/rest/{$this->cacheName}";
210 }
211
212 /**
213 * Get a cURL handle for the given cache URL.
214 * We cache the handles to allow keepalive.
215 */
216 protected function getCurl( $cacheUrl ) {
217 if ( !isset( $this->curls[$cacheUrl] ) ) {
218 $this->curls[$cacheUrl] = curl_init();
219 }
220 return $this->curls[$cacheUrl];
221 }
222
223 /**
224 * @param $key string
225 * @param $data
226 * @param $type
227 * @param $ttl
228 * @return int
229 */
230 protected function attemptPut( $key, $data, $type, $ttl ) {
231 // In initial benchmarking, it was 30 times faster to use CURLOPT_POST
232 // than CURLOPT_UPLOAD with CURLOPT_READFUNCTION. This was because
233 // CURLOPT_UPLOAD was pushing the request headers first, then waiting
234 // for an ACK packet, then sending the data, whereas CURLOPT_POST just
235 // sends the headers and the data in a single send().
236 $response = $this->doItemRequest( $key,
237 array(
238 CURLOPT_POST => 1,
239 CURLOPT_CUSTOMREQUEST => 'PUT',
240 CURLOPT_POSTFIELDS => $data,
241 CURLOPT_HTTPHEADER => array(
242 'Content-Type: ' . $type,
243 'ehcacheTimeToLiveSeconds: ' . $ttl
244 )
245 )
246 );
247 if ( !$response ) {
248 return 0;
249 } else {
250 return $response['http_code'];
251 }
252 }
253
254 /**
255 * @param $key string
256 * @return bool
257 */
258 protected function createCache( $key ) {
259 wfDebug( __METHOD__.": creating cache for $key\n" );
260 $response = $this->doCacheRequest( $key,
261 array(
262 CURLOPT_POST => 1,
263 CURLOPT_CUSTOMREQUEST => 'PUT',
264 CURLOPT_POSTFIELDS => '',
265 ) );
266 if ( !$response ) {
267 wfDebug( __CLASS__.": failed to create cache for $key\n" );
268 return false;
269 }
270 return ( $response['http_code'] == 201 /* created */
271 || $response['http_code'] == 409 /* already there */ );
272 }
273
274 /**
275 * @param $key string
276 * @param $curlOptions array
277 * @return array|bool|mixed
278 */
279 protected function doCacheRequest( $key, $curlOptions = array() ) {
280 $cacheUrl = $this->getCacheUrl( $key );
281 $curl = $this->getCurl( $cacheUrl );
282 return $this->doRequest( $curl, $cacheUrl, $curlOptions );
283 }
284
285 /**
286 * @param $key string
287 * @param $curlOptions array
288 * @return array|bool|mixed
289 */
290 protected function doItemRequest( $key, $curlOptions = array() ) {
291 $cacheUrl = $this->getCacheUrl( $key );
292 $curl = $this->getCurl( $cacheUrl );
293 $url = $cacheUrl . '/' . rawurlencode( $key );
294 return $this->doRequest( $curl, $url, $curlOptions );
295 }
296
297 /**
298 * @param $curl
299 * @param $url string
300 * @param $curlOptions array
301 * @return array|bool|mixed
302 * @throws MWException
303 */
304 protected function doRequest( $curl, $url, $curlOptions = array() ) {
305 if ( array_diff_key( $curlOptions, $this->curlOptions ) ) {
306 // var_dump( array_diff_key( $curlOptions, $this->curlOptions ) );
307 throw new MWException( __METHOD__.": to prevent options set in one doRequest() " .
308 "call from affecting subsequent doRequest() calls, only options listed " .
309 "in \$this->curlOptions may be specified in the \$curlOptions parameter." );
310 }
311 $curlOptions += $this->curlOptions;
312 $curlOptions[CURLOPT_URL] = $url;
313
314 curl_setopt_array( $curl, $curlOptions );
315 $result = curl_exec( $curl );
316 if ( $result === false ) {
317 wfDebug( __CLASS__.": curl error: " . curl_error( $curl ) . "\n" );
318 return false;
319 }
320 $info = curl_getinfo( $curl );
321 $info['body'] = $result;
322 return $info;
323 }
324 }