diff options
Diffstat (limited to 'server/middlewares/cache.ts')
-rw-r--r-- | server/middlewares/cache.ts | 72 |
1 files changed, 8 insertions, 64 deletions
diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts index 8ffe75700..ef8611875 100644 --- a/server/middlewares/cache.ts +++ b/server/middlewares/cache.ts | |||
@@ -1,72 +1,16 @@ | |||
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' | 1 | import { Redis } from '../lib/redis' |
5 | import { logger } from '../helpers/logger' | 2 | import * as apicache from 'apicache' |
6 | 3 | ||
7 | const lock = new AsyncLock({ timeout: 5000 }) | 4 | // Ensure Redis is initialized |
5 | Redis.Instance.init() | ||
8 | 6 | ||
9 | function cacheRoute (lifetimeArg: string | number) { | 7 | const options = { |
10 | return async function (req: express.Request, res: express.Response, next: express.NextFunction) { | 8 | redisClient: Redis.Instance.getClient(), |
11 | const redisKey = Redis.Instance.generateCachedRouteKey(req) | 9 | appendKey: () => Redis.Instance.getPrefix() |
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 | } | 10 | } |
69 | 11 | ||
12 | const cacheRoute = apicache.options(options).middleware | ||
13 | |||
70 | // --------------------------------------------------------------------------- | 14 | // --------------------------------------------------------------------------- |
71 | 15 | ||
72 | export { | 16 | export { |