jenkins-bot has submitted this change and it was merged.

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).

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, 60 insertions(+), 10 deletions(-)

Approvals:
  Ori.livneh: Looks good to me, but someone else must approve
  Daniel Kinzler: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/includes/modules/SitesModule.php 
b/lib/includes/modules/SitesModule.php
index 61c8a09..4c2dab5 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( wfIsHHVM() ? CACHE_ACCEL : CACHE_ANYTHING )
                );
        }
 
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..71ad43b 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,20 @@
                );
        }
 
+       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: merged
Gerrit-Change-Id: I9510fc2de7abfe7ed459e5b0e9f07c05e4ef4389
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Hoo man <h...@online.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: JanZerebecki <jan.wikime...@zerebecki.de>
Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to