aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/cache.ts
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2018-07-21 23:00:25 +0200
committerChocobozzz <me@florianbigard.com>2018-07-24 14:08:44 +0200
commit3f6d68d9671ddb7ba1c4f3a35021b84856dafb6a (patch)
tree4a84a6a532fa042710c88edbaa5a5613173e00e3 /server/middlewares/cache.ts
parent4278710d5b48546709720fac46657b84bba52a18 (diff)
downloadPeerTube-3f6d68d9671ddb7ba1c4f3a35021b84856dafb6a.tar.gz
PeerTube-3f6d68d9671ddb7ba1c4f3a35021b84856dafb6a.tar.zst
PeerTube-3f6d68d9671ddb7ba1c4f3a35021b84856dafb6a.zip
adding initial support for nodeinfo
Diffstat (limited to 'server/middlewares/cache.ts')
-rw-r--r--server/middlewares/cache.ts13
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 @@
1import * as express from 'express' 1import * as express from 'express'
2import * as AsyncLock from 'async-lock' 2import * as AsyncLock from 'async-lock'
3import { parseDuration } from '../helpers/utils'
3import { Redis } from '../lib/redis' 4import { Redis } from '../lib/redis'
4import { logger } from '../helpers/logger' 5import { 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
54const cache = (duration: number | string) => {
55 const _lifetime = parseDuration(duration, 3600000)
56 return cacheRoute(_lifetime)
57}
58
53// --------------------------------------------------------------------------- 59// ---------------------------------------------------------------------------
54 60
55export { 61export {
56 cacheRoute 62 cacheRoute,
63 cache
57} 64}