kopia lustrzana https://github.com/badgen/badgen.net
Remove lru-cache-live in favor of Now CDN
rodzic
042387cb33
commit
c5f0d8beb0
|
|
@ -26,7 +26,7 @@ async function d (period, args) {
|
|||
const stats = await r2(endpoint).json
|
||||
return {
|
||||
subject: 'downloads',
|
||||
status: millify(stats.downloads) + period.replace('last-', '%2F'),
|
||||
status: millify(stats.downloads) + period.replace('last-', '/'),
|
||||
color: 'green'
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +0,0 @@
|
|||
const LRU = require('lru-cache')
|
||||
|
||||
const cache = LRU({
|
||||
max: 5000,
|
||||
maxAge: 12e4, // 2 minutes
|
||||
stale: true
|
||||
})
|
||||
|
||||
function listCache (req, res) {
|
||||
res.writeHead(200)
|
||||
res.end(`Total ${cache.length}\n${cache.keys().join('\n')}`)
|
||||
}
|
||||
|
||||
function clearCache (req, res) {
|
||||
const count = cache.length
|
||||
const keys = cache.keys().join('\n')
|
||||
cache.reset()
|
||||
|
||||
res.writeHead(200)
|
||||
res.end(`Cleared ${count}\n${keys}`)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
cache,
|
||||
listCache,
|
||||
clearCache
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
const badgen = require('badgen')
|
||||
const liveFns = require('./live-fns/_index.js')
|
||||
const { cache, listCache, clearCache } = require('./lru-cache-live.js')
|
||||
|
||||
module.exports = function (router) {
|
||||
Object.entries(liveFns).forEach(([name, fn]) => {
|
||||
|
|
@ -10,42 +10,29 @@ module.exports = function (router) {
|
|||
color = 'grey'
|
||||
} = await fetchLiveParams(name, fn, params['*'])
|
||||
|
||||
res.writeHead(302, {
|
||||
Location: `/badge/${subject}/${status}/${color}`
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'image/svg+xml;charset=utf-8',
|
||||
'Cache-Control': 'public, max-age=60, s-maxage=60'
|
||||
})
|
||||
res.end()
|
||||
res.end(badgen({subject, status, color}))
|
||||
})
|
||||
})
|
||||
|
||||
router.get('/list-cache-live', listCache)
|
||||
router.get('/clear-cache-live', clearCache)
|
||||
}
|
||||
|
||||
async function fetchLiveParams (scope, fn, paramsPath) {
|
||||
const cached = cache.get(paramsPath)
|
||||
if (cached) {
|
||||
return cached
|
||||
} else {
|
||||
const cacheKey = `#${scope} ${paramsPath}`
|
||||
console.time(cacheKey)
|
||||
return timeout(fn(...paramsPath.split('/')), 30000)
|
||||
.then(fetched => {
|
||||
// Update cache if deleted (after got stale)
|
||||
cache.has(paramsPath) || cache.set(cacheKey, fetched)
|
||||
return fetched
|
||||
}, e => {
|
||||
console.error(e)
|
||||
return {}
|
||||
}).then(result => {
|
||||
console.timeEnd(cacheKey)
|
||||
return result
|
||||
})
|
||||
}
|
||||
const params = paramsPath.split('/')
|
||||
const logKey = `#${scope} ${paramsPath}`
|
||||
return timeout(fn(...params), 30000, logKey)
|
||||
}
|
||||
|
||||
function timeout (promise, period) {
|
||||
return Promise.race([
|
||||
new Promise((resolve, reject) => setTimeout(reject, period)),
|
||||
promise
|
||||
])
|
||||
function timeout (task, period, logKey) {
|
||||
console.time(logKey)
|
||||
const timer = new Promise((resolve, reject) => setTimeout(reject, period))
|
||||
return Promise.race([task, timer]).catch(e => {
|
||||
console.error(e)
|
||||
return {}
|
||||
}).then(result => {
|
||||
console.timeEnd(logKey)
|
||||
return result
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue