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