mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/PageImages
synced 2024-11-15 03:43:46 +00:00
Add in-process cache for result of getPageImage() DB query
There has been cases of different extensions calling this function for the same page title within the same web request, leading to duplicate queries and roundtrips. This avoids those. Maybe in future we could have APCu cache for it. Bug: T322528 Change-Id: I9b08b4de2648bf794bfdbfe57de9db433cfd79ee
This commit is contained in:
parent
d7d9c0275f
commit
bb555ae71d
|
@ -7,7 +7,9 @@ use ApiMain;
|
|||
use FauxRequest;
|
||||
use File;
|
||||
use IContextSource;
|
||||
use MapCacheLRU;
|
||||
use MediaWiki\Api\Hook\ApiOpenSearchSuggestHook;
|
||||
use MediaWiki\Cache\CacheKeyHelper;
|
||||
use MediaWiki\Hook\BeforePageDisplayHook;
|
||||
use MediaWiki\Hook\InfoActionHook;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
|
@ -59,6 +61,9 @@ class PageImages implements
|
|||
/** @var UserOptionsLookup */
|
||||
private $userOptionsLookup;
|
||||
|
||||
/** @var MapCacheLRU */
|
||||
private static $cache = null;
|
||||
|
||||
/**
|
||||
* @param UserOptionsLookup $userOptionsLookup
|
||||
*/
|
||||
|
@ -104,6 +109,13 @@ class PageImages implements
|
|||
* @return File|bool
|
||||
*/
|
||||
public static function getPageImage( Title $title ) {
|
||||
$cacheKey = CacheKeyHelper::getKeyForPage( $title );
|
||||
if ( self::$cache === null ) {
|
||||
self::$cache = new MapCacheLRU( 100 );
|
||||
}
|
||||
if ( self::$cache->has( $cacheKey ) ) {
|
||||
return self::$cache->get( $cacheKey );
|
||||
}
|
||||
// Do not query for special pages or other titles never in the database
|
||||
if ( !$title->canExist() ) {
|
||||
return false;
|
||||
|
@ -115,6 +127,7 @@ class PageImages implements
|
|||
|
||||
if ( !$title->exists() ) {
|
||||
// No page id to select from
|
||||
self::$cache->set( $cacheKey, false );
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -134,6 +147,7 @@ class PageImages implements
|
|||
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile( $fileName );
|
||||
}
|
||||
|
||||
self::$cache->set( $cacheKey, $file );
|
||||
return $file;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue