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
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',
61 CURLOPT_POSTFIELDS
=> '',
62 CURLOPT_HTTPHEADER
=> array(),
70 public function get( $key ) {
71 wfProfileIn( __METHOD__
);
72 $response = $this->doItemRequest( $key );
73 if ( !$response ||
$response['http_code'] == 404 ) {
74 wfProfileOut( __METHOD__
);
77 if ( $response['http_code'] >= 300 ) {
78 wfDebug( __METHOD__
.": GET failure, got HTTP {$response['http_code']}\n" );
79 wfProfileOut( __METHOD__
);
82 $body = $response['body'];
83 $type = $response['content_type'];
84 if ( $type == 'application/vnd.php.serialized+deflate' ) {
85 $body = gzinflate( $body );
87 wfDebug( __METHOD__
.": error inflating $key\n" );
88 wfProfileOut( __METHOD__
);
91 $data = unserialize( $body );
92 } elseif ( $type == 'application/vnd.php.serialized' ) {
93 $data = unserialize( $body );
95 wfDebug( __METHOD__
.": unknown content type \"$type\"\n" );
96 wfProfileOut( __METHOD__
);
100 wfProfileOut( __METHOD__
);
106 * @param $value mixed
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';
119 $contentType = 'application/vnd.php.serialized';
122 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
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__
);
131 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
136 wfDebug( __METHOD__
.": PUT failure for key $key\n" );
137 } elseif ( $code >= 300 ) {
138 wfDebug( __METHOD__
.": PUT failure for key $key: HTTP $code\n" );
143 wfProfileOut( __METHOD__
);
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" );
163 wfProfileOut( __METHOD__
);
171 protected function getCacheUrl( $key ) {
172 if ( count( $this->servers
) == 1 ) {
173 $server = reset( $this->servers
);
175 // Use consistent hashing
177 foreach ( $this->servers
as $server ) {
178 $hashes[$server] = md5( $server . '/' . $key );
182 $server = key( $hashes );
184 return "http://$server/ehcache/rest/{$this->cacheName}";
188 * Get a cURL handle for the given cache URL.
189 * We cache the handles to allow keepalive.
191 protected function getCurl( $cacheUrl ) {
192 if ( !isset( $this->curls
[$cacheUrl] ) ) {
193 $this->curls
[$cacheUrl] = curl_init();
195 return $this->curls
[$cacheUrl];
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,
214 CURLOPT_CUSTOMREQUEST
=> 'PUT',
215 CURLOPT_POSTFIELDS
=> $data,
216 CURLOPT_HTTPHEADER
=> array(
217 'Content-Type: ' . $type,
218 'ehcacheTimeToLiveSeconds: ' . $ttl
225 return $response['http_code'];
233 protected function createCache( $key ) {
234 wfDebug( __METHOD__
.": creating cache for $key\n" );
235 $response = $this->doCacheRequest( $key,
238 CURLOPT_CUSTOMREQUEST
=> 'PUT',
239 CURLOPT_POSTFIELDS
=> '',
242 wfDebug( __CLASS__
.": failed to create cache for $key\n" );
245 return ( $response['http_code'] == 201 /* created */
246 ||
$response['http_code'] == 409 /* already there */ );
251 * @param $curlOptions array
252 * @return array|bool|mixed
254 protected function doCacheRequest( $key, $curlOptions = array() ) {
255 $cacheUrl = $this->getCacheUrl( $key );
256 $curl = $this->getCurl( $cacheUrl );
257 return $this->doRequest( $curl, $cacheUrl, $curlOptions );
262 * @param $curlOptions array
263 * @return array|bool|mixed
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 );
275 * @param $curlOptions array
276 * @return array|bool|mixed
277 * @throws MWException
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." );
286 $curlOptions +
= $this->curlOptions
;
287 $curlOptions[CURLOPT_URL
] = $url;
289 curl_setopt_array( $curl, $curlOptions );
290 $result = curl_exec( $curl );
291 if ( $result === false ) {
292 wfDebug( __CLASS__
.": curl error: " . curl_error( $curl ) . "\n" );
295 $info = curl_getinfo( $curl );
296 $info['body'] = $result;