From 272f4a5389a8dd9f905fd9eb128c91e0eb987dbe Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Thu, 3 Sep 2020 21:53:20 +0200 Subject: [PATCH] fix: some cache issues with disabling sometimes --- src/app.ts | 67 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/src/app.ts b/src/app.ts index dcf45d7..696e6d1 100644 --- a/src/app.ts +++ b/src/app.ts @@ -39,23 +39,38 @@ if (process.env.DISABLE_TWIG_CACHE === 'true') { twig.cache(false) } -const cache = ExpressRedisCache({ - host: process.env.REDIS_HOST, - port: process.env.REDIS_PORT, - auth_pass: process.env.REDIS_PASSWORD, - prefix: process.env.REDIS_PREFIX -}) - const cacheDuration = { - home: 3600 * 0.75, + home: 3600 * 0.5, countdown: 3600 * 2, - page: 3600 * 6 + page: 3600 * 3 } -// reset cache -cache.del('*', (err, deleted) => { - console.log('> App: Reset cache', err, deleted) -}) +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, + port: process.env.REDIS_PORT, + auth_pass: process.env.REDIS_PASSWORD, + prefix: process.env.REDIS_PREFIX + }) + + // reset cache + cache.del('*', (err: any, deleted: any) => { + console.log('> App: Reset cache', err, deleted) + }) +} let main = async () => { EmailService.init() @@ -95,22 +110,34 @@ let main = async () => { let byPass = req.query.bypass != null let target: any = new Date(process.env.OPEN_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') } else { next() } - - let withId = req.query.only != null - if (byPass || withId) { - res.use_express_redis_cache = false - } }, cache.route('/home', cacheDuration.home), PublicController.home ) 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('/mentions-legales', PublicController.legals)