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
|
const stats = await r2(endpoint).json
|
||||||
return {
|
return {
|
||||||
subject: 'downloads',
|
subject: 'downloads',
|
||||||
status: millify(stats.downloads) + period.replace('last-', '%2F'),
|
status: millify(stats.downloads) + period.replace('last-', '/'),
|
||||||
color: 'green'
|
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 liveFns = require('./live-fns/_index.js')
|
||||||
const { cache, listCache, clearCache } = require('./lru-cache-live.js')
|
|
||||||
|
|
||||||
module.exports = function (router) {
|
module.exports = function (router) {
|
||||||
Object.entries(liveFns).forEach(([name, fn]) => {
|
Object.entries(liveFns).forEach(([name, fn]) => {
|
||||||
|
|
@ -10,42 +10,29 @@ module.exports = function (router) {
|
||||||
color = 'grey'
|
color = 'grey'
|
||||||
} = await fetchLiveParams(name, fn, params['*'])
|
} = await fetchLiveParams(name, fn, params['*'])
|
||||||
|
|
||||||
res.writeHead(302, {
|
res.writeHead(200, {
|
||||||
Location: `/badge/${subject}/${status}/${color}`
|
'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) {
|
async function fetchLiveParams (scope, fn, paramsPath) {
|
||||||
const cached = cache.get(paramsPath)
|
const params = paramsPath.split('/')
|
||||||
if (cached) {
|
const logKey = `#${scope} ${paramsPath}`
|
||||||
return cached
|
return timeout(fn(...params), 30000, logKey)
|
||||||
} 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
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function timeout (promise, period) {
|
function timeout (task, period, logKey) {
|
||||||
return Promise.race([
|
console.time(logKey)
|
||||||
new Promise((resolve, reject) => setTimeout(reject, period)),
|
const timer = new Promise((resolve, reject) => setTimeout(reject, period))
|
||||||
promise
|
return Promise.race([task, timer]).catch(e => {
|
||||||
])
|
console.error(e)
|
||||||
|
return {}
|
||||||
|
}).then(result => {
|
||||||
|
console.timeEnd(logKey)
|
||||||
|
return result
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Ładowanie…
Reference in New Issue