Hoo man has uploaded a new change for review. https://gerrit.wikimedia.org/r/242150
Change subject: Cache the "modified hash" for the SitesModule ...................................................................... Cache the "modified hash" for the SitesModule Computing it is quite heavy, so cache it for a couple of minutes (10) in the accelerator cache. Bug: T113665 Change-Id: I9510fc2de7abfe7ed459e5b0e9f07c05e4ef4389 --- M lib/includes/modules/SitesModule.php M lib/includes/modules/SitesModuleWorker.php M lib/tests/phpunit/SitesModuleWorkerTest.php 3 files changed, 59 insertions(+), 10 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/50/242150/1 diff --git a/lib/includes/modules/SitesModule.php b/lib/includes/modules/SitesModule.php index 61c8a09..69d3e99 100644 --- a/lib/includes/modules/SitesModule.php +++ b/lib/includes/modules/SitesModule.php @@ -26,7 +26,8 @@ public function __construct() { $this->worker = new SitesModuleWorker( Settings::singleton(), - SiteSQLStore::newInstance() + SiteSQLStore::newInstance(), + wfGetCache( CACHE_ACCEL ) ); } diff --git a/lib/includes/modules/SitesModuleWorker.php b/lib/includes/modules/SitesModuleWorker.php index 1d747b0..f343613 100644 --- a/lib/includes/modules/SitesModuleWorker.php +++ b/lib/includes/modules/SitesModuleWorker.php @@ -2,6 +2,7 @@ namespace Wikibase\Lib; +use BagOStuff; use MediaWikiSite; use Site; use SiteList; @@ -32,12 +33,19 @@ private $siteStore; /** + * @var BagOStuff + */ + private $cache; + + /** * @param SettingsArray $settings * @param SiteStore $siteStore + * @param BagOStuff $cache */ - public function __construct( SettingsArray $settings, SiteStore $siteStore ) { + public function __construct( SettingsArray $settings, SiteStore $siteStore, BagOStuff $cache ) { $this->settings = $settings; $this->siteStore = $siteStore; + $this->cache = $cache; } /** @@ -170,18 +178,37 @@ } /** + * @return string + */ + private function computeModifiedHash() { + $data = array( + $this->getSiteLinkGroups(), + $this->getSpecialSiteLinkGroups(), + $this->getSitesHash() + ); + + return sha1( json_encode( $data ) ); + } + + /** + * This returns a hash which should change whenever either a relevant setting + * or the list of sites changes. Because computing this list is quite heavy and + * it barely changes, cache that hash for a short bit. + * * @see ResourceLoaderModule::getModifiedHash * * @return string */ public function getModifiedHash() { - $data = array( - $this->getSiteLinkGroups(), - $this->getSpecialSiteLinkGroups(), - $this->getSitesHash() - ); + $cacheKey = wfMemcKey( 'wikibase-sites-module-modified-hash' ); + $hash = $this->cache->get( $cacheKey ); - return sha1( json_encode( $data ) ); + if ( $hash === false ) { + $hash = $this->computeModifiedHash(); + $this->cache->set( $cacheKey, $hash, 10 * 60 ); + } + + return $hash; } } diff --git a/lib/tests/phpunit/SitesModuleWorkerTest.php b/lib/tests/phpunit/SitesModuleWorkerTest.php index 9233dbd..3fff82e 100644 --- a/lib/tests/phpunit/SitesModuleWorkerTest.php +++ b/lib/tests/phpunit/SitesModuleWorkerTest.php @@ -2,6 +2,8 @@ namespace Wikibase\Test; +use BagOStuff; +use HashBagOStuff; use MediaWikiSite; use PHPUnit_Framework_TestCase; use Site; @@ -27,13 +29,15 @@ * @param Site[] $sites * @param string[] $groups * @param string[] $specialGroups + * @param BagOStuff $cache * * @return SitesModuleWorker */ private function newSitesModuleWorker( array $sites = array(), array $groups = array(), - array $specialGroups = array() + array $specialGroups = array(), + BagOStuff $cache = null ) { $siteStore = $this->getMock( '\SiteStore' ); $siteStore->expects( $this->any() ) @@ -44,7 +48,9 @@ new SettingsArray( array( 'siteLinkGroups' => $groups, 'specialSiteLinkGroups' => $specialGroups - ) ), $siteStore + ) ), + $siteStore, + $cache ?: new HashBagOStuff() ); } @@ -157,4 +163,19 @@ ); } + public function testGetModifiedHash_caching() { + $cacheKey = wfMemcKey( 'wikibase-sites-module-modified-hash' ); + $cache = new HashBagOStuff(); + $worker = $this->newSitesModuleWorker( array(), array( 'foo' ), array(), $cache ); + + // Make sure whatever hash is computed ends up in the cache + $hash = $worker->getModifiedHash(); + $this->assertSame( $hash, $cache->get( $cacheKey ) ); + + $cache->set( $cacheKey, 'cache all the things!' ); + + // Verify that cached results are returned + $hash = $worker->getModifiedHash(); + $this->assertSame( 'cache all the things!', $hash ); + } } -- To view, visit https://gerrit.wikimedia.org/r/242150 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I9510fc2de7abfe7ed459e5b0e9f07c05e4ef4389 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <h...@online.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits