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(),
68 * @param $casToken[optional] 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__
);
78 if ( $response['http_code'] >= 300 ) {
79 wfDebug( __METHOD__
. ": GET failure, got HTTP {$response['http_code']}\n" );
80 wfProfileOut( __METHOD__
);
83 $body = $response['body'];
84 $type = $response['content_type'];
85 if ( $type == 'application/vnd.php.serialized+deflate' ) {
86 $body = gzinflate( $body );
88 wfDebug( __METHOD__
. ": error inflating $key\n" );
89 wfProfileOut( __METHOD__
);
92 $data = unserialize( $body );
93 } elseif ( $type == 'application/vnd.php.serialized' ) {
94 $data = unserialize( $body );
96 wfDebug( __METHOD__
. ": unknown content type \"$type\"\n" );
97 wfProfileOut( __METHOD__
);
103 wfProfileOut( __METHOD__
);
109 * @param $value mixed
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';
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__
);
134 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
139 wfDebug( __METHOD__
. ": PUT failure for key $key\n" );
140 } elseif ( $code >= 300 ) {
141 wfDebug( __METHOD__
. ": PUT failure for key $key: HTTP $code\n" );
146 wfProfileOut( __METHOD__
);
151 * @param $casToken mixed
153 * @param $value mixed
154 * @param $exptime int
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__
);
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" );
180 wfProfileOut( __METHOD__
);
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 );
196 protected function getCacheUrl( $key ) {
197 if ( count( $this->servers
) == 1 ) {
198 $server = reset( $this->servers
);
200 // Use consistent hashing
202 foreach ( $this->servers
as $server ) {
203 $hashes[$server] = md5( $server . '/' . $key );
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];
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,
239 CURLOPT_CUSTOMREQUEST
=> 'PUT',
240 CURLOPT_POSTFIELDS
=> $data,
241 CURLOPT_HTTPHEADER
=> array(
242 'Content-Type: ' . $type,
243 'ehcacheTimeToLiveSeconds: ' . $ttl
250 return $response['http_code'];
258 protected function createCache( $key ) {
259 wfDebug( __METHOD__
. ": creating cache for $key\n" );
260 $response = $this->doCacheRequest( $key,
263 CURLOPT_CUSTOMREQUEST
=> 'PUT',
264 CURLOPT_POSTFIELDS
=> '',
267 wfDebug( __CLASS__
. ": failed to create cache for $key\n" );
270 return ( $response['http_code'] == 201 /* created */
271 ||
$response['http_code'] == 409 /* already there */ );
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 );
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 );
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" );
320 $info = curl_getinfo( $curl );
321 $info['body'] = $result;