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