use Wikimedia\Rdbms\ResultWrapper;
use Wikimedia\Rdbms\FakeResultWrapper;
use Wikimedia\Rdbms\IDatabase;
+use MediaWiki\MediaWikiServices;
/**
* Class for fetching backlink lists, approximate backlink counts and
*/
protected $fullResultCache = [];
+ /**
+ * @var WANObjectCache
+ */
+ protected $wanCache;
+
/**
* Local copy of a database object.
*
*/
public function __construct( Title $title ) {
$this->title = $title;
+ $this->wanCache = MediaWikiServices::getInstance()->getMainWANObjectCache();
}
/**
}
/**
- * Clear locally stored data and database object.
+ * Clear locally stored data and database object. Invalidate data in memcache.
*/
public function clear() {
$this->partitionCache = [];
$this->fullResultCache = [];
+ $this->wanCache->touchCheckKey( $this->makeCheckKey() );
unset( $this->db );
}
public function getNumLinks( $table, $max = INF ) {
global $wgUpdateRowsPerJob;
- $cache = ObjectCache::getMainWANInstance();
// 1) try partition cache ...
if ( isset( $this->partitionCache[$table] ) ) {
$entry = reset( $this->partitionCache[$table] );
return min( $max, $this->fullResultCache[$table]->numRows() );
}
- $memcKey = $cache->makeKey(
+ $memcKey = $this->wanCache->makeKey(
'numbacklinks',
md5( $this->title->getPrefixedDBkey() ),
$table
);
// 3) ... fallback to memcached ...
- $count = $cache->get( $memcKey );
- if ( $count ) {
+ $curTTL = INF;
+ $count = $this->wanCache->get(
+ $memcKey,
+ $curTTL,
+ [
+ $this->makeCheckKey()
+ ]
+ );
+ if ( $count && ( $curTTL > 0 ) ) {
return min( $max, $count );
}
// Fetch the full title info, since the caller will likely need it next
$count = $this->getLinks( $table, false, false, $max )->count();
if ( $count < $max ) { // full count
- $cache->set( $memcKey, $count, self::CACHE_EXPIRY );
+ $this->wanCache->set( $memcKey, $count, self::CACHE_EXPIRY );
}
}
return $this->partitionCache[$table][$batchSize]['batches'];
}
- $cache = ObjectCache::getMainWANInstance();
$this->partitionCache[$table][$batchSize] = false;
$cacheEntry =& $this->partitionCache[$table][$batchSize];
return $cacheEntry['batches'];
}
- $memcKey = $cache->makeKey(
+ $memcKey = $this->wanCache->makeKey(
'backlinks',
md5( $this->title->getPrefixedDBkey() ),
$table,
);
// 3) ... fallback to memcached ...
- $memcValue = $cache->get( $memcKey );
- if ( is_array( $memcValue ) ) {
+ $curTTL = 0;
+ $memcValue = $this->wanCache->get(
+ $memcKey,
+ $curTTL,
+ [
+ $this->makeCheckKey()
+ ]
+ );
+ if ( is_array( $memcValue ) && ( $curTTL > 0 ) ) {
$cacheEntry = $memcValue;
wfDebug( __METHOD__ . ": got from memcached $memcKey\n" );
}
// Save partitions to memcached
- $cache->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
+ $this->wanCache->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
// Save backlink count to memcached
- $memcKey = $cache->makeKey(
+ $memcKey = $this->wanCache->makeKey(
'numbacklinks',
md5( $this->title->getPrefixedDBkey() ),
$table
);
- $cache->set( $memcKey, $cacheEntry['numRows'], self::CACHE_EXPIRY );
+ $this->wanCache->set( $memcKey, $cacheEntry['numRows'], self::CACHE_EXPIRY );
wfDebug( __METHOD__ . ": got from database\n" );
return TitleArray::newFromResult(
new FakeResultWrapper( array_values( $mergedRes ) ) );
}
+
+ /**
+ * Returns check key for the backlinks cache for a particular title
+ *
+ * @return String
+ */
+ private function makeCheckKey() {
+ return $this->wanCache->makeKey(
+ 'backlinks',
+ md5( $this->title->getPrefixedDBkey() )
+ );
+ }
}