summaryrefslogtreecommitdiff
path: root/includes/objectcache/EhcacheBagOStuff.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/objectcache/EhcacheBagOStuff.php')
-rw-r--r--includes/objectcache/EhcacheBagOStuff.php72
1 files changed, 49 insertions, 23 deletions
diff --git a/includes/objectcache/EhcacheBagOStuff.php b/includes/objectcache/EhcacheBagOStuff.php
index f86cf157..960668f5 100644
--- a/includes/objectcache/EhcacheBagOStuff.php
+++ b/includes/objectcache/EhcacheBagOStuff.php
@@ -28,27 +28,28 @@
* @ingroup Cache
*/
class EhcacheBagOStuff extends BagOStuff {
- var $servers, $cacheName, $connectTimeout, $timeout, $curlOptions,
+ var $servers, $cacheName, $connectTimeout, $timeout, $curlOptions,
$requestData, $requestDataPos;
-
+
var $curls = array();
/**
* @param $params array
+ * @throws MWException
*/
function __construct( $params ) {
if ( !defined( 'CURLOPT_TIMEOUT_MS' ) ) {
- throw new MWException( __CLASS__.' requires curl version 7.16.2 or later.' );
+ throw new MWException( __CLASS__ . ' requires curl version 7.16.2 or later.' );
}
if ( !extension_loaded( 'zlib' ) ) {
- throw new MWException( __CLASS__.' requires the zlib extension' );
+ throw new MWException( __CLASS__ . ' requires the zlib extension' );
}
if ( !isset( $params['servers'] ) ) {
- throw new MWException( __METHOD__.': servers parameter is required' );
+ throw new MWException( __METHOD__ . ': servers parameter is required' );
}
$this->servers = $params['servers'];
$this->cacheName = isset( $params['cache'] ) ? $params['cache'] : 'mw';
- $this->connectTimeout = isset( $params['connectTimeout'] )
+ $this->connectTimeout = isset( $params['connectTimeout'] )
? $params['connectTimeout'] : 1;
$this->timeout = isset( $params['timeout'] ) ? $params['timeout'] : 1;
$this->curlOptions = array(
@@ -64,9 +65,10 @@ class EhcacheBagOStuff extends BagOStuff {
/**
* @param $key string
+ * @param $casToken[optional] mixed
* @return bool|mixed
*/
- public function get( $key ) {
+ public function get( $key, &$casToken = null ) {
wfProfileIn( __METHOD__ );
$response = $this->doItemRequest( $key );
if ( !$response || $response['http_code'] == 404 ) {
@@ -74,16 +76,16 @@ class EhcacheBagOStuff extends BagOStuff {
return false;
}
if ( $response['http_code'] >= 300 ) {
- wfDebug( __METHOD__.": GET failure, got HTTP {$response['http_code']}\n" );
+ wfDebug( __METHOD__ . ": GET failure, got HTTP {$response['http_code']}\n" );
wfProfileOut( __METHOD__ );
- return false;
+ return false;
}
$body = $response['body'];
$type = $response['content_type'];
if ( $type == 'application/vnd.php.serialized+deflate' ) {
$body = gzinflate( $body );
if ( !$body ) {
- wfDebug( __METHOD__.": error inflating $key\n" );
+ wfDebug( __METHOD__ . ": error inflating $key\n" );
wfProfileOut( __METHOD__ );
return false;
}
@@ -91,11 +93,13 @@ class EhcacheBagOStuff extends BagOStuff {
} elseif ( $type == 'application/vnd.php.serialized' ) {
$data = unserialize( $body );
} else {
- wfDebug( __METHOD__.": unknown content type \"$type\"\n" );
+ wfDebug( __METHOD__ . ": unknown content type \"$type\"\n" );
wfProfileOut( __METHOD__ );
return false;
}
+ $casToken = $body;
+
wfProfileOut( __METHOD__ );
return $data;
}
@@ -123,7 +127,7 @@ class EhcacheBagOStuff extends BagOStuff {
if ( $code == 404 ) {
// Maybe the cache does not exist yet, let's try creating it
if ( !$this->createCache( $key ) ) {
- wfDebug( __METHOD__.": cache creation failed\n" );
+ wfDebug( __METHOD__ . ": cache creation failed\n" );
wfProfileOut( __METHOD__ );
return false;
}
@@ -132,9 +136,9 @@ class EhcacheBagOStuff extends BagOStuff {
$result = false;
if ( !$code ) {
- wfDebug( __METHOD__.": PUT failure for key $key\n" );
+ wfDebug( __METHOD__ . ": PUT failure for key $key\n" );
} elseif ( $code >= 300 ) {
- wfDebug( __METHOD__.": PUT failure for key $key: HTTP $code\n" );
+ wfDebug( __METHOD__ . ": PUT failure for key $key: HTTP $code\n" );
} else {
$result = true;
}
@@ -144,6 +148,20 @@ class EhcacheBagOStuff extends BagOStuff {
}
/**
+ * @param $casToken mixed
+ * @param $key string
+ * @param $value mixed
+ * @param $exptime int
+ * @return bool
+ */
+ public function cas( $casToken, $key, $value, $exptime = 0 ) {
+ // Not sure if we can implement CAS for ehcache. There appears to be CAS-support per
+ // http://ehcache.org/documentation/get-started/consistency-options#cas-cache-operations,
+ // but I can't find any docs for our current implementation.
+ throw new MWException( "CAS is not implemented in " . __CLASS__ );
+ }
+
+ /**
* @param $key string
* @param $time int
* @return bool
@@ -154,7 +172,7 @@ class EhcacheBagOStuff extends BagOStuff {
array( CURLOPT_CUSTOMREQUEST => 'DELETE' ) );
$code = isset( $response['http_code'] ) ? $response['http_code'] : 0;
if ( !$response || ( $code != 404 && $code >= 300 ) ) {
- wfDebug( __METHOD__.": DELETE failure for key $key\n" );
+ wfDebug( __METHOD__ . ": DELETE failure for key $key\n" );
$result = false;
} else {
$result = true;
@@ -164,6 +182,14 @@ class EhcacheBagOStuff extends BagOStuff {
}
/**
+ * @see BagOStuff::merge()
+ * @return bool success
+ */
+ public function merge( $key, closure $callback, $exptime = 0, $attempts = 10 ) {
+ return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
+ }
+
+ /**
* @param $key string
* @return string
*/
@@ -202,9 +228,9 @@ class EhcacheBagOStuff extends BagOStuff {
* @return int
*/
protected function attemptPut( $key, $data, $type, $ttl ) {
- // In initial benchmarking, it was 30 times faster to use CURLOPT_POST
+ // In initial benchmarking, it was 30 times faster to use CURLOPT_POST
// than CURLOPT_UPLOAD with CURLOPT_READFUNCTION. This was because
- // CURLOPT_UPLOAD was pushing the request headers first, then waiting
+ // CURLOPT_UPLOAD was pushing the request headers first, then waiting
// for an ACK packet, then sending the data, whereas CURLOPT_POST just
// sends the headers and the data in a single send().
$response = $this->doItemRequest( $key,
@@ -230,15 +256,15 @@ class EhcacheBagOStuff extends BagOStuff {
* @return bool
*/
protected function createCache( $key ) {
- wfDebug( __METHOD__.": creating cache for $key\n" );
- $response = $this->doCacheRequest( $key,
+ wfDebug( __METHOD__ . ": creating cache for $key\n" );
+ $response = $this->doCacheRequest( $key,
array(
CURLOPT_POST => 1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => '',
) );
if ( !$response ) {
- wfDebug( __CLASS__.": failed to create cache for $key\n" );
+ wfDebug( __CLASS__ . ": failed to create cache for $key\n" );
return false;
}
return ( $response['http_code'] == 201 /* created */
@@ -278,8 +304,8 @@ class EhcacheBagOStuff extends BagOStuff {
protected function doRequest( $curl, $url, $curlOptions = array() ) {
if ( array_diff_key( $curlOptions, $this->curlOptions ) ) {
// var_dump( array_diff_key( $curlOptions, $this->curlOptions ) );
- throw new MWException( __METHOD__.": to prevent options set in one doRequest() " .
- "call from affecting subsequent doRequest() calls, only options listed " .
+ throw new MWException( __METHOD__ . ": to prevent options set in one doRequest() " .
+ "call from affecting subsequent doRequest() calls, only options listed " .
"in \$this->curlOptions may be specified in the \$curlOptions parameter." );
}
$curlOptions += $this->curlOptions;
@@ -288,7 +314,7 @@ class EhcacheBagOStuff extends BagOStuff {
curl_setopt_array( $curl, $curlOptions );
$result = curl_exec( $curl );
if ( $result === false ) {
- wfDebug( __CLASS__.": curl error: " . curl_error( $curl ) . "\n" );
+ wfDebug( __CLASS__ . ": curl error: " . curl_error( $curl ) . "\n" );
return false;
}
$info = curl_getinfo( $curl );