Merge branch 'develop' into pr/1217
[oweals/peertube.git] / server / middlewares / cache.ts
1 import * as express from 'express'
2 import * as AsyncLock from 'async-lock'
3 import { parseDuration } from '../helpers/core-utils'
4 import { Redis } from '../lib/redis'
5 import { logger } from '../helpers/logger'
6
7 const lock = new AsyncLock({ timeout: 5000 })
8
9 function cacheRoute (lifetimeArg: string | number) {
10   return async function (req: express.Request, res: express.Response, next: express.NextFunction) {
11     const redisKey = Redis.Instance.generateCachedRouteKey(req)
12
13     try {
14       await lock.acquire(redisKey, async (done) => {
15         const cached = await Redis.Instance.getCachedRoute(req)
16
17         // Not cached
18         if (!cached) {
19           logger.debug('No cached results for route %s.', req.originalUrl)
20
21           const sendSave = res.send.bind(res)
22           const redirectSave = res.redirect.bind(res)
23
24           res.send = (body) => {
25             if (res.statusCode >= 200 && res.statusCode < 400) {
26               const contentType = res.get('content-type')
27               const lifetime = parseDuration(lifetimeArg)
28
29               Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode)
30                    .then(() => done())
31                    .catch(err => {
32                      logger.error('Cannot cache route.', { err })
33                      return done(err)
34                    })
35             } else {
36               done()
37             }
38
39             return sendSave(body)
40           }
41
42           res.redirect = url => {
43             done()
44
45             return redirectSave(url)
46           }
47
48           return next()
49         }
50
51         if (cached.contentType) res.set('content-type', cached.contentType)
52
53         if (cached.statusCode) {
54           const statusCode = parseInt(cached.statusCode, 10)
55           if (!isNaN(statusCode)) res.status(statusCode)
56         }
57
58         logger.debug('Use cached result for %s.', req.originalUrl)
59         res.send(cached.body).end()
60
61         return done()
62       })
63     } catch (err) {
64       logger.error('Cannot serve cached route.', { err })
65       return next()
66     }
67   }
68 }
69
70 // ---------------------------------------------------------------------------
71
72 export {
73   cacheRoute
74 }