From 6b864e6fef42c9dcc163e76b09b2470c6671f402 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Sun, 7 Nov 2021 02:23:28 +0100 Subject: [PATCH] Add some caching to the wikidata search box, partial fix to #551 and #530 --- Logic/Web/Wikidata.ts | 5 +++-- Models/Constants.ts | 2 +- UI/Wikipedia/WikidataSearchBox.ts | 33 +++++++++++++++++++------------ 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Logic/Web/Wikidata.ts b/Logic/Web/Wikidata.ts index 3a597a6fa..0d9d5b6ba 100644 --- a/Logic/Web/Wikidata.ts +++ b/Logic/Web/Wikidata.ts @@ -172,7 +172,7 @@ export default class Wikidata { lang + "&type=item&origin=*" + "&props=";// props= removes some unused values in the result - const response = await Utils.downloadJson(url) + const response = await Utils.downloadJsonCached(url, 10000) const result: any[] = response.search @@ -192,6 +192,7 @@ export default class Wikidata { return result; } + public static async searchAndFetch( search: string, options?: WikidataSearchoptions @@ -287,7 +288,7 @@ export default class Wikidata { } const url = "https://www.wikidata.org/wiki/Special:EntityData/" + id + ".json"; - const entities = (await Utils.downloadJson(url)).entities + const entities = (await Utils.downloadJsonCached(url, 10000)).entities const firstKey = Array.from(Object.keys(entities))[0] // Roundabout way to fetch the entity; it might have been a redirect const response = entities[firstKey] diff --git a/Models/Constants.ts b/Models/Constants.ts index 59857d108..169beef28 100644 --- a/Models/Constants.ts +++ b/Models/Constants.ts @@ -2,7 +2,7 @@ import {Utils} from "../Utils"; export default class Constants { - public static vNumber = "0.11.3"; + public static vNumber = "0.11.4"; public static ImgurApiKey = '7070e7167f0a25a' public static readonly mapillary_client_token_v3 = 'TXhLaWthQ1d4RUg0czVxaTVoRjFJZzowNDczNjUzNmIyNTQyYzI2' public static readonly mapillary_client_token_v4 = "MLY|4441509239301885|b40ad2d3ea105435bd40c7e76993ae85" diff --git a/UI/Wikipedia/WikidataSearchBox.ts b/UI/Wikipedia/WikidataSearchBox.ts index da0c90ebc..c3df838cd 100644 --- a/UI/Wikipedia/WikidataSearchBox.ts +++ b/UI/Wikipedia/WikidataSearchBox.ts @@ -13,6 +13,8 @@ import Svg from "../../Svg"; export default class WikidataSearchBox extends InputElement { + private static readonly _searchCache = new Map>() + IsSelected: UIEventSource = new UIEventSource(false); private readonly wikidataId: UIEventSource private readonly searchText: UIEventSource @@ -29,6 +31,10 @@ export default class WikidataSearchBox extends InputElement { return this.wikidataId; } + IsValid(t: string): boolean { + return t.startsWith("Q") && !isNaN(Number(t.substring(1))); + } + protected InnerConstructElement(): HTMLElement { const searchField = new TextField({ @@ -46,12 +52,20 @@ export default class WikidataSearchBox extends InputElement { return; } searchFailMessage.setData(undefined) - lastSearchResults.WaitForPromise( - Wikidata.searchAndFetch(searchText, { - lang: Locale.language.data, + + const lang = Locale.language.data + const key = lang + ":" + searchText + let promise = WikidataSearchBox._searchCache.get(key) + if (promise === undefined) { + promise = Wikidata.searchAndFetch(searchText, { + lang, maxCount: 5 } - ), err => searchFailMessage.setData(err)) + ) + WikidataSearchBox._searchCache.set(key, promise) + } + + lastSearchResults.WaitForPromise(promise, err => searchFailMessage.setData(err)) }) @@ -61,10 +75,10 @@ export default class WikidataSearchBox extends InputElement { return new Combine([Translations.t.general.wikipedia.failed.Clone().SetClass("alert"), searchFailMessage.data]) } - if(searchResults.length === 0){ + if (searchResults.length === 0) { return Translations.t.general.wikipedia.noResults.Subs({search: searchField.GetValue().data ?? ""}) } - + if (searchResults.length === 0) { return Translations.t.general.wikipedia.doSearch } @@ -88,7 +102,6 @@ export default class WikidataSearchBox extends InputElement { }, [searchFailMessage])) - // const full = new Combine([ new Title(Translations.t.general.wikipedia.searchWikidata, 3).SetClass("m-2"), new Combine([ @@ -108,10 +121,4 @@ export default class WikidataSearchBox extends InputElement { ]).ConstructElement(); } - IsSelected: UIEventSource = new UIEventSource(false); - - IsValid(t: string): boolean { - return t.startsWith("Q") && !isNaN(Number(t.substring(1))); - } - } \ No newline at end of file