From 10e4b19f40de608b13f7e364d9abd374549a99b6 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 6 Jan 2024 23:25:04 +0800 Subject: [PATCH] Perf: add cache to user-agent list --- Build/build-speedtest-domainset.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Build/build-speedtest-domainset.ts b/Build/build-speedtest-domainset.ts index a94415a2..f1346a1f 100644 --- a/Build/build-speedtest-domainset.ts +++ b/Build/build-speedtest-domainset.ts @@ -16,8 +16,17 @@ import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-file const s = new Sema(2); -const latestTopUserAgentsPromise = fetchWithRetry('https://unpkg.com/top-user-agents@latest/src/desktop.json') - .then(res => res.json()).then(userAgents => userAgents.filter(ua => ua.startsWith('Mozilla/5.0 '))); +const latestTopUserAgentsPromise = fsCache.apply( + 'https://unpkg.com/top-user-agents@latest/src/desktop.json', + () => fetchWithRetry('https://unpkg.com/top-user-agents@latest/src/desktop.json') + .then(res => res.json()) + .then(userAgents => userAgents.filter(ua => ua.startsWith('Mozilla/5.0 '))), + { + serializer: serializeArray, + deserializer: deserializeArray, + ttl: TTL.ONE_DAY() + } +); const querySpeedtestApi = async (keyword: string): Promise> => { const topUserAgents = await latestTopUserAgentsPromise;