Page MenuHomePhabricator
Paste P7764

0001-hacky-api-rsp-caching-code.patch
ArchivedPublic

Authored by Mholloway on Nov 5 2018, 8:24 PM.
From 38817a01681aa1cb1fb7954097ece057afee936a Mon Sep 17 00:00:00 2001
From: Michael Holloway <mholloway@wikimedia.org>
Date: Mon, 5 Nov 2018 14:19:01 -0600
Subject: [PATCH 1/1] hacky api rsp caching code
Change-Id: I8bce128d37dfb2b8951a7963382853a8af8014f7
---
lib/api-util.js | 9 +++++++++
lib/parsoid-access.js | 12 +++++++++++-
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/lib/api-util.js b/lib/api-util.js
index b7c3c02..2ad4a83 100644
--- a/lib/api-util.js
+++ b/lib/api-util.js
@@ -9,6 +9,8 @@ const HTTPError = sUtil.HTTPError;
const MAX_BATCH_SIZE = 50;
+const CACHE = {};
+
function prettyMwApiReq(request) {
// formatting it as GET request even though we use POST requests for MW API,
// just because GETs are easier to run from commandline or see it in a browser
@@ -37,6 +39,12 @@ function mwApiGet(app, domain, query) {
}
});
+ const key = JSON.stringify(query);
+
+ if (CACHE[key]) {
+ return CACHE[key];
+ }
+
if (app.conf.debug) {
app.logger.log(`trace/mwApiGet`, { msg: 'outgoing request', to: prettyMwApiReq(request) });
}
@@ -52,6 +60,7 @@ function mwApiGet(app, domain, query) {
detail: response.body
});
}
+ CACHE[key] = response;
return response;
});
diff --git a/lib/parsoid-access.js b/lib/parsoid-access.js
index 3ff6cc8..69d5d19 100644
--- a/lib/parsoid-access.js
+++ b/lib/parsoid-access.js
@@ -4,6 +4,7 @@
'use strict';
+const P = require('bluebird');
const domino = require('domino');
const sUtil = require('./util');
const mUtil = require('./mobile-util');
@@ -14,6 +15,8 @@ const parsoidSections = require('./sections/parsoidSections');
const transforms = require('./transforms');
const HTTPError = sUtil.HTTPError;
+const CACHE = {};
+
/**
* Generic function to get page content from the REST API.
@@ -34,11 +37,18 @@ function _getRestPageContent(app, req, endpoint, spec) {
}
const domain = req.params.domain.replace(/^(\w+\.)m\./, '$1');
const path = `page/${endpoint}/${encodeURIComponent(req.params.title)}${suffix}`;
+ if (CACHE[path]) {
+ return P.resolve(CACHE[path]);
+ }
const restReq = { headers: {
accept: mUtil.getContentTypeString(spec),
'accept-language': req.headers['accept-language']
} };
- return api.restApiGet(app, domain, path, restReq);
+ return api.restApiGet(app, domain, path, restReq)
+ .then((rsp) => {
+ CACHE[path] = rsp;
+ return rsp;
+ });
}
/**
--
2.19.1

Event Timeline

Nice. Not that it makes a big difference but instead of caching at the parsoid-access._getRestPageContent() level you could also do it at api-util.restApiGet().