Ejegg has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/232897

Change subject: Cache choices retrieved from API or DB
......................................................................

Cache choices retrieved from API or DB

getChoices is called twice, once to get the hash and once to make
the script. Only call API or DB again in the unlikely case language
or project mutates between calls.

A single page load calls load.php a few times, so we could even cache
the choices where they persist across requests.

Change-Id: I62b013dac763a6e10a594d6db3f49e163fc77b0f
---
M includes/CNBannerChoiceDataResourceLoaderModule.php
1 file changed, 18 insertions(+), 11 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralNotice 
refs/changes/97/232897/1

diff --git a/includes/CNBannerChoiceDataResourceLoaderModule.php 
b/includes/CNBannerChoiceDataResourceLoaderModule.php
index 282f60e..67a902e 100644
--- a/includes/CNBannerChoiceDataResourceLoaderModule.php
+++ b/includes/CNBannerChoiceDataResourceLoaderModule.php
@@ -16,28 +16,35 @@
 
        const API_REQUEST_TIMEOUT = 20;
 
+       static $choices = array();
+
        protected function getChoices( ResourceLoaderContext $context ) {
                global $wgNoticeProject, $wgCentralNoticeApiUrl;
 
                $project = $wgNoticeProject;
                $language = $context->getLanguage();
+               $choicesId = "$project|$language";
 
-               // Only fetch the data via the API if $wgCentralNoticeApiUrl is 
set.
-               // Otherwise, use the DB.
-               if ( $wgCentralNoticeApiUrl ) {
-                       $choices = $this->getFromApi( $project, $language );
+               if ( !isset( self::$choices[$choicesId] ) ) {
 
-                       if ( !$choices ) {
-                               wfLogWarning( 'Couldn\'t fetch banner choice 
data via API. ' .
-                                       '$$wgCentralNoticeApiUrl = ' . 
$wgCentralNoticeApiUrl );
+                       // Only fetch the data via the API if 
$wgCentralNoticeApiUrl is set.
+                       // Otherwise, use the DB.
+                       if ( $wgCentralNoticeApiUrl ) {
+                               $choices = $this->getFromApi( $project, 
$language );
 
-                               return array();
+                               if ( !$choices ) {
+                                       wfLogWarning( 'Couldn\'t fetch banner 
choice data via API. ' .
+                                               '$$wgCentralNoticeApiUrl = ' . 
$wgCentralNoticeApiUrl );
+
+                                       $choices = array();
+                               }
+                       } else {
+                                $choices = $this->getFromDb( $project, 
$language );
                        }
-               } else {
-                        $choices = $this->getFromDb( $project, $language );
+                       self::$choices[$choicesId] = $choices;
                }
 
-               return $choices;
+               return self::$choices[$choicesId];
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/232897
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I62b013dac763a6e10a594d6db3f49e163fc77b0f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to