Skip to content

Commit

Permalink
Caching summary
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Nov 21, 2023
1 parent 01228d8 commit 43788e0
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/pages/api/v1/[[...all]].js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ let handler = nextConnect({ attachParams: true, })
})

.get('/api/v1/summary', async (req, res) => {
const now = Date.now()
if (global.cachedDate && ((now - global.cachedDate) > 5*60*1000)) {
delete global.cachedSummary
delete global.cachedDate
}
if (global.cachedSummary) {
res.setHeader('X-Cached', 'true')
res.json(global.cachedSummary)
}
const ret = []
for (const pairId of cfgPairs) {
const obj = {
Expand Down Expand Up @@ -124,7 +133,11 @@ let handler = nextConnect({ attachParams: true, })

ret.push(obj)
}
res.json(ret)
if (!global.cachedSummary) {
res.json(ret)
}
global.cachedSummary = ret
global.cachedDate = now
})

.get('/api/v1/orderbook/:market_pair', async (req, res) => {
Expand Down

0 comments on commit 43788e0

Please sign in to comment.