From 8baffeae4fea39837755e29302efc6018ac7254e Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Sun, 21 Oct 2018 05:07:44 -0700 Subject: [PATCH] filerepo: Inject the WAN cache info FileRepo instances Also fixed some intellij IDEA warnings. Change-Id: I667ed7ef9b49c1da1c8c7c9914daadb7c74b3c79 --- includes/filerepo/FileRepo.php | 13 +++++++++---- includes/filerepo/ForeignAPIRepo.php | 21 +++++++++------------ includes/filerepo/LocalRepo.php | 12 ++++++------ includes/filerepo/RepoGroup.php | 15 ++++++++++++--- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index 455d38f243..3225625b6f 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -137,6 +137,9 @@ class FileRepo { /** @var string Secret key to pass as an X-Swift-Secret header to the proxied thumb service */ protected $thumbProxySecret; + /** @var WANObjectCache */ + protected $wanCache; + /** * @param array|null $info * @throws MWException @@ -200,6 +203,8 @@ class FileRepo { } $this->supportsSha1URLs = !empty( $info['supportsSha1URLs'] ); + + $this->wanCache = $info['wanCache'] ?? WANObjectCache::newEmpty(); } /** @@ -1507,7 +1512,7 @@ class FileRepo { * @throws MWException */ protected function resolveToStoragePath( $path ) { - if ( $this->isVirtualUrl( $path ) ) { + if ( self::isVirtualUrl( $path ) ) { return $this->resolveVirtualUrl( $path ); } @@ -1818,7 +1823,7 @@ class FileRepo { /** * Get a key on the primary cache for this repository. * Returns false if the repository's cache is not accessible at this site. - * The parameters are the parts of the key, as for wfMemcKey(). + * The parameters are the parts of the key. * * STUB * @return bool @@ -1830,7 +1835,7 @@ class FileRepo { /** * Get a key for this repo in the local cache domain. These cache keys are * not shared with remote instances of the repo. - * The parameters are the parts of the key, as for wfMemcKey(). + * The parameters are the parts of the key. * * @return string */ @@ -1838,7 +1843,7 @@ class FileRepo { $args = func_get_args(); array_unshift( $args, 'filerepo', $this->getName() ); - return wfMemcKey( ...$args ); + return $this->wanCache->makeKey( ...$args ); } /** diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index c20df76e2b..9e4b6dfd71 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -22,7 +22,6 @@ */ use MediaWiki\Logger\LoggerFactory; -use MediaWiki\MediaWikiServices; /** * A foreign repository with a remote MediaWiki with an API thingy @@ -333,7 +332,6 @@ class ForeignAPIRepo extends FileRepo { * @return bool|string */ function getThumbUrlFromCache( $name, $width, $height, $params = "" ) { - $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); // We can't check the local cache using FileRepo functions because // we override fileExistsBatch(). We have to use the FileBackend directly. $backend = $this->getBackend(); // convenience @@ -346,7 +344,7 @@ class ForeignAPIRepo extends FileRepo { $sizekey = "$width:$height:$params"; /* Get the array of urls that we already know */ - $knownThumbUrls = $cache->get( $key ); + $knownThumbUrls = $this->wanCache->get( $key ); if ( !$knownThumbUrls ) { /* No knownThumbUrls for this file */ $knownThumbUrls = []; @@ -392,7 +390,7 @@ class ForeignAPIRepo extends FileRepo { if ( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) { /* Use our current and already downloaded thumbnail */ $knownThumbUrls[$sizekey] = $localUrl; - $cache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry ); + $this->wanCache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry ); return $localUrl; } @@ -417,9 +415,9 @@ class ForeignAPIRepo extends FileRepo { $knownThumbUrls[$sizekey] = $localUrl; $ttl = $mtime - ? $cache->adaptiveTTL( $mtime, $this->apiThumbCacheExpiry ) + ? $this->wanCache->adaptiveTTL( $mtime, $this->apiThumbCacheExpiry ) : $this->apiThumbCacheExpiry; - $cache->set( $key, $knownThumbUrls, $ttl ); + $this->wanCache->set( $key, $knownThumbUrls, $ttl ); wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" ); return $localUrl; @@ -570,22 +568,21 @@ class ForeignAPIRepo extends FileRepo { $url = $this->makeUrl( $query, 'api' ); } - $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); - return $cache->getWithSetCallback( + return $this->wanCache->getWithSetCallback( $this->getLocalCacheKey( static::class, $target, md5( $url ) ), $cacheTTL, - function ( $curValue, &$ttl ) use ( $url, $cache ) { + function ( $curValue, &$ttl ) use ( $url ) { $html = self::httpGet( $url, 'default', [], $mtime ); if ( $html !== false ) { - $ttl = $mtime ? $cache->adaptiveTTL( $mtime, $ttl ) : $ttl; + $ttl = $mtime ? $this->wanCache->adaptiveTTL( $mtime, $ttl ) : $ttl; } else { - $ttl = $cache->adaptiveTTL( $mtime, $ttl ); + $ttl = $this->wanCache->adaptiveTTL( $mtime, $ttl ); $html = null; // caches negatives } return $html; }, - [ 'pcTTL' => $cache::TTL_PROC_LONG ] + [ 'pcTTL' => WANObjectCache::TTL_PROC_LONG ] ); } diff --git a/includes/filerepo/LocalRepo.php b/includes/filerepo/LocalRepo.php index c889e56723..b3eae900ee 100644 --- a/includes/filerepo/LocalRepo.php +++ b/includes/filerepo/LocalRepo.php @@ -23,7 +23,7 @@ */ use MediaWiki\MediaWikiServices; -use Wikimedia\Rdbms\ResultWrapper; +use Wikimedia\Rdbms\IResultWrapper; use Wikimedia\Rdbms\Database; use Wikimedia\Rdbms\IDatabase; @@ -201,7 +201,7 @@ class LocalRepo extends FileRepo { } $method = __METHOD__; - $redirDbKey = MediaWikiServices::getInstance()->getMainWANObjectCache()->getWithSetCallback( + $redirDbKey = $this->wanCache->getWithSetCallback( $memcKey, $expiry, function ( $oldValue, &$ttl, array &$setOpts ) use ( $method, $title ) { @@ -275,7 +275,7 @@ class LocalRepo extends FileRepo { ); }; - $applyMatchingFiles = function ( ResultWrapper $res, &$searchSet, &$finalFiles ) + $applyMatchingFiles = function ( IResultWrapper $res, &$searchSet, &$finalFiles ) use ( $fileMatchesSearch, $flags ) { $contLang = MediaWikiServices::getInstance()->getContentLanguage(); @@ -500,14 +500,14 @@ class LocalRepo extends FileRepo { /** * Get a key on the primary cache for this repository. * Returns false if the repository's cache is not accessible at this site. - * The parameters are the parts of the key, as for wfMemcKey(). + * The parameters are the parts of the key. * * @return string */ function getSharedCacheKey( /*...*/ ) { $args = func_get_args(); - return wfMemcKey( ...$args ); + return $this->wanCache->makeKey( ...$args ); } /** @@ -521,7 +521,7 @@ class LocalRepo extends FileRepo { if ( $key ) { $this->getMasterDB()->onTransactionPreCommitOrIdle( function () use ( $key ) { - MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key ); + $this->wanCache->delete( $key ); }, __METHOD__ ); diff --git a/includes/filerepo/RepoGroup.php b/includes/filerepo/RepoGroup.php index f3fed5772d..b6c70ab65e 100644 --- a/includes/filerepo/RepoGroup.php +++ b/includes/filerepo/RepoGroup.php @@ -21,6 +21,8 @@ * @ingroup FileRepo */ +use MediaWiki\MediaWikiServices; + /** * Prioritized list of file repositories * @@ -61,6 +63,7 @@ class RepoGroup { return self::$instance; } global $wgLocalFileRepo, $wgForeignFileRepos; + /** @var array $wgLocalFileRepo */ self::$instance = new RepoGroup( $wgLocalFileRepo, $wgForeignFileRepos ); return self::$instance; @@ -164,7 +167,7 @@ class RepoGroup { } } - $image = $image ?: false; // type sanity + $image = $image instanceof File ? $image : false; // type sanity # Cache file existence or non-existence if ( $useCache && ( !$image || $image->isCacheable() ) ) { $this->cache->setField( $dbkey, $timeKey, $image ); @@ -317,7 +320,7 @@ class RepoGroup { /** * Get the repo instance with a given key. * @param string|int $index - * @return bool|LocalRepo + * @return bool|FileRepo */ function getRepo( $index ) { if ( !$this->reposInitialised ) { @@ -354,7 +357,10 @@ class RepoGroup { * @return LocalRepo */ function getLocalRepo() { - return $this->getRepo( 'local' ); + /** @var LocalRepo $repo */ + $repo = $this->getRepo( 'local' ); + + return $repo; } /** @@ -413,6 +419,9 @@ class RepoGroup { protected function newRepo( $info ) { $class = $info['class']; + $cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); + $info['wanCache'] = $cache; + return new $class( $info ); } -- 2.20.1