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;
36 function __construct( $params ) {
37 if ( !defined( 'CURLOPT_TIMEOUT_MS' ) ) {
38 throw new MWException( __CLASS__
.' requires curl version 7.16.2 or later.' );
40 if ( !extension_loaded( 'zlib' ) ) {
41 throw new MWException( __CLASS__
.' requires the zlib extension' );
43 if ( !isset( $params['servers'] ) ) {
44 throw new MWException( __METHOD__
.': servers parameter is required' );
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',
57 CURLOPT_POSTFIELDS
=> '',
58 CURLOPT_HTTPHEADER
=> array(),
62 public function get( $key ) {
63 wfProfileIn( __METHOD__
);
64 $response = $this->doItemRequest( $key );
65 if ( !$response ||
$response['http_code'] == 404 ) {
66 wfProfileOut( __METHOD__
);
69 if ( $response['http_code'] >= 300 ) {
70 wfDebug( __METHOD__
.": GET failure, got HTTP {$response['http_code']}\n" );
71 wfProfileOut( __METHOD__
);
74 $body = $response['body'];
75 $type = $response['content_type'];
76 if ( $type == 'application/vnd.php.serialized+deflate' ) {
77 $body = gzinflate( $body );
79 wfDebug( __METHOD__
.": error inflating $key\n" );
80 wfProfileOut( __METHOD__
);
83 $data = unserialize( $body );
84 } elseif ( $type == 'application/vnd.php.serialized' ) {
85 $data = unserialize( $body );
87 wfDebug( __METHOD__
.": unknown content type \"$type\"\n" );
88 wfProfileOut( __METHOD__
);
92 wfProfileOut( __METHOD__
);
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';
105 $contentType = 'application/vnd.php.serialized';
108 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
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__
);
117 $code = $this->attemptPut( $key, $blob, $contentType, $ttl );
122 wfDebug( __METHOD__
.": PUT failure for key $key\n" );
123 } elseif ( $code >= 300 ) {
124 wfDebug( __METHOD__
.": PUT failure for key $key: HTTP $code\n" );
129 wfProfileOut( __METHOD__
);
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" );
144 wfProfileOut( __METHOD__
);
148 protected function getCacheUrl( $key ) {
149 if ( count( $this->servers
) == 1 ) {
150 $server = reset( $this->servers
);
152 // Use consistent hashing
154 foreach ( $this->servers
as $server ) {
155 $hashes[$server] = md5( $server . '/' . $key );
159 $server = key( $hashes );
161 return "http://$server/ehcache/rest/{$this->cacheName}";
165 * Get a cURL handle for the given cache URL.
166 * We cache the handles to allow keepalive.
168 protected function getCurl( $cacheUrl ) {
169 if ( !isset( $this->curls
[$cacheUrl] ) ) {
170 $this->curls
[$cacheUrl] = curl_init();
172 return $this->curls
[$cacheUrl];
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,
184 CURLOPT_CUSTOMREQUEST
=> 'PUT',
185 CURLOPT_POSTFIELDS
=> $data,
186 CURLOPT_HTTPHEADER
=> array(
187 'Content-Type: ' . $type,
188 'ehcacheTimeToLiveSeconds: ' . $ttl
195 return $response['http_code'];
199 protected function createCache( $key ) {
200 wfDebug( __METHOD__
.": creating cache for $key\n" );
201 $response = $this->doCacheRequest( $key,
204 CURLOPT_CUSTOMREQUEST
=> 'PUT',
205 CURLOPT_POSTFIELDS
=> '',
208 wfDebug( __CLASS__
.": failed to create cache for $key\n" );
211 if ( $response['http_code'] == 201 /* created */
212 ||
$response['http_code'] == 409 /* already there */ )
220 protected function doCacheRequest( $key, $curlOptions = array() ) {
221 $cacheUrl = $this->getCacheUrl( $key );
222 $curl = $this->getCurl( $cacheUrl );
223 return $this->doRequest( $curl, $cacheUrl, $curlOptions );
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 );
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." );
240 $curlOptions +
= $this->curlOptions
;
241 $curlOptions[CURLOPT_URL
] = $url;
243 curl_setopt_array( $curl, $curlOptions );
244 $result = curl_exec( $curl );
245 if ( $result === false ) {
246 wfDebug( __CLASS__
.": curl error: " . curl_error( $curl ) . "\n" );
249 $info = curl_getinfo( $curl );
250 $info['body'] = $result;