Skip to content

Commit

Permalink
api: improve cache-control headers (#1659)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
ns212 and tomjeatt committed Apr 5, 2024
1 parent 92ba0ba commit d84d927
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions api/market_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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;
}
Expand Down Expand Up @@ -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(',')
Expand All @@ -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
})
Expand All @@ -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)
Expand All @@ -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;
}
Expand All @@ -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)
Expand Down

0 comments on commit d84d927

Please sign in to comment.