From d84d9277bce99a2d56f6c23b31189d13dfc0727b Mon Sep 17 00:00:00 2001 From: ns212 <73105077+ns212@users.noreply.github.com> Date: Fri, 5 Apr 2024 08:09:37 +0100 Subject: [PATCH] api: improve cache-control headers (#1659) * api: use coingecko for UI market data * api: use coingecko for UI market data. store js objects in redis According to the vercel documentation get() and set() support js objects so we do not need to serialise them. Ref.: https://vercel.com/docs/storage/vercel-kv/kv-reference#set * api: re-enable diadata market data --------- Co-authored-by: tomjeatt <40243778+tomjeatt@users.noreply.github.com> --- api/market_data.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/api/market_data.js b/api/market_data.js index c1a681adb..778fb2d0e 100644 --- a/api/market_data.js +++ b/api/market_data.js @@ -47,7 +47,7 @@ const fetchDiaXLSD = async () => { const cache_key = "diaxlsd" const cached = await kv.get(cache_key) if (cached) { - return JSON.parse(cached) + return cached } const url = 'https://api.diadata.org/xlsd' @@ -59,7 +59,7 @@ const fetchDiaXLSD = async () => { const result = new Map(json.map(x => [x.Token, x])) // cache the data for 120 seconds - await kv.set(cache_key, JSON.stringify(result), { ex: 120 }) + await kv.set(cache_key, result, { ex: 120 }) .catch(err => console.error('Unable to cache Dia data', err)) return result; } @@ -103,7 +103,7 @@ const dia = async (args) => { const cache_key = "dia_" + args.ids const cached = await kv.get(cache_key) if (cached) { - return JSON.parse(cached) + return cached } const assets = args.ids.split(',') @@ -117,7 +117,7 @@ const dia = async (args) => { }, {})) .then(async x => { // cache the data for 120 seconds - kv.set(cache_key, JSON.stringify(x), { ex: 120 }) + kv.set(cache_key, x, { ex: 120 }) .catch(err => console.error('Unable to cache Dia data', err)) return x }) @@ -128,7 +128,7 @@ const coingecko = async (args) => { const cached = await kv.get(cache_key) if (cached) { console.log('Cached', cache_key, cached) - return JSON.parse(cached) + return cached } const url = 'https://api.coingecko.com/api/v3/simple/price?' + new URLSearchParams(args) @@ -139,8 +139,8 @@ const coingecko = async (args) => { } // cache the data for 120 seconds - console.log('Caching', cache_key, JSON.stringify(data)) - kv.set(cache_key, JSON.stringify(data), { ex: 120 }) + console.log('Caching', cache_key, data) + kv.set(cache_key, data, { ex: 120 }) .catch(err => console.error('Unable to cache coingecko data', err)) return data; } @@ -164,7 +164,7 @@ export default async function (request, response) { return response .status(200) .setHeader("content-type", "application/json") - .setHeader("cache-control", "public, maxage=0, s-maxage=120") + .setHeader("cache-control", "public, maxage=0, s-maxage=120, stale-while-revalidate=300, stale-if-error=300") .json(resp) } catch (err) { console.error('Unable to fetch prices', err)