diff options
author | Chocobozzz <me@florianbigard.com> | 2019-04-25 11:27:13 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-04-25 11:27:13 +0200 |
commit | 47f6409bb8bc49a50027b9579bb651c0506b6912 (patch) | |
tree | bcf27fb4bffa2c10c5374da60457e025078c1acf /server/middlewares/cache.ts | |
parent | d0dba1fce6dcc0b91c0561eda8707c5c5bb6e626 (diff) | |
download | PeerTube-47f6409bb8bc49a50027b9579bb651c0506b6912.tar.gz PeerTube-47f6409bb8bc49a50027b9579bb651c0506b6912.tar.zst PeerTube-47f6409bb8bc49a50027b9579bb651c0506b6912.zip |
Use apicache instead of our broken implementation
Diffstat (limited to 'server/middlewares/cache.ts')
-rw-r--r-- | server/middlewares/cache.ts | 73 |
1 files changed, 8 insertions, 65 deletions
diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts index 091c82d92..ef8611875 100644 --- a/server/middlewares/cache.ts +++ b/server/middlewares/cache.ts | |||
@@ -1,73 +1,16 @@ | |||
1 | import * as express from 'express' | ||
2 | import * as AsyncLock from 'async-lock' | ||
3 | import { parseDurationToMs } 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 | const lifetime = parseDurationToMs(lifetimeArg) | 8 | redisClient: Redis.Instance.getClient(), |
11 | 9 | appendKey: () => Redis.Instance.getPrefix() | |
12 | return async function (req: express.Request, res: express.Response, next: express.NextFunction) { | ||
13 | const redisKey = Redis.Instance.generateCachedRouteKey(req) | ||
14 | |||
15 | try { | ||
16 | await lock.acquire(redisKey, async (done) => { | ||
17 | const cached = await Redis.Instance.getCachedRoute(req) | ||
18 | |||
19 | // Not cached | ||
20 | if (!cached) { | ||
21 | logger.debug('No cached results for route %s.', req.originalUrl) | ||
22 | |||
23 | const sendSave = res.send.bind(res) | ||
24 | const redirectSave = res.redirect.bind(res) | ||
25 | |||
26 | res.send = (body) => { | ||
27 | if (res.statusCode >= 200 && res.statusCode < 400) { | ||
28 | const contentType = res.get('content-type') | ||
29 | |||
30 | Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode) | ||
31 | .then(() => done()) | ||
32 | .catch(err => { | ||
33 | logger.error('Cannot cache route.', { err }) | ||
34 | return done(err) | ||
35 | }) | ||
36 | } else { | ||
37 | done() | ||
38 | } | ||
39 | |||
40 | return sendSave(body) | ||
41 | } | ||
42 | |||
43 | res.redirect = url => { | ||
44 | done() | ||
45 | |||
46 | return redirectSave(url) | ||
47 | } | ||
48 | |||
49 | return next() | ||
50 | } | ||
51 | |||
52 | if (cached.contentType) res.set('content-type', cached.contentType) | ||
53 | |||
54 | if (cached.statusCode) { | ||
55 | const statusCode = parseInt(cached.statusCode, 10) | ||
56 | if (!isNaN(statusCode)) res.status(statusCode) | ||
57 | } | ||
58 | |||
59 | logger.debug('Use cached result for %s.', req.originalUrl) | ||
60 | res.send(cached.body).end() | ||
61 | |||
62 | return done() | ||
63 | }) | ||
64 | } catch (err) { | ||
65 | logger.error('Cannot serve cached route.', { err }) | ||
66 | return next() | ||
67 | } | ||
68 | } | ||
69 | } | 10 | } |
70 | 11 | ||
12 | const cacheRoute = apicache.options(options).middleware | ||
13 | |||
71 | // --------------------------------------------------------------------------- | 14 | // --------------------------------------------------------------------------- |
72 | 15 | ||
73 | export { | 16 | export { |