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

Change subject: Templates: Cache the template data requests
......................................................................


Templates: Cache the template data requests

The getTargetTemplate method in Templates module was not optimized
to reuse the template data we recieved already.
This causes too many requests for template information.

Testplan:

For example, watch the network requests while translating
https://en.wikipedia.org/wiki/Electronic_cigarette to some language.
Without this patch, you will see hundreds of duplicate requests. With this
patch, you should see only a few unique requests

Bug: T123301
Change-Id: Ife2fee28d0ab20dc3c0effedccd3e7ab0a39ee9a
---
M modules/tools/ext.cx.tools.template.js
1 file changed, 16 insertions(+), 4 deletions(-)

Approvals:
  Nikerabbit: Checked; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/tools/ext.cx.tools.template.js 
b/modules/tools/ext.cx.tools.template.js
index 5192994..5851f46 100644
--- a/modules/tools/ext.cx.tools.template.js
+++ b/modules/tools/ext.cx.tools.template.js
@@ -10,6 +10,8 @@
 ( function ( $, mw ) {
        'use strict';
 
+       var cachedTemplates = {};
+
        /**
         * TemplateTool encapsulates the handling of templates in translation.
         *
@@ -180,11 +182,18 @@
         * @return {number} return.done.data The page id
         */
        TemplateTool.prototype.getTargetTemplate = function () {
+               var api, request,
+                       cacheKey = mw.cx.targetLanguage + ':' + 
this.templateTitle;
+
+               if ( cachedTemplates[ cacheKey ] ) {
+                       return cachedTemplates[ cacheKey ].promise();
+               }
+
                // TODO: Avoid direct access to globals
-               var api = mw.cx.siteMapper.getApi( mw.cx.targetLanguage );
+               api = mw.cx.siteMapper.getApi( mw.cx.targetLanguage );
 
                // Note that we use canonical namespace 'Template' for title.
-               return api.get( {
+               request = api.get( {
                        action: 'query',
                        titles: 'Template:' + this.templateTitle,
                        redirects: true,
@@ -199,9 +208,12 @@
                        if ( pageId === '-1' ) {
                                return $.Deferred().reject().promise();
                        }
-
                        return response.query;
-               } ).promise();
+               } );
+
+               cachedTemplates[ cacheKey ] = request;
+
+               return request.promise();
        };
 
        /**

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ife2fee28d0ab20dc3c0effedccd3e7ab0a39ee9a
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Santhosh <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to