aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-05-11 09:44:04 +0200
committerChocobozzz <me@florianbigard.com>2018-05-11 15:24:17 +0200
commitfd4484f19eae8b0a0c30d5d30e98880c8708516a (patch)
tree9dcb357413c84797b79e301fb843b1695e20d902 /server/middlewares
parent8a2db2e8cb67f7f802ecb35be7b9154695f1a6ec (diff)
downloadPeerTube-fd4484f19eae8b0a0c30d5d30e98880c8708516a.tar.gz
PeerTube-fd4484f19eae8b0a0c30d5d30e98880c8708516a.tar.zst
PeerTube-fd4484f19eae8b0a0c30d5d30e98880c8708516a.zip
Cache AP video route for 5 seconds
Diffstat (limited to 'server/middlewares')
-rw-r--r--server/middlewares/cache.ts43
1 files changed, 23 insertions, 20 deletions
diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts
index a2c7f7cbd..c589ef683 100644
--- a/server/middlewares/cache.ts
+++ b/server/middlewares/cache.ts
@@ -2,36 +2,39 @@ import * as express from 'express'
2import { Redis } from '../lib/redis' 2import { Redis } from '../lib/redis'
3import { logger } from '../helpers/logger' 3import { logger } from '../helpers/logger'
4 4
5async function cacheRoute (req: express.Request, res: express.Response, next: express.NextFunction) { 5function cacheRoute (lifetime: number) {
6 const cached = await Redis.Instance.getCachedRoute(req) 6 return async function (req: express.Request, res: express.Response, next: express.NextFunction) {
7 const cached = await Redis.Instance.getCachedRoute(req)
7 8
8 // Not cached 9 // Not cached
9 if (!cached) { 10 if (!cached) {
10 logger.debug('Not cached result for route %s.', req.originalUrl) 11 logger.debug('Not cached result for route %s.', req.originalUrl)
11 12
12 const sendSave = res.send.bind(res) 13 const sendSave = res.send.bind(res)
13 14
14 res.send = (body) => { 15 res.send = (body) => {
15 if (res.statusCode >= 200 && res.statusCode < 400) { 16 if (res.statusCode >= 200 && res.statusCode < 400) {
16 Redis.Instance.setCachedRoute(req, body, res.getHeader('content-type').toString(), res.statusCode) 17 const contentType = res.getHeader('content-type').toString()
17 .catch(err => logger.error('Cannot cache route.', { err })) 18 Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode)
19 .catch(err => logger.error('Cannot cache route.', { err }))
20 }
21
22 return sendSave(body)
18 } 23 }
19 24
20 return sendSave(body) 25 return next()
21 } 26 }
22 27
23 return next() 28 if (cached.contentType) res.contentType(cached.contentType)
24 }
25 29
26 if (cached.contentType) res.contentType(cached.contentType) 30 if (cached.statusCode) {
31 const statusCode = parseInt(cached.statusCode, 10)
32 if (!isNaN(statusCode)) res.status(statusCode)
33 }
27 34
28 if (cached.statusCode) { 35 logger.debug('Use cached result for %s.', req.originalUrl)
29 const statusCode = parseInt(cached.statusCode, 10) 36 return res.send(cached.body).end()
30 if (!isNaN(statusCode)) res.status(statusCode)
31 } 37 }
32
33 logger.debug('Use cached result for %s.', req.originalUrl)
34 return res.send(cached.body).end()
35} 38}
36 39
37// --------------------------------------------------------------------------- 40// ---------------------------------------------------------------------------