Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / objectcache / EhcacheBagOStuff.php
blob960668f5e07ad797c58dfbdb49a9b0977a50aac2
1 <?php
2 /**
3 * Object caching using the Ehcache RESTful web service.
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.
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.
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
20 * @file
21 * @ingroup Cache
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.
28 * @ingroup Cache
30 class EhcacheBagOStuff extends BagOStuff {
31 var $servers, $cacheName, $connectTimeout, $timeout, $curlOptions,
32 $requestData, $requestDataPos;
34 var $curls = array();
36 /**
37 * @param $params array
38 * @throws MWException
40 function __construct( $params ) {
41 if ( !defined( 'CURLOPT_TIMEOUT_MS' ) ) {
42 throw new MWException( __CLASS__ . ' requires curl version 7.16.2 or later.' );
44 if ( !extension_loaded( 'zlib' ) ) {
45 throw new MWException( __CLASS__ . ' requires the zlib extension' );
47 if ( !isset( $params['servers'] ) ) {
48 throw new MWException( __METHOD__ . ': servers parameter is required' );
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(),
66 /**
67 * @param $key string
68 * @param $casToken[optional] mixed
69 * @return bool|mixed
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;
78 if ( $response['http_code'] >= 300 ) {
79 wfDebug( __METHOD__ . ": GET failure, got HTTP {$response['http_code']}\n" );
80 wfProfileOut( __METHOD__ );
81 return false;
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;
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;
101 $casToken = $body;
103 wfProfileOut( __METHOD__ );
104 return $data;
108 * @param $key string
109 * @param $value mixed
110 * @param $expiry int
111 * @return bool
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';
125 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
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;
134 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
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;
146 wfProfileOut( __METHOD__ );
147 return $result;
151 * @param $casToken mixed
152 * @param $key string
153 * @param $value mixed
154 * @param $exptime int
155 * @return bool
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__ );
165 * @param $key string
166 * @param $time int
167 * @return bool
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;
180 wfProfileOut( __METHOD__ );
181 return $result;
185 * @see BagOStuff::merge()
186 * @return bool success
188 public function merge( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
189 return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
193 * @param $key string
194 * @return string
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 );
205 asort( $hashes );
206 reset( $hashes );
207 $server = key( $hashes );
209 return "http://$server/ehcache/rest/{$this->cacheName}";
213 * Get a cURL handle for the given cache URL.
214 * We cache the handles to allow keepalive.
216 protected function getCurl( $cacheUrl ) {
217 if ( !isset( $this->curls[$cacheUrl] ) ) {
218 $this->curls[$cacheUrl] = curl_init();
220 return $this->curls[$cacheUrl];
224 * @param $key string
225 * @param $data
226 * @param $type
227 * @param $ttl
228 * @return int
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
247 if ( !$response ) {
248 return 0;
249 } else {
250 return $response['http_code'];
255 * @param $key string
256 * @return bool
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;
270 return ( $response['http_code'] == 201 /* created */
271 || $response['http_code'] == 409 /* already there */ );
275 * @param $key string
276 * @param $curlOptions array
277 * @return array|bool|mixed
279 protected function doCacheRequest( $key, $curlOptions = array() ) {
280 $cacheUrl = $this->getCacheUrl( $key );
281 $curl = $this->getCurl( $cacheUrl );
282 return $this->doRequest( $curl, $cacheUrl, $curlOptions );
286 * @param $key string
287 * @param $curlOptions array
288 * @return array|bool|mixed
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 );
298 * @param $curl
299 * @param $url string
300 * @param $curlOptions array
301 * @return array|bool|mixed
302 * @throws MWException
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." );
311 $curlOptions += $this->curlOptions;
312 $curlOptions[CURLOPT_URL] = $url;
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;
320 $info = curl_getinfo( $curl );
321 $info['body'] = $result;
322 return $info;