* @ingroup Cache
*/
class SqlBagOStuff extends BagOStuff {
- /**
- * @var LoadBalancer
- */
- var $lb;
+ /** @var LoadBalancer */
+ protected $lb;
+
+ protected $serverInfos;
+
+ /** @var array */
+ protected $serverNames;
+
+ /** @var int */
+ protected $numServers;
+
+ /** @var array */
+ protected $conns;
+
+ /** @var int */
+ protected $lastExpireAll = 0;
+
+ /** @var int */
+ protected $purgePeriod = 100;
- var $serverInfos;
- var $serverNames;
- var $numServers;
- var $conns;
- var $lastExpireAll = 0;
- var $purgePeriod = 100;
- var $shards = 1;
- var $tableName = 'objectcache';
+ /** @var int */
+ protected $shards = 1;
- protected $connFailureTimes = array(); // UNIX timestamps
- protected $connFailureErrors = array(); // exceptions
+ /** @var string */
+ protected $tableName = 'objectcache';
+
+ /** @var array UNIX timestamps */
+ protected $connFailureTimes = array();
+
+ /** @var array Exceptions */
+ protected $connFailureErrors = array();
/**
* Constructor. Parameters are:
* distributed across all tables by key hash. This is for
* MySQL bugs 61735 and 61736.
*
- * @param $params array
+ * @param array $params
*/
public function __construct( $params ) {
if ( isset( $params['servers'] ) ) {
/**
* Get a connection to the specified database
*
- * @param $serverIndex integer
+ * @param int $serverIndex
* @return DatabaseBase
*/
protected function getDB( $serverIndex ) {
/**
* Get the server index and table name for a given key
- * @param $key string
- * @return Array: server index and table name
+ * @param string $key
+ * @return array Server index and table name
*/
protected function getTableByKey( $key ) {
if ( $this->shards > 1 ) {
/**
* Get the table name for a given shard index
- * @param $index int
+ * @param int $index
* @return string
*/
protected function getTableNameByShard( $index ) {
}
/**
- * @param $key string
- * @param $casToken[optional] mixed
+ * @param string $key
+ * @param mixed $casToken [optional]
* @return mixed
*/
public function get( $key, &$casToken = null ) {
}
/**
- * @param $keys array
- * @return Array
+ * @param array $keys
+ * @return array
*/
public function getMulti( array $keys ) {
$values = array(); // array of (key => value)
}
/**
- * @param $key string
- * @param $value mixed
- * @param $exptime int
+ * @param array $data
+ * @param int $expiry
+ * @return bool
+ */
+ public function setMulti( array $data, $expiry = 0 ) {
+ $keysByTable = array();
+ foreach ( $data as $key => $value ) {
+ list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
+ $keysByTable[$serverIndex][$tableName][] = $key;
+ }
+
+ $this->garbageCollect(); // expire old entries if any
+
+ $result = true;
+ $exptime = (int)$expiry;
+ foreach ( $keysByTable as $serverIndex => $serverKeys ) {
+ try {
+ $db = $this->getDB( $serverIndex );
+ } catch ( DBError $e ) {
+ $this->handleWriteError( $e, $serverIndex );
+ $result = false;
+ continue;
+ }
+
+ if ( $exptime < 0 ) {
+ $exptime = 0;
+ }
+
+ if ( $exptime == 0 ) {
+ $encExpiry = $this->getMaxDateTime( $db );
+ } else {
+ if ( $exptime < 3.16e8 ) { # ~10 years
+ $exptime += time();
+ }
+ $encExpiry = $db->timestamp( $exptime );
+ }
+ foreach ( $serverKeys as $tableName => $tableKeys ) {
+ $rows = array();
+ foreach ( $tableKeys as $key ) {
+ $rows[] = array(
+ 'keyname' => $key,
+ 'value' => $db->encodeBlob( $this->serialize( $data[$key] ) ),
+ 'exptime' => $encExpiry,
+ );
+ }
+
+ try {
+ $db->commit( __METHOD__, 'flush' );
+ $db->replace(
+ $tableName,
+ array( 'keyname' ),
+ $rows,
+ __METHOD__
+ );
+ $db->commit( __METHOD__, 'flush' );
+ } catch ( DBError $e ) {
+ $this->handleWriteError( $e, $serverIndex );
+ $result = false;
+ }
+
+ }
+
+ }
+
+ return $result;
+ }
+
+
+
+ /**
+ * @param string $key
+ * @param mixed $value
+ * @param int $exptime
* @return bool
*/
public function set( $key, $value, $exptime = 0 ) {
}
/**
- * @param $casToken mixed
- * @param $key string
- * @param $value mixed
- * @param $exptime int
+ * @param mixed $casToken
+ * @param string $key
+ * @param mixed $value
+ * @param int $exptime
* @return bool
*/
public function cas( $casToken, $key, $value, $exptime = 0 ) {
}
/**
- * @param $key string
- * @param $time int
+ * @param string $key
+ * @param int $time
* @return bool
*/
public function delete( $key, $time = 0 ) {
}
/**
- * @param $key string
- * @param $step int
+ * @param string $key
+ * @param int $step
* @return int|null
*/
public function incr( $key, $step = 1 ) {
}
/**
- * @param $exptime string
+ * @param string $exptime
* @return bool
*/
protected function isExpired( $db, $exptime ) {
}
/**
+ * @param DatabaseBase $db
* @return string
*/
protected function getMaxDateTime( $db ) {
/**
* Delete objects from the database which expire before a certain date.
- * @param $timestamp string
- * @param $progressCallback bool|callback
+ * @param string $timestamp
+ * @param bool|callable $progressCallback
* @return bool
*/
public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
* On typical message and page data, this can provide a 3X decrease
* in storage requirements.
*
- * @param $data mixed
+ * @param mixed $data
* @return string
*/
protected function serialize( &$data ) {
/**
* Unserialize and, if necessary, decompress an object.
- * @param $serial string
+ * @param string $serial
* @return mixed
*/
protected function unserialize( $serial ) {
/**
* Handle a DBError which occurred during a read operation.
+ *
+ * @param DBError $exception
+ * @param int $serverIndex
*/
protected function handleReadError( DBError $exception, $serverIndex ) {
if ( $exception instanceof DBConnectionError ) {
}
wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
if ( $exception instanceof DBConnectionError ) {
+ $this->setLastError( BagOStuff::ERR_UNREACHABLE );
wfDebug( __METHOD__ . ": ignoring connection error\n" );
} else {
+ $this->setLastError( BagOStuff::ERR_UNEXPECTED );
wfDebug( __METHOD__ . ": ignoring query error\n" );
}
}
/**
* Handle a DBQueryError which occurred during a write operation.
+ *
+ * @param DBError $exception
+ * @param int $serverIndex
*/
protected function handleWriteError( DBError $exception, $serverIndex ) {
if ( $exception instanceof DBConnectionError ) {
if ( $exception->db && $exception->db->wasReadOnlyError() ) {
try {
$exception->db->rollback( __METHOD__ );
- } catch ( DBError $e ) {}
+ } catch ( DBError $e ) {
+ }
}
wfDebugLog( 'SQLBagOStuff', "DBError: {$exception->getMessage()}" );
if ( $exception instanceof DBConnectionError ) {
+ $this->setLastError( BagOStuff::ERR_UNREACHABLE );
wfDebug( __METHOD__ . ": ignoring connection error\n" );
} else {
+ $this->setLastError( BagOStuff::ERR_UNEXPECTED );
wfDebug( __METHOD__ . ": ignoring query error\n" );
}
}
/**
* Mark a server down due to a DBConnectionError exception
+ *
+ * @param DBError $exception
+ * @param int $serverIndex
*/
protected function markServerDown( $exception, $serverIndex ) {
if ( isset( $this->connFailureTimes[$serverIndex] ) ) {
/**
* Backwards compatibility alias
*/
-class MediaWikiBagOStuff extends SqlBagOStuff { }
+class MediaWikiBagOStuff extends SqlBagOStuff {
+}