diff options
Diffstat (limited to 'server/middlewares/cache.ts')
-rw-r--r-- | server/middlewares/cache.ts | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/server/middlewares/cache.ts b/server/middlewares/cache.ts index 1de44db70..1e5a13b2e 100644 --- a/server/middlewares/cache.ts +++ b/server/middlewares/cache.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import * as AsyncLock from 'async-lock' | 2 | import * as AsyncLock from 'async-lock' |
3 | import { parseDuration } from '../helpers/utils' | ||
3 | import { Redis } from '../lib/redis' | 4 | import { Redis } from '../lib/redis' |
4 | import { logger } from '../helpers/logger' | 5 | import { logger } from '../helpers/logger' |
5 | 6 | ||
@@ -20,7 +21,7 @@ function cacheRoute (lifetime: number) { | |||
20 | 21 | ||
21 | res.send = (body) => { | 22 | res.send = (body) => { |
22 | if (res.statusCode >= 200 && res.statusCode < 400) { | 23 | if (res.statusCode >= 200 && res.statusCode < 400) { |
23 | const contentType = res.getHeader('content-type').toString() | 24 | const contentType = res.get('content-type') |
24 | Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode) | 25 | Redis.Instance.setCachedRoute(req, body, lifetime, contentType, res.statusCode) |
25 | .then(() => done()) | 26 | .then(() => done()) |
26 | .catch(err => { | 27 | .catch(err => { |
@@ -35,7 +36,7 @@ function cacheRoute (lifetime: number) { | |||
35 | return next() | 36 | return next() |
36 | } | 37 | } |
37 | 38 | ||
38 | if (cached.contentType) res.contentType(cached.contentType) | 39 | if (cached.contentType) res.set('content-type', cached.contentType) |
39 | 40 | ||
40 | if (cached.statusCode) { | 41 | if (cached.statusCode) { |
41 | const statusCode = parseInt(cached.statusCode, 10) | 42 | const statusCode = parseInt(cached.statusCode, 10) |
@@ -50,8 +51,14 @@ function cacheRoute (lifetime: number) { | |||
50 | } | 51 | } |
51 | } | 52 | } |
52 | 53 | ||
54 | const cache = (duration: number | string) => { | ||
55 | const _lifetime = parseDuration(duration, 3600000) | ||
56 | return cacheRoute(_lifetime) | ||
57 | } | ||
58 | |||
53 | // --------------------------------------------------------------------------- | 59 | // --------------------------------------------------------------------------- |
54 | 60 | ||
55 | export { | 61 | export { |
56 | cacheRoute | 62 | cacheRoute, |
63 | cache | ||
57 | } | 64 | } |