/** @var int */
protected $lastExpireAll = 0;
/** @var int */
- protected $purgePeriod = 100;
+ protected $purgePeriod = 10;
+ /** @var int */
+ protected $purgeLimit = 100;
/** @var int */
protected $shards = 1;
/** @var string */
* when a cluster is replicated to another site (with different host names)
* but each server has a corresponding replica in the other cluster.
*
- * - purgePeriod: The average number of object cache requests in between
+ * - purgePeriod: The average number of object cache writes in between
* garbage collection operations, where expired entries
* are removed from the database. Or in other words, the
* reciprocal of the probability of purging on any given
- * request. If this is set to zero, purging will never be
- * done.
+ * write. If this is set to zero, purging will never be done.
+ *
+ * - purgeLimit: Maximum number of rows to purge at once.
*
* - tableName: The table name to use, default is "objectcache".
*
if ( isset( $params['purgePeriod'] ) ) {
$this->purgePeriod = intval( $params['purgePeriod'] );
}
+ if ( isset( $params['purgeLimit'] ) ) {
+ $this->purgeLimit = intval( $params['purgeLimit'] );
+ }
if ( isset( $params['tableName'] ) ) {
$this->tableName = $params['tableName'];
}
return false;
}
- public function getMulti( array $keys, $flags = 0 ) {
+ protected function doGetMulti( array $keys, $flags = 0 ) {
$values = [];
$blobs = $this->fetchBlobMulti( $keys );
return $values;
}
- public function fetchBlobMulti( array $keys, $flags = 0 ) {
+ protected function fetchBlobMulti( array $keys, $flags = 0 ) {
$values = []; // array of (key => value)
$keysByTable = [];
$keysByTable[$serverIndex][$tableName][] = $key;
}
- $this->garbageCollect(); // expire old entries if any
-
$dataRows = [];
foreach ( $keysByTable as $serverIndex => $serverKeys ) {
try {
$result = true;
$exptime = (int)$expiry;
+ /** @noinspection PhpUnusedLocalVariableInspection */
$silenceScope = $this->silenceTransactionProfiler();
foreach ( $keysByTable as $serverIndex => $serverKeys ) {
$db = null;
return $result;
}
- public function set( $key, $value, $exptime = 0, $flags = 0 ) {
- $ok = $this->setMulti( [ $key => $value ], $exptime );
+ protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
+ $ok = $this->insertMulti( [ $key => $value ], $exptime, $flags, true );
return $ok;
}
protected function cas( $casToken, $key, $value, $exptime = 0, $flags = 0 ) {
list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
$db = null;
+ /** @noinspection PhpUnusedLocalVariableInspection */
$silenceScope = $this->silenceTransactionProfiler();
try {
$db = $this->getDB( $serverIndex );
}
public function deleteMulti( array $keys, $flags = 0 ) {
+ return $this->purgeMulti( $keys, $flags );
+ }
+
+ public function purgeMulti( array $keys, $flags = 0 ) {
$keysByTable = [];
foreach ( $keys as $key ) {
list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
}
$result = true;
+ /** @noinspection PhpUnusedLocalVariableInspection */
$silenceScope = $this->silenceTransactionProfiler();
foreach ( $keysByTable as $serverIndex => $serverKeys ) {
$db = null;
return $result;
}
- public function delete( $key, $flags = 0 ) {
- $ok = $this->deleteMulti( [ $key ], $flags );
+ protected function doDelete( $key, $flags = 0 ) {
+ $ok = $this->purgeMulti( [ $key ], $flags );
return $ok;
}
public function incr( $key, $step = 1 ) {
list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
$db = null;
+ /** @noinspection PhpUnusedLocalVariableInspection */
$silenceScope = $this->silenceTransactionProfiler();
try {
$db = $this->getDB( $serverIndex );
public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
list( $serverIndex, $tableName ) = $this->getTableByKey( $key );
$db = null;
+ /** @noinspection PhpUnusedLocalVariableInspection */
$silenceScope = $this->silenceTransactionProfiler();
try {
$db = $this->getDB( $serverIndex );
// Disabled
return;
}
- // Only purge on one in every $this->purgePeriod requests.
+ // Only purge on one in every $this->purgePeriod writes
if ( $this->purgePeriod !== 1 && mt_rand( 0, $this->purgePeriod - 1 ) ) {
return;
}
// Avoid repeating the delete within a few seconds
if ( $now > ( $this->lastExpireAll + 1 ) ) {
$this->lastExpireAll = $now;
- $this->expireAll();
+ $this->deleteObjectsExpiringBefore(
+ wfTimestamp( TS_MW, $now ),
+ false,
+ $this->purgeLimit
+ );
}
}
$this->deleteObjectsExpiringBefore( wfTimestampNow() );
}
- /**
- * Delete objects from the database which expire before a certain date.
- * @param string $timestamp
- * @param bool|callable $progressCallback
- * @return bool
- */
- public function deleteObjectsExpiringBefore( $timestamp, $progressCallback = false ) {
+ public function deleteObjectsExpiringBefore(
+ $timestamp,
+ $progressCallback = false,
+ $limit = INF
+ ) {
+ /** @noinspection PhpUnusedLocalVariableInspection */
$silenceScope = $this->silenceTransactionProfiler();
+
+ $count = 0;
for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
$db = null;
try {
[ 'keyname', 'exptime' ],
$conds,
__METHOD__,
- [ 'LIMIT' => 100, 'ORDER BY' => 'exptime' ] );
+ [ 'LIMIT' => 100, 'ORDER BY' => 'exptime' ]
+ );
if ( $rows === false || !$rows->numRows() ) {
break;
}
'exptime < ' . $db->addQuotes( $dbTimestamp ),
'keyname' => $keys
],
- __METHOD__ );
+ __METHOD__
+ );
+ $count += $db->affectedRows();
+ if ( $count >= $limit ) {
+ return true;
+ }
- if ( $progressCallback ) {
+ if ( is_callable( $progressCallback ) ) {
if ( intval( $totalSeconds ) === 0 ) {
$percent = 0;
} else {
return false;
}
}
+
return true;
}
* @return bool
*/
public function deleteAll() {
+ /** @noinspection PhpUnusedLocalVariableInspection */
$silenceScope = $this->silenceTransactionProfiler();
for ( $serverIndex = 0; $serverIndex < $this->numServers; $serverIndex++ ) {
$db = null;
return true;
}
+ public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
+ // Avoid deadlocks and allow lock reentry if specified
+ if ( isset( $this->locks[$key] ) ) {
+ if ( $rclass != '' && $this->locks[$key]['class'] === $rclass ) {
+ ++$this->locks[$key]['depth'];
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ list( $serverIndex ) = $this->getTableByKey( $key );
+ try {
+ $db = $this->getDB( $serverIndex );
+ $ok = $db->lock( $key, __METHOD__, $timeout );
+ if ( $ok ) {
+ $this->locks[$key] = [ 'class' => $rclass, 'depth' => 1 ];
+ }
+
+ $this->logger->warning(
+ __METHOD__ . " failed due to timeout for {key}.",
+ [ 'key' => $key, 'timeout' => $timeout ]
+ );
+
+ return $ok;
+ } catch ( DBError $e ) {
+ $this->handleWriteError( $e, $db, $serverIndex );
+ $ok = false;
+ }
+
+ return $ok;
+ }
+
+ public function unlock( $key ) {
+ if ( !isset( $this->locks[$key] ) ) {
+ return false;
+ }
+
+ if ( --$this->locks[$key]['depth'] <= 0 ) {
+ unset( $this->locks[$key] );
+
+ list( $serverIndex ) = $this->getTableByKey( $key );
+ try {
+ $db = $this->getDB( $serverIndex );
+ $ok = $db->unlock( $key, __METHOD__ );
+ if ( !$ok ) {
+ $this->logger->warning(
+ __METHOD__ . ' failed to release lock for {key}.',
+ [ 'key' => $key ]
+ );
+ }
+ } catch ( DBError $e ) {
+ $this->handleWriteError( $e, $db, $serverIndex );
+ $ok = false;
+ }
+ } else {
+ $ok = false;
+ }
+
+ return $ok;
+ }
+
/**
* Serialize an object and, if possible, compress the representation.
* On typical message and page data, this can provide a 3X decrease
* in storage requirements.
*
- * @param mixed &$data
+ * @param mixed $data
* @return string
*/
- protected function serialize( &$data ) {
+ protected function serialize( $data ) {
$serial = serialize( $data );
if ( function_exists( 'gzdeflate' ) ) {
* @param int $serverIndex
* @throws Exception
*/
- protected function handleWriteError( DBError $exception, IDatabase $db = null, $serverIndex ) {
- if ( !$db ) {
+ protected function handleWriteError( DBError $exception, $db, $serverIndex ) {
+ if ( !( $db instanceof IDatabase ) ) {
$this->markServerDown( $exception, $serverIndex );
}