]>
Commit | Line | Data |
---|---|---|
4195cd2b | 1 | import * as express from 'express' |
ecb4e35f C |
2 | import { createClient, RedisClient } from 'redis' |
3 | import { logger } from '../helpers/logger' | |
4 | import { generateRandomString } from '../helpers/utils' | |
fd4484f1 | 5 | import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' |
4195cd2b C |
6 | |
7 | type CachedRoute = { | |
8 | body: string, | |
2cebd797 C |
9 | contentType?: string |
10 | statusCode?: string | |
4195cd2b | 11 | } |
ecb4e35f C |
12 | |
13 | class Redis { | |
14 | ||
15 | private static instance: Redis | |
16 | private initialized = false | |
17 | private client: RedisClient | |
18 | private prefix: string | |
19 | ||
20 | private constructor () {} | |
21 | ||
22 | init () { | |
23 | // Already initialized | |
24 | if (this.initialized === true) return | |
25 | this.initialized = true | |
26 | ||
19f7b248 | 27 | this.client = createClient(Redis.getRedisClient()) |
ecb4e35f C |
28 | |
29 | this.client.on('error', err => { | |
d5b7d911 | 30 | logger.error('Error in Redis client.', { err }) |
ecb4e35f C |
31 | process.exit(-1) |
32 | }) | |
33 | ||
34 | if (CONFIG.REDIS.AUTH) { | |
35 | this.client.auth(CONFIG.REDIS.AUTH) | |
36 | } | |
37 | ||
38 | this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-' | |
39 | } | |
40 | ||
19f7b248 RK |
41 | static getRedisClient () { |
42 | return Object.assign({}, | |
43 | (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {}, | |
44 | (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {}, | |
45 | (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) ? | |
46 | { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } : | |
47 | { path: CONFIG.REDIS.SOCKET } | |
48 | ) | |
49 | } | |
50 | ||
ecb4e35f C |
51 | async setResetPasswordVerificationString (userId: number) { |
52 | const generatedString = await generateRandomString(32) | |
53 | ||
54 | await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME) | |
55 | ||
56 | return generatedString | |
57 | } | |
58 | ||
59 | async getResetPasswordLink (userId: number) { | |
60 | return this.getValue(this.generateResetPasswordKey(userId)) | |
61 | } | |
62 | ||
6b616860 | 63 | setIPVideoView (ip: string, videoUUID: string) { |
b5c0e955 C |
64 | return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME) |
65 | } | |
66 | ||
6b616860 | 67 | async isVideoIPViewExists (ip: string, videoUUID: string) { |
b5c0e955 C |
68 | return this.exists(this.buildViewKey(ip, videoUUID)) |
69 | } | |
70 | ||
4195cd2b C |
71 | async getCachedRoute (req: express.Request) { |
72 | const cached = await this.getObject(this.buildCachedRouteKey(req)) | |
73 | ||
74 | return cached as CachedRoute | |
75 | } | |
76 | ||
fd4484f1 | 77 | setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) { |
c1e791ba RK |
78 | const cached: CachedRoute = Object.assign({}, { |
79 | body: body.toString() | |
80 | }, | |
81 | (contentType) ? { contentType } : null, | |
82 | (statusCode) ? { statusCode: statusCode.toString() } : null | |
83 | ) | |
4195cd2b | 84 | |
fd4484f1 | 85 | return this.setObject(this.buildCachedRouteKey(req), cached, lifetime) |
4195cd2b C |
86 | } |
87 | ||
6b616860 C |
88 | addVideoView (videoId: number) { |
89 | const keyIncr = this.generateVideoViewKey(videoId) | |
90 | const keySet = this.generateVideosViewKey() | |
91 | ||
92 | return Promise.all([ | |
93 | this.addToSet(keySet, videoId.toString()), | |
94 | this.increment(keyIncr) | |
95 | ]) | |
96 | } | |
97 | ||
98 | async getVideoViews (videoId: number, hour: number) { | |
99 | const key = this.generateVideoViewKey(videoId, hour) | |
100 | ||
101 | const valueString = await this.getValue(key) | |
102 | return parseInt(valueString, 10) | |
103 | } | |
104 | ||
105 | async getVideosIdViewed (hour: number) { | |
106 | const key = this.generateVideosViewKey(hour) | |
107 | ||
108 | const stringIds = await this.getSet(key) | |
109 | return stringIds.map(s => parseInt(s, 10)) | |
110 | } | |
111 | ||
112 | deleteVideoViews (videoId: number, hour: number) { | |
113 | const keySet = this.generateVideosViewKey(hour) | |
114 | const keyIncr = this.generateVideoViewKey(videoId, hour) | |
115 | ||
116 | return Promise.all([ | |
117 | this.deleteFromSet(keySet, videoId.toString()), | |
118 | this.deleteKey(keyIncr) | |
119 | ]) | |
120 | } | |
121 | ||
122 | generateVideosViewKey (hour?: number) { | |
123 | if (!hour) hour = new Date().getHours() | |
124 | ||
125 | return `videos-view-h${hour}` | |
126 | } | |
127 | ||
128 | generateVideoViewKey (videoId: number, hour?: number) { | |
129 | if (!hour) hour = new Date().getHours() | |
130 | ||
131 | return `video-view-${videoId}-h${hour}` | |
132 | } | |
133 | ||
b40f0575 C |
134 | generateResetPasswordKey (userId: number) { |
135 | return 'reset-password-' + userId | |
136 | } | |
137 | ||
138 | buildViewKey (ip: string, videoUUID: string) { | |
139 | return videoUUID + '-' + ip | |
140 | } | |
141 | ||
142 | buildCachedRouteKey (req: express.Request) { | |
143 | return req.method + '-' + req.originalUrl | |
144 | } | |
145 | ||
ecb4e35f C |
146 | private getValue (key: string) { |
147 | return new Promise<string>((res, rej) => { | |
148 | this.client.get(this.prefix + key, (err, value) => { | |
149 | if (err) return rej(err) | |
150 | ||
151 | return res(value) | |
152 | }) | |
153 | }) | |
154 | } | |
155 | ||
6b616860 C |
156 | private getSet (key: string) { |
157 | return new Promise<string[]>((res, rej) => { | |
158 | this.client.smembers(this.prefix + key, (err, value) => { | |
159 | if (err) return rej(err) | |
160 | ||
161 | return res(value) | |
162 | }) | |
163 | }) | |
164 | } | |
165 | ||
166 | private addToSet (key: string, value: string) { | |
167 | return new Promise<string[]>((res, rej) => { | |
168 | this.client.sadd(this.prefix + key, value, err => err ? rej(err) : res()) | |
169 | }) | |
170 | } | |
171 | ||
172 | private deleteFromSet (key: string, value: string) { | |
173 | return new Promise<void>((res, rej) => { | |
174 | this.client.srem(this.prefix + key, value, err => err ? rej(err) : res()) | |
175 | }) | |
176 | } | |
177 | ||
178 | private deleteKey (key: string) { | |
179 | return new Promise<void>((res, rej) => { | |
180 | this.client.del(this.prefix + key, err => err ? rej(err) : res()) | |
181 | }) | |
182 | } | |
183 | ||
ecb4e35f C |
184 | private setValue (key: string, value: string, expirationMilliseconds: number) { |
185 | return new Promise<void>((res, rej) => { | |
186 | this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => { | |
187 | if (err) return rej(err) | |
188 | ||
4195cd2b | 189 | if (ok !== 'OK') return rej(new Error('Redis set result is not OK.')) |
ecb4e35f C |
190 | |
191 | return res() | |
192 | }) | |
193 | }) | |
194 | } | |
195 | ||
4195cd2b C |
196 | private setObject (key: string, obj: { [ id: string ]: string }, expirationMilliseconds: number) { |
197 | return new Promise<void>((res, rej) => { | |
198 | this.client.hmset(this.prefix + key, obj, (err, ok) => { | |
199 | if (err) return rej(err) | |
200 | if (!ok) return rej(new Error('Redis mset result is not OK.')) | |
201 | ||
202 | this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => { | |
203 | if (err) return rej(err) | |
204 | if (!ok) return rej(new Error('Redis expiration result is not OK.')) | |
205 | ||
206 | return res() | |
207 | }) | |
208 | }) | |
209 | }) | |
210 | } | |
211 | ||
212 | private getObject (key: string) { | |
213 | return new Promise<{ [ id: string ]: string }>((res, rej) => { | |
214 | this.client.hgetall(this.prefix + key, (err, value) => { | |
215 | if (err) return rej(err) | |
216 | ||
217 | return res(value) | |
218 | }) | |
219 | }) | |
220 | } | |
221 | ||
6b616860 C |
222 | private increment (key: string) { |
223 | return new Promise<number>((res, rej) => { | |
224 | this.client.incr(this.prefix + key, (err, value) => { | |
225 | if (err) return rej(err) | |
226 | ||
227 | return res(value) | |
228 | }) | |
229 | }) | |
230 | } | |
231 | ||
b5c0e955 C |
232 | private exists (key: string) { |
233 | return new Promise<boolean>((res, rej) => { | |
234 | this.client.exists(this.prefix + key, (err, existsNumber) => { | |
235 | if (err) return rej(err) | |
236 | ||
237 | return res(existsNumber === 1) | |
238 | }) | |
239 | }) | |
240 | } | |
241 | ||
ecb4e35f C |
242 | static get Instance () { |
243 | return this.instance || (this.instance = new this()) | |
244 | } | |
245 | } | |
246 | ||
247 | // --------------------------------------------------------------------------- | |
248 | ||
249 | export { | |
250 | Redis | |
251 | } |