feat: proposed cache WIP ?

moved the countdown section
This commit is contained in:
Matthieu Bessat 2020-09-03 00:25:13 +02:00
parent e6383d64c8
commit 15164c6aa1
7 changed files with 145 additions and 28 deletions

View file

@ -16,6 +16,8 @@ import twig from 'twig'
import EmailService from './EmailService'
import ErrorController from './controllers/ErrorController'
import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-proxy-middleware';
import ExpressRedisCache from 'express-redis-cache'
import Utils from './Utils'
process.env.TZ = "Europe/Paris"
@ -37,6 +39,24 @@ 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,
countdown: 3600 * 2,
page: 3600 * 6
}
// reset cache
cache.del('*', (err, deleted) => {
console.log('> App: Reset cache', err, deleted)
})
let main = async () => {
EmailService.init()
@ -54,16 +74,43 @@ let main = async () => {
console.log('> App: Connected to mongodb')
})
app.set("twig options", {
app.set('twig options', {
allow_async: true,
strict_variables: false
})
app.set('cache', cache)
app.use(cors())
app.use(bodyParser.json())
app.get('/', PublicController.home)
app.get('/c', PublicController.countdown)
app.get('/association/:slug', PublicController.organization)
app.get(
'/',
(req, res, next) => {
if (process.env.OPEN_DATE == null) {
next()
return
}
// redirect to countdown if needed
let isProposed = Utils.isStrUsable(req.query, 'only')
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) {
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('/a-propos', PublicController.about)
app.get('/mentions-legales', PublicController.legals)

View file

@ -325,7 +325,23 @@ export default class AdminOrganizationController {
link: EmailService.getBaseUrl() + '/association/' + extra.slugs[extra.slugs.length - 1]
}
)
res.json({ success: true, data: updateData })
// finally, reset cache of version public and proposed of ALL the slug
// reset the cache of the home page because it is very important also.
const cache = req.app.get('cache')
let proposed = extra.slugs.map((slug: any) => slug + '?version=proposed')
extra.slugs.concat(proposed).forEach((elem: any) => {
console.log('Reset page', '/association/' + elem)
cache.del('/association/' + elem, (err: any, deleted: any) => {
console.log('> App: Reset cache of public page', err, deleted)
})
})
cache.del('/home', (err: any, deleted: any) => {
console.log('> App: Reset cache of public home', err, deleted)
})
res
.json({ success: true, data: updateData })
}).catch(err => res.status(400).json({ success: false, errors: err.errors !== undefined ? err.errors : err }))
}).catch(err => res.status(400).json({ success: false, errors: err }))
}

View file

@ -23,14 +23,6 @@ export default class PublicController {
static async home(req: express.Request, res: express.Response) {
let isProposed = Utils.isStrUsable(req.query, 'only')
let byPass = req.query.bypass != null
let target: any = new Date(process.env.OPEN_DATE == null ? '01/01/1970 00:00' : process.env.OPEN_DATE)
let now: any = new Date()
if (!byPass && !isProposed && target > now) {
return res.render('countdown.twig', {
openDate: process.env.OPEN_DATE
})
}
// let client: IORedis.Redis = RedisService.getClient()
// await client.set('hello', 'world')
// res.json({