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
25 * Client for the Ehcache RESTful web service - http://ehcache.org/documentation/cache_server.html
26 * TODO: Simplify configuration and add to the installer.
30 class EhcacheBagOStuff
extends BagOStuff
{
31 var $servers, $cacheName, $connectTimeout, $timeout, $curlOptions,
32 $requestData, $requestDataPos;
37 * @param $params array
39 function __construct( $params ) {
40 if ( !defined( 'CURLOPT_TIMEOUT_MS' ) ) {
41 throw new MWException( __CLASS__
.' requires curl version 7.16.2 or later.' );
43 if ( !extension_loaded( 'zlib' ) ) {
44 throw new MWException( __CLASS__
.' requires the zlib extension' );
46 if ( !isset( $params['servers'] ) ) {
47 throw new MWException( __METHOD__
.': servers parameter is required' );
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',
60 CURLOPT_POSTFIELDS
=> '',
61 CURLOPT_HTTPHEADER
=> array(),
69 public function get( $key ) {
70 wfProfileIn( __METHOD__
);
71 $response = $this->doItemRequest( $key );
72 if ( !$response ||
$response['http_code'] == 404 ) {
73 wfProfileOut( __METHOD__
);
76 if ( $response['http_code'] >= 300 ) {
77 wfDebug( __METHOD__
.": GET failure, got HTTP {$response['http_code']}\n" );
78 wfProfileOut( __METHOD__
);
81 $body = $response['body'];
82 $type = $response['content_type'];
83 if ( $type == 'application/vnd.php.serialized+deflate' ) {
84 $body = gzinflate( $body );
86 wfDebug( __METHOD__
.": error inflating $key\n" );
87 wfProfileOut( __METHOD__
);
90 $data = unserialize( $body );
91 } elseif ( $type == 'application/vnd.php.serialized' ) {
92 $data = unserialize( $body );
94 wfDebug( __METHOD__
.": unknown content type \"$type\"\n" );
95 wfProfileOut( __METHOD__
);
99 wfProfileOut( __METHOD__
);
105 * @param $value mixed
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';
118 $contentType = 'application/vnd.php.serialized';
121 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
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__
);
130 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
135 wfDebug( __METHOD__
.": PUT failure for key $key\n" );
136 } elseif ( $code >= 300 ) {
137 wfDebug( __METHOD__
.": PUT failure for key $key: HTTP $code\n" );
142 wfProfileOut( __METHOD__
);
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" );
162 wfProfileOut( __METHOD__
);
170 protected function getCacheUrl( $key ) {
171 if ( count( $this->servers
) == 1 ) {
172 $server = reset( $this->servers
);
174 // Use consistent hashing
176 foreach ( $this->servers
as $server ) {
177 $hashes[$server] = md5( $server . '/' . $key );
181 $server = key( $hashes );
183 return "http://$server/ehcache/rest/{$this->cacheName}";
187 * Get a cURL handle for the given cache URL.
188 * We cache the handles to allow keepalive.
190 protected function getCurl( $cacheUrl ) {
191 if ( !isset( $this->curls
[$cacheUrl] ) ) {
192 $this->curls
[$cacheUrl] = curl_init();
194 return $this->curls
[$cacheUrl];
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,
213 CURLOPT_CUSTOMREQUEST
=> 'PUT',
214 CURLOPT_POSTFIELDS
=> $data,
215 CURLOPT_HTTPHEADER
=> array(
216 'Content-Type: ' . $type,
217 'ehcacheTimeToLiveSeconds: ' . $ttl
224 return $response['http_code'];
232 protected function createCache( $key ) {
233 wfDebug( __METHOD__
.": creating cache for $key\n" );
234 $response = $this->doCacheRequest( $key,
237 CURLOPT_CUSTOMREQUEST
=> 'PUT',
238 CURLOPT_POSTFIELDS
=> '',
241 wfDebug( __CLASS__
.": failed to create cache for $key\n" );
244 return ( $response['http_code'] == 201 /* created */
245 ||
$response['http_code'] == 409 /* already there */ );
250 * @param $curlOptions array
251 * @return array|bool|mixed
253 protected function doCacheRequest( $key, $curlOptions = array() ) {
254 $cacheUrl = $this->getCacheUrl( $key );
255 $curl = $this->getCurl( $cacheUrl );
256 return $this->doRequest( $curl, $cacheUrl, $curlOptions );
261 * @param $curlOptions array
262 * @return array|bool|mixed
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 );
274 * @param $curlOptions array
275 * @return array|bool|mixed
276 * @throws MWException
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." );
285 $curlOptions +
= $this->curlOptions
;
286 $curlOptions[CURLOPT_URL
] = $url;
288 curl_setopt_array( $curl, $curlOptions );
289 $result = curl_exec( $curl );
290 if ( $result === false ) {
291 wfDebug( __CLASS__
.": curl error: " . curl_error( $curl ) . "\n" );
294 $info = curl_getinfo( $curl );
295 $info['body'] = $result;