2018-04-17 07:01:06 -05:00
|
|
|
import * as express from 'express'
|
2018-05-23 03:03:26 -05:00
|
|
|
import * as AsyncLock from 'async-lock'
|
2019-04-11 08:38:53 -05:00
|
|
|
import { parseDurationToMs } from '../helpers/core-utils'
|
2018-04-17 07:01:06 -05:00
|
|
|
import { Redis } from '../lib/redis'
|
|
|
|
import { logger } from '../helpers/logger'
|
|
|
|
|
2018-05-23 03:03:26 -05:00
|
|
|
const lock = new AsyncLock({ timeout: 5000 })
|
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
function cacheRoute (lifetimeArg: string | number) {
|
2019-04-24 10:19:00 -05:00
|
|
|
const lifetime = parseDurationToMs(lifetimeArg)
|
|
|
|
|
2018-05-11 02:44:04 -05:00
|
|
|
return async function (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2018-10-05 04:15:06 -05:00
|
|
|
const redisKey = Redis.Instance.generateCachedRouteKey(req)
|
2018-05-23 03:03:26 -05:00
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
try {
|
|
|
|
await lock.acquire(redisKey, async (done) => {
|
|
|
|
const cached = await Redis.Instance.getCachedRoute(req)
|
2018-04-17 07:01:06 -05:00
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
// Not cached
|
|
|
|
if (!cached) {
|
|
|
|
logger.debug('No cached results for route %s.', req.originalUrl)
|
2018-04-17 07:01:06 -05:00
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
const sendSave = res.send.bind(res)
|
2018-11-16 04:18:13 -06:00
|
|
|
const redirectSave = res.redirect.bind(res)
|
2018-04-17 07:01:06 -05:00
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
res.send = (body) => {
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 400) {
|
|
|
|
const contentType = res.get('content-type')
|
|
|
|
|
|
|
|
Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode)
|
|
|
|
.then(() => done())
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Cannot cache route.', { err })
|
|
|
|
return done(err)
|
|
|
|
})
|
2018-08-31 04:43:46 -05:00
|
|
|
} else {
|
|
|
|
done()
|
2018-07-24 07:35:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return sendSave(body)
|
2018-05-23 03:03:26 -05:00
|
|
|
}
|
|
|
|
|
2018-11-16 04:18:13 -06:00
|
|
|
res.redirect = url => {
|
|
|
|
done()
|
|
|
|
|
|
|
|
return redirectSave(url)
|
|
|
|
}
|
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
return next()
|
2018-05-11 02:44:04 -05:00
|
|
|
}
|
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
if (cached.contentType) res.set('content-type', cached.contentType)
|
2018-04-17 07:01:06 -05:00
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
if (cached.statusCode) {
|
|
|
|
const statusCode = parseInt(cached.statusCode, 10)
|
|
|
|
if (!isNaN(statusCode)) res.status(statusCode)
|
|
|
|
}
|
2018-04-17 07:01:06 -05:00
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
logger.debug('Use cached result for %s.', req.originalUrl)
|
|
|
|
res.send(cached.body).end()
|
2018-04-17 07:01:06 -05:00
|
|
|
|
2018-07-24 07:35:11 -05:00
|
|
|
return done()
|
|
|
|
})
|
|
|
|
} catch (err) {
|
2018-08-31 04:43:46 -05:00
|
|
|
logger.error('Cannot serve cached route.', { err })
|
2018-07-24 07:35:11 -05:00
|
|
|
return next()
|
|
|
|
}
|
2018-04-17 07:01:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-07-24 07:35:11 -05:00
|
|
|
cacheRoute
|
2018-04-17 07:01:06 -05:00
|
|
|
}
|