kopia lustrzana https://github.com/badgen/badgen.net
Fix incomplete live cache key
rodzic
190a4ac7b5
commit
d1ccfb490f
|
@ -2,13 +2,13 @@ const liveFns = require('./live-fns/index.js')
|
||||||
const { cache, listCache, clearCache } = require('./lru-cache-live.js')
|
const { cache, listCache, clearCache } = require('./lru-cache-live.js')
|
||||||
|
|
||||||
module.exports = function (router) {
|
module.exports = function (router) {
|
||||||
Object.entries(liveFns).forEach(([key, fn]) => {
|
Object.entries(liveFns).forEach(([name, fn]) => {
|
||||||
router.get(`/${key}/*`, async (req, res, params) => {
|
router.get(`/${name}/*`, async (req, res, params) => {
|
||||||
const {
|
const {
|
||||||
subject = key,
|
subject = name,
|
||||||
status = 'unknown',
|
status = 'unknown',
|
||||||
color = 'grey'
|
color = 'grey'
|
||||||
} = await fetchLiveParams(key, params['*'], fn)
|
} = await fetchLiveParams(name, fn, params['*'])
|
||||||
|
|
||||||
res.writeHead(302, {
|
res.writeHead(302, {
|
||||||
Location: `/badge/${subject}/${status}/${color}`
|
Location: `/badge/${subject}/${status}/${color}`
|
||||||
|
@ -21,23 +21,23 @@ module.exports = function (router) {
|
||||||
router.get('/clear-cache-live', clearCache)
|
router.get('/clear-cache-live', clearCache)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchLiveParams (key, paramsPath, fn) {
|
async function fetchLiveParams (scope, fn, paramsPath) {
|
||||||
const cached = cache.get(paramsPath)
|
const cached = cache.get(paramsPath)
|
||||||
if (cached) {
|
if (cached) {
|
||||||
return cached
|
return cached
|
||||||
} else {
|
} else {
|
||||||
const logStamp = `$${key} ${paramsPath}`
|
const cacheKey = `#${scope} ${paramsPath}`
|
||||||
console.time(logStamp)
|
console.time(cacheKey)
|
||||||
return timeout(fn(...paramsPath.split('/')), 30000)
|
return timeout(fn(...paramsPath.split('/')), 30000)
|
||||||
.then(fetched => {
|
.then(fetched => {
|
||||||
// Update cache if deleted (after got stale)
|
// Update cache if deleted (after got stale)
|
||||||
cache.has(paramsPath) || cache.set(paramsPath, fetched)
|
cache.has(paramsPath) || cache.set(cacheKey, fetched)
|
||||||
return fetched
|
return fetched
|
||||||
}, e => {
|
}, e => {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
return {}
|
return {}
|
||||||
}).then(result => {
|
}).then(result => {
|
||||||
console.timeEnd(logStamp)
|
console.timeEnd(cacheKey)
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue