diff options
author | Chocobozzz <me@florianbigard.com> | 2018-04-17 14:01:06 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-04-17 14:04:34 +0200 |
commit | 4195cd2bc5046d4cdf1c677c27cd41f427d7a13a (patch) | |
tree | 3300258d96a427da29cc4785686d741e336102bb /server/lib/redis.ts | |
parent | cff8b272b1631661b8d5f5f4b59bd534ad8c86ca (diff) | |
download | PeerTube-4195cd2bc5046d4cdf1c677c27cd41f427d7a13a.tar.gz PeerTube-4195cd2bc5046d4cdf1c677c27cd41f427d7a13a.tar.zst PeerTube-4195cd2bc5046d4cdf1c677c27cd41f427d7a13a.zip |
Add redis cache to feed route
Diffstat (limited to 'server/lib/redis.ts')
-rw-r--r-- | server/lib/redis.ts | 57 |
1 files changed, 55 insertions, 2 deletions
diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 41f4c9869..1e7c0a821 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts | |||
@@ -1,7 +1,14 @@ | |||
1 | import * as express from 'express' | ||
1 | import { createClient, RedisClient } from 'redis' | 2 | import { createClient, RedisClient } from 'redis' |
2 | import { logger } from '../helpers/logger' | 3 | import { logger } from '../helpers/logger' |
3 | import { generateRandomString } from '../helpers/utils' | 4 | import { generateRandomString } from '../helpers/utils' |
4 | import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' | 5 | import { CONFIG, FEEDS, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' |
6 | |||
7 | type CachedRoute = { | ||
8 | body: string, | ||
9 | contentType?: string | ||
10 | statusCode?: string | ||
11 | } | ||
5 | 12 | ||
6 | class Redis { | 13 | class Redis { |
7 | 14 | ||
@@ -54,6 +61,22 @@ class Redis { | |||
54 | return this.exists(this.buildViewKey(ip, videoUUID)) | 61 | return this.exists(this.buildViewKey(ip, videoUUID)) |
55 | } | 62 | } |
56 | 63 | ||
64 | async getCachedRoute (req: express.Request) { | ||
65 | const cached = await this.getObject(this.buildCachedRouteKey(req)) | ||
66 | |||
67 | return cached as CachedRoute | ||
68 | } | ||
69 | |||
70 | setCachedRoute (req: express.Request, body: any, contentType?: string, statusCode?: number) { | ||
71 | const cached: CachedRoute = { | ||
72 | body: body.toString(), | ||
73 | contentType, | ||
74 | statusCode: statusCode.toString() | ||
75 | } | ||
76 | |||
77 | return this.setObject(this.buildCachedRouteKey(req), cached, FEEDS.CACHE_LIFETIME) | ||
78 | } | ||
79 | |||
57 | listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) { | 80 | listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) { |
58 | return new Promise<string[]>((res, rej) => { | 81 | return new Promise<string[]>((res, rej) => { |
59 | this.client.sort(jobsPrefix + ':jobs:' + state, 'by', mode, order, 'LIMIT', offset.toString(), count.toString(), (err, values) => { | 82 | this.client.sort(jobsPrefix + ':jobs:' + state, 'by', mode, order, 'LIMIT', offset.toString(), count.toString(), (err, values) => { |
@@ -79,13 +102,39 @@ class Redis { | |||
79 | this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => { | 102 | this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => { |
80 | if (err) return rej(err) | 103 | if (err) return rej(err) |
81 | 104 | ||
82 | if (ok !== 'OK') return rej(new Error('Redis result is not OK.')) | 105 | if (ok !== 'OK') return rej(new Error('Redis set result is not OK.')) |
83 | 106 | ||
84 | return res() | 107 | return res() |
85 | }) | 108 | }) |
86 | }) | 109 | }) |
87 | } | 110 | } |
88 | 111 | ||
112 | private setObject (key: string, obj: { [ id: string ]: string }, expirationMilliseconds: number) { | ||
113 | return new Promise<void>((res, rej) => { | ||
114 | this.client.hmset(this.prefix + key, obj, (err, ok) => { | ||
115 | if (err) return rej(err) | ||
116 | if (!ok) return rej(new Error('Redis mset result is not OK.')) | ||
117 | |||
118 | this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => { | ||
119 | if (err) return rej(err) | ||
120 | if (!ok) return rej(new Error('Redis expiration result is not OK.')) | ||
121 | |||
122 | return res() | ||
123 | }) | ||
124 | }) | ||
125 | }) | ||
126 | } | ||
127 | |||
128 | private getObject (key: string) { | ||
129 | return new Promise<{ [ id: string ]: string }>((res, rej) => { | ||
130 | this.client.hgetall(this.prefix + key, (err, value) => { | ||
131 | if (err) return rej(err) | ||
132 | |||
133 | return res(value) | ||
134 | }) | ||
135 | }) | ||
136 | } | ||
137 | |||
89 | private exists (key: string) { | 138 | private exists (key: string) { |
90 | return new Promise<boolean>((res, rej) => { | 139 | return new Promise<boolean>((res, rej) => { |
91 | this.client.exists(this.prefix + key, (err, existsNumber) => { | 140 | this.client.exists(this.prefix + key, (err, existsNumber) => { |
@@ -104,6 +153,10 @@ class Redis { | |||
104 | return videoUUID + '-' + ip | 153 | return videoUUID + '-' + ip |
105 | } | 154 | } |
106 | 155 | ||
156 | private buildCachedRouteKey (req: express.Request) { | ||
157 | return req.method + '-' + req.originalUrl | ||
158 | } | ||
159 | |||
107 | static get Instance () { | 160 | static get Instance () { |
108 | return this.instance || (this.instance = new this()) | 161 | return this.instance || (this.instance = new this()) |
109 | } | 162 | } |