fix: some cache issues with disabling sometimes

This commit is contained in:
Matthieu Bessat 2020-09-03 21:53:20 +02:00
parent de4a85a247
commit 272f4a5389

View file

@ -39,23 +39,38 @@ if (process.env.DISABLE_TWIG_CACHE === 'true') {
twig.cache(false) twig.cache(false)
} }
const cache = ExpressRedisCache({ const cacheDuration = {
home: 3600 * 0.5,
countdown: 3600 * 2,
page: 3600 * 3
}
let cache: any = {
route: (x = null, y = null) => {
return (req: any, res: any, next: any) => { }
}
}
if (process.env.DISABLE_CACHE === 'true') {
cache = {
route: (x = null, y = null) => {
return (req: any, res: any, next: any) => {
next()
}
}
}
} else {
cache = ExpressRedisCache({
host: process.env.REDIS_HOST, host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT, port: process.env.REDIS_PORT,
auth_pass: process.env.REDIS_PASSWORD, auth_pass: process.env.REDIS_PASSWORD,
prefix: process.env.REDIS_PREFIX prefix: process.env.REDIS_PREFIX
}) })
const cacheDuration = { // reset cache
home: 3600 * 0.75, cache.del('*', (err: any, deleted: any) => {
countdown: 3600 * 2,
page: 3600 * 6
}
// reset cache
cache.del('*', (err, deleted) => {
console.log('> App: Reset cache', err, deleted) console.log('> App: Reset cache', err, deleted)
}) })
}
let main = async () => { let main = async () => {
EmailService.init() EmailService.init()
@ -95,22 +110,34 @@ let main = async () => {
let byPass = req.query.bypass != null let byPass = req.query.bypass != null
let target: any = new Date(process.env.OPEN_DATE) let target: any = new Date(process.env.OPEN_DATE)
let now: any = new Date() let now: any = new Date()
if (!byPass && !isProposed && target > now) { // disable if 'only' key exists in query param
if (isProposed) {
res.use_express_redis_cache = false
}
// add some padding in seconds
if (!byPass && !isProposed && target - 4000 > now) {
return res.redirect('/c') return res.redirect('/c')
} else { } else {
next() next()
} }
let withId = req.query.only != null
if (byPass || withId) {
res.use_express_redis_cache = false
}
}, },
cache.route('/home', cacheDuration.home), cache.route('/home', cacheDuration.home),
PublicController.home PublicController.home
) )
app.get('/c', cache.route('/c', cacheDuration.countdown), PublicController.countdown) app.get('/c', cache.route('/c', cacheDuration.countdown), PublicController.countdown)
app.get('/association/:slug', cache.route(cacheDuration.page), PublicController.organization) app.get(
'/association/:slug',
(req, res, next) => {
// disable cache if proposed in query
let isProposed = Utils.isStrUsable(req.query, 'version')
if (isProposed) {
res.use_express_redis_cache = false
}
next()
},
cache.route(cacheDuration.page),
PublicController.organization
)
app.get('/a-propos', PublicController.about) app.get('/a-propos', PublicController.about)
app.get('/mentions-legales', PublicController.legals) app.get('/mentions-legales', PublicController.legals)