1 import * as express from 'express'
2 import { createClient, RedisClient } from 'redis'
3 import { logger } from '../helpers/logger'
4 import { generateRandomString } from '../helpers/utils'
5 import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers'
15 private static instance: Redis
16 private initialized = false
17 private client: RedisClient
18 private prefix: string
20 private constructor () {}
23 // Already initialized
24 if (this.initialized === true) return
25 this.initialized = true
27 this.client = createClient({
28 host: CONFIG.REDIS.HOSTNAME,
29 port: CONFIG.REDIS.PORT,
33 this.client.on('error', err => {
34 logger.error('Error in Redis client.', { err })
38 if (CONFIG.REDIS.AUTH) {
39 this.client.auth(CONFIG.REDIS.AUTH)
42 this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-'
45 async setResetPasswordVerificationString (userId: number) {
46 const generatedString = await generateRandomString(32)
48 await this.setValue(this.generateResetPasswordKey(userId), generatedString, USER_PASSWORD_RESET_LIFETIME)
50 return generatedString
53 async getResetPasswordLink (userId: number) {
54 return this.getValue(this.generateResetPasswordKey(userId))
57 setView (ip: string, videoUUID: string) {
58 return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
61 async isViewExists (ip: string, videoUUID: string) {
62 return this.exists(this.buildViewKey(ip, videoUUID))
65 async getCachedRoute (req: express.Request) {
66 const cached = await this.getObject(this.buildCachedRouteKey(req))
68 return cached as CachedRoute
71 setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) {
72 const cached: CachedRoute = {
73 body: body.toString(),
75 statusCode: statusCode.toString()
78 return this.setObject(this.buildCachedRouteKey(req), cached, lifetime)
81 generateResetPasswordKey (userId: number) {
82 return 'reset-password-' + userId
85 buildViewKey (ip: string, videoUUID: string) {
86 return videoUUID + '-' + ip
89 buildCachedRouteKey (req: express.Request) {
90 return req.method + '-' + req.originalUrl
93 private getValue (key: string) {
94 return new Promise<string>((res, rej) => {
95 this.client.get(this.prefix + key, (err, value) => {
96 if (err) return rej(err)
103 private setValue (key: string, value: string, expirationMilliseconds: number) {
104 return new Promise<void>((res, rej) => {
105 this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => {
106 if (err) return rej(err)
108 if (ok !== 'OK') return rej(new Error('Redis set result is not OK.'))
115 private setObject (key: string, obj: { [ id: string ]: string }, expirationMilliseconds: number) {
116 return new Promise<void>((res, rej) => {
117 this.client.hmset(this.prefix + key, obj, (err, ok) => {
118 if (err) return rej(err)
119 if (!ok) return rej(new Error('Redis mset result is not OK.'))
121 this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => {
122 if (err) return rej(err)
123 if (!ok) return rej(new Error('Redis expiration result is not OK.'))
131 private getObject (key: string) {
132 return new Promise<{ [ id: string ]: string }>((res, rej) => {
133 this.client.hgetall(this.prefix + key, (err, value) => {
134 if (err) return rej(err)
141 private exists (key: string) {
142 return new Promise<boolean>((res, rej) => {
143 this.client.exists(this.prefix + key, (err, existsNumber) => {
144 if (err) return rej(err)
146 return res(existsNumber === 1)
151 static get Instance () {
152 return this.instance || (this.instance = new this())
156 // ---------------------------------------------------------------------------