Merge "Fixed dependencies for jquery.collapsibleTabs"
[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 * @return bool|mixed
69 */
70 public function get( $key ) {
71 wfProfileIn( __METHOD__ );
72 $response = $this->doItemRequest( $key );
73 if ( !$response || $response['http_code'] == 404 ) {
74 wfProfileOut( __METHOD__ );
75 return false;
76 }
77 if ( $response['http_code'] >= 300 ) {
78 wfDebug( __METHOD__.": GET failure, got HTTP {$response['http_code']}\n" );
79 wfProfileOut( __METHOD__ );
80 return false;
81 }
82 $body = $response['body'];
83 $type = $response['content_type'];
84 if ( $type == 'application/vnd.php.serialized+deflate' ) {
85 $body = gzinflate( $body );
86 if ( !$body ) {
87 wfDebug( __METHOD__.": error inflating $key\n" );
88 wfProfileOut( __METHOD__ );
89 return false;
90 }
91 $data = unserialize( $body );
92 } elseif ( $type == 'application/vnd.php.serialized' ) {
93 $data = unserialize( $body );
94 } else {
95 wfDebug( __METHOD__.": unknown content type \"$type\"\n" );
96 wfProfileOut( __METHOD__ );
97 return false;
98 }
99
100 wfProfileOut( __METHOD__ );
101 return $data;
102 }
103
104 /**
105 * @param $key string
106 * @param $value mixed
107 * @param $expiry int
108 * @return bool
109 */
110 public function set( $key, $value, $expiry = 0 ) {
111 wfProfileIn( __METHOD__ );
112 $expiry = $this->convertExpiry( $expiry );
113 $ttl = $expiry ? $expiry - time() : 2147483647;
114 $blob = serialize( $value );
115 if ( strlen( $blob ) > 100 ) {
116 $blob = gzdeflate( $blob );
117 $contentType = 'application/vnd.php.serialized+deflate';
118 } else {
119 $contentType = 'application/vnd.php.serialized';
120 }
121
122 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
123
124 if ( $code == 404 ) {
125 // Maybe the cache does not exist yet, let's try creating it
126 if ( !$this->createCache( $key ) ) {
127 wfDebug( __METHOD__.": cache creation failed\n" );
128 wfProfileOut( __METHOD__ );
129 return false;
130 }
131 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
132 }
133
134 $result = false;
135 if ( !$code ) {
136 wfDebug( __METHOD__.": PUT failure for key $key\n" );
137 } elseif ( $code >= 300 ) {
138 wfDebug( __METHOD__.": PUT failure for key $key: HTTP $code\n" );
139 } else {
140 $result = true;
141 }
142
143 wfProfileOut( __METHOD__ );
144 return $result;
145 }
146
147 /**
148 * @param $key string
149 * @param $time int
150 * @return bool
151 */
152 public function delete( $key, $time = 0 ) {
153 wfProfileIn( __METHOD__ );
154 $response = $this->doItemRequest( $key,
155 array( CURLOPT_CUSTOMREQUEST => 'DELETE' ) );
156 $code = isset( $response['http_code'] ) ? $response['http_code'] : 0;
157 if ( !$response || ( $code != 404 && $code >= 300 ) ) {
158 wfDebug( __METHOD__.": DELETE failure for key $key\n" );
159 $result = false;
160 } else {
161 $result = true;
162 }
163 wfProfileOut( __METHOD__ );
164 return $result;
165 }
166
167 /**
168 * @param $key string
169 * @return string
170 */
171 protected function getCacheUrl( $key ) {
172 if ( count( $this->servers ) == 1 ) {
173 $server = reset( $this->servers );
174 } else {
175 // Use consistent hashing
176 $hashes = array();
177 foreach ( $this->servers as $server ) {
178 $hashes[$server] = md5( $server . '/' . $key );
179 }
180 asort( $hashes );
181 reset( $hashes );
182 $server = key( $hashes );
183 }
184 return "http://$server/ehcache/rest/{$this->cacheName}";
185 }
186
187 /**
188 * Get a cURL handle for the given cache URL.
189 * We cache the handles to allow keepalive.
190 */
191 protected function getCurl( $cacheUrl ) {
192 if ( !isset( $this->curls[$cacheUrl] ) ) {
193 $this->curls[$cacheUrl] = curl_init();
194 }
195 return $this->curls[$cacheUrl];
196 }
197
198 /**
199 * @param $key string
200 * @param $data
201 * @param $type
202 * @param $ttl
203 * @return int
204 */
205 protected function attemptPut( $key, $data, $type, $ttl ) {
206 // In initial benchmarking, it was 30 times faster to use CURLOPT_POST
207 // than CURLOPT_UPLOAD with CURLOPT_READFUNCTION. This was because
208 // CURLOPT_UPLOAD was pushing the request headers first, then waiting
209 // for an ACK packet, then sending the data, whereas CURLOPT_POST just
210 // sends the headers and the data in a single send().
211 $response = $this->doItemRequest( $key,
212 array(
213 CURLOPT_POST => 1,
214 CURLOPT_CUSTOMREQUEST => 'PUT',
215 CURLOPT_POSTFIELDS => $data,
216 CURLOPT_HTTPHEADER => array(
217 'Content-Type: ' . $type,
218 'ehcacheTimeToLiveSeconds: ' . $ttl
219 )
220 )
221 );
222 if ( !$response ) {
223 return 0;
224 } else {
225 return $response['http_code'];
226 }
227 }
228
229 /**
230 * @param $key string
231 * @return bool
232 */
233 protected function createCache( $key ) {
234 wfDebug( __METHOD__.": creating cache for $key\n" );
235 $response = $this->doCacheRequest( $key,
236 array(
237 CURLOPT_POST => 1,
238 CURLOPT_CUSTOMREQUEST => 'PUT',
239 CURLOPT_POSTFIELDS => '',
240 ) );
241 if ( !$response ) {
242 wfDebug( __CLASS__.": failed to create cache for $key\n" );
243 return false;
244 }
245 return ( $response['http_code'] == 201 /* created */
246 || $response['http_code'] == 409 /* already there */ );
247 }
248
249 /**
250 * @param $key string
251 * @param $curlOptions array
252 * @return array|bool|mixed
253 */
254 protected function doCacheRequest( $key, $curlOptions = array() ) {
255 $cacheUrl = $this->getCacheUrl( $key );
256 $curl = $this->getCurl( $cacheUrl );
257 return $this->doRequest( $curl, $cacheUrl, $curlOptions );
258 }
259
260 /**
261 * @param $key string
262 * @param $curlOptions array
263 * @return array|bool|mixed
264 */
265 protected function doItemRequest( $key, $curlOptions = array() ) {
266 $cacheUrl = $this->getCacheUrl( $key );
267 $curl = $this->getCurl( $cacheUrl );
268 $url = $cacheUrl . '/' . rawurlencode( $key );
269 return $this->doRequest( $curl, $url, $curlOptions );
270 }
271
272 /**
273 * @param $curl
274 * @param $url string
275 * @param $curlOptions array
276 * @return array|bool|mixed
277 * @throws MWException
278 */
279 protected function doRequest( $curl, $url, $curlOptions = array() ) {
280 if ( array_diff_key( $curlOptions, $this->curlOptions ) ) {
281 // var_dump( array_diff_key( $curlOptions, $this->curlOptions ) );
282 throw new MWException( __METHOD__.": to prevent options set in one doRequest() " .
283 "call from affecting subsequent doRequest() calls, only options listed " .
284 "in \$this->curlOptions may be specified in the \$curlOptions parameter." );
285 }
286 $curlOptions += $this->curlOptions;
287 $curlOptions[CURLOPT_URL] = $url;
288
289 curl_setopt_array( $curl, $curlOptions );
290 $result = curl_exec( $curl );
291 if ( $result === false ) {
292 wfDebug( __CLASS__.": curl error: " . curl_error( $curl ) . "\n" );
293 return false;
294 }
295 $info = curl_getinfo( $curl );
296 $info['body'] = $result;
297 return $info;
298 }
299 }