Convert UserCache to a service

Bug: T253061
Change-Id: Id951177712648dc5ac684ae38097a0e9f3e12c7a
This commit is contained in:
DannyS712 2020-05-18 20:12:34 +00:00
parent b1a3a8149d
commit cc464b2028
3 changed files with 56 additions and 10 deletions

View file

@ -109,6 +109,7 @@ use TitleFactory;
use TitleFormatter;
use TitleParser;
use UploadRevisionImporter;
use UserCache;
use VirtualRESTServiceClient;
use WANObjectCache;
use WatchedItemQueryService;
@ -1340,6 +1341,14 @@ class MediaWikiServices extends ServiceContainer {
return $this->getService( 'UploadRevisionImporter' );
}
/**
* @since 1.36
* @return UserCache
*/
public function getUserCache() : UserCache {
return $this->getService( 'UserCache' );
}
/**
* @since 1.35
* @return UserEditTracker

View file

@ -1336,6 +1336,14 @@ return [
);
},
'UserCache' => function ( MediaWikiServices $services ) : UserCache {
return new UserCache(
LoggerFactory::getInstance( 'UserCache' ),
$services->getDBLoadBalancer(),
$services->getLinkBatchFactory()
);
},
'UserEditTracker' => function ( MediaWikiServices $services ) : UserEditTracker {
$jobQueueGroup = JobQueueGroup::singleton();

View file

@ -21,6 +21,11 @@
* @ingroup Cache
*/
use MediaWiki\Cache\LinkBatchFactory;
use MediaWiki\MediaWikiServices;
use Psr\Log\LoggerInterface;
use Wikimedia\Rdbms\ILoadBalancer;
/**
* @since 1.20
*/
@ -28,19 +33,37 @@ class UserCache {
protected $cache = []; // (uid => property => value)
protected $typesCached = []; // (uid => cache type => 1)
/** @var LoggerInterface */
private $logger;
/** @var LinkBatchFactory */
private $linkBatchFactory;
/** @var ILoadBalancer */
private $loadBalancer;
/**
* @return UserCache
*/
public static function singleton() {
static $instance = null;
if ( $instance === null ) {
$instance = new self();
}
return $instance;
return MediaWikiServices::getInstance()->getUserCache();
}
protected function __construct() {
/**
* Uses dependency injection since 1.36
*
* @param LoggerInterface $logger
* @param ILoadBalancer $loadBalancer
* @param LinkBatchFactory $linkBatchFactory
*/
public function __construct(
LoggerInterface $logger,
ILoadBalancer $loadBalancer,
LinkBatchFactory $linkBatchFactory
) {
$this->logger = $logger;
$this->loadBalancer = $loadBalancer;
$this->linkBatchFactory = $linkBatchFactory;
}
/**
@ -52,7 +75,13 @@ class UserCache {
*/
public function getProp( $userId, $prop ) {
if ( !isset( $this->cache[$userId][$prop] ) ) {
wfDebug( __METHOD__ . ": querying DB for prop '$prop' for user ID '$userId'." );
$this->logger->debug(
'Querying DB for prop {prop} for user ID {userId}',
[
'prop' => $prop,
'userId' => $userId,
]
);
$this->doQuery( [ $userId ] ); // cache miss
}
@ -97,7 +126,7 @@ class UserCache {
// Lookup basic info for users not yet loaded...
if ( count( $usersToQuery ) ) {
$dbr = wfGetDB( DB_REPLICA );
$dbr = $this->loadBalancer->getConnection( DB_REPLICA );
$tables = [ 'user', 'actor' ];
$conds = [ 'user_id' => $usersToQuery ];
$fields = [ 'user_name', 'user_real_name', 'user_registration', 'user_id', 'actor_id' ];
@ -121,7 +150,7 @@ class UserCache {
}
}
$lb = new LinkBatch();
$lb = $this->linkBatchFactory->newLinkBatch();
foreach ( $usersToCheck as $userId => $name ) {
if ( $this->queryNeeded( $userId, 'userpage', $options ) ) {
$lb->add( NS_USER, $name );