diff options
Diffstat (limited to 'server/middlewares/cache.ts')
-rw-r--r-- | server/middlewares/cache.ts | 43 |
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' | |||
2 | import { Redis } from '../lib/redis' | 2 | import { Redis } from '../lib/redis' |
3 | import { logger } from '../helpers/logger' | 3 | import { logger } from '../helpers/logger' |
4 | 4 | ||
5 | async function cacheRoute (req: express.Request, res: express.Response, next: express.NextFunction) { | 5 | function 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 | // --------------------------------------------------------------------------- |