From 5a6671d728b43b8fb3241b81f56dfc9bc9de1c0f Mon Sep 17 00:00:00 2001 From: Bryan Housel Date: Sat, 1 Dec 2018 22:06:17 -0500 Subject: [PATCH] Cache wikidata results Because wikidata field potentially shown on hover now and the results of this lookup will never change --- modules/services/wikidata.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/services/wikidata.js b/modules/services/wikidata.js index 83301fc65..1db0a5937 100644 --- a/modules/services/wikidata.js +++ b/modules/services/wikidata.js @@ -1,15 +1,18 @@ import { json as d3_json } from 'd3-request'; import { utilQsString } from '../util'; - import { currentLocale } from '../util/locale'; var endpoint = 'https://www.wikidata.org/w/api.php?'; +var _wikidataCache = {}; export default { init: function() {}, - reset: function() {}, + + reset: function() { + _wikidataCache = {}; + }, // Given a Wikipedia language and article title, return an array of @@ -42,6 +45,10 @@ export default { callback('', {}); return; } + if (_wikidataCache[qid]) { + callback('', _wikidataCache[qid]); + return + } var lang = currentLocale.replace(/-/g, '_'); @@ -58,6 +65,7 @@ export default { if (err || !data || data.error) { callback('', {}); } else { + _wikidataCache[qid] = data.entities[qid]; callback(qid, data.entities[qid] || {}); } });