X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fredis.ts;h=06a34006012f5e0e6ae96a2e56ff23e16d1af5f9;hb=40e87e9ecc54e3513fb586928330a7855eb192c6;hp=41f4c986989c0d5754ad5f48abfbd23122008c74;hpb=d5b7d9110dd637a7f67ce9e430145314812a8df1;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/redis.ts b/server/lib/redis.ts index 41f4c9869..06a340060 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -1,8 +1,15 @@ +import * as express from 'express' import { createClient, RedisClient } from 'redis' import { logger } from '../helpers/logger' import { generateRandomString } from '../helpers/utils' import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' +type CachedRoute = { + body: string, + contentType?: string + statusCode?: string +} + class Redis { private static instance: Redis @@ -17,10 +24,7 @@ class Redis { if (this.initialized === true) return this.initialized = true - this.client = createClient({ - host: CONFIG.REDIS.HOSTNAME, - port: CONFIG.REDIS.PORT - }) + this.client = createClient(Redis.getRedisClient()) this.client.on('error', err => { logger.error('Error in Redis client.', { err }) @@ -34,6 +38,16 @@ class Redis { this.prefix = 'redis-' + CONFIG.WEBSERVER.HOST + '-' } + static getRedisClient () { + return Object.assign({}, + (CONFIG.REDIS.AUTH && CONFIG.REDIS.AUTH != null) ? { password: CONFIG.REDIS.AUTH } : {}, + (CONFIG.REDIS.DB) ? { db: CONFIG.REDIS.DB } : {}, + (CONFIG.REDIS.HOSTNAME && CONFIG.REDIS.PORT) ? + { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT } : + { path: CONFIG.REDIS.SOCKET } + ) + } + async setResetPasswordVerificationString (userId: number) { const generatedString = await generateRandomString(32) @@ -54,14 +68,32 @@ class Redis { return this.exists(this.buildViewKey(ip, videoUUID)) } - listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) { - return new Promise((res, rej) => { - this.client.sort(jobsPrefix + ':jobs:' + state, 'by', mode, order, 'LIMIT', offset.toString(), count.toString(), (err, values) => { - if (err) return rej(err) + async getCachedRoute (req: express.Request) { + const cached = await this.getObject(this.buildCachedRouteKey(req)) - return res(values) - }) - }) + return cached as CachedRoute + } + + setCachedRoute (req: express.Request, body: any, lifetime: number, contentType?: string, statusCode?: number) { + const cached: CachedRoute = { + body: body.toString(), + contentType, + statusCode: statusCode.toString() + } + + return this.setObject(this.buildCachedRouteKey(req), cached, lifetime) + } + + generateResetPasswordKey (userId: number) { + return 'reset-password-' + userId + } + + buildViewKey (ip: string, videoUUID: string) { + return videoUUID + '-' + ip + } + + buildCachedRouteKey (req: express.Request) { + return req.method + '-' + req.originalUrl } private getValue (key: string) { @@ -79,29 +111,47 @@ class Redis { this.client.set(this.prefix + key, value, 'PX', expirationMilliseconds, (err, ok) => { if (err) return rej(err) - if (ok !== 'OK') return rej(new Error('Redis result is not OK.')) + if (ok !== 'OK') return rej(new Error('Redis set result is not OK.')) return res() }) }) } - private exists (key: string) { - return new Promise((res, rej) => { - this.client.exists(this.prefix + key, (err, existsNumber) => { + private setObject (key: string, obj: { [ id: string ]: string }, expirationMilliseconds: number) { + return new Promise((res, rej) => { + this.client.hmset(this.prefix + key, obj, (err, ok) => { if (err) return rej(err) + if (!ok) return rej(new Error('Redis mset result is not OK.')) - return res(existsNumber === 1) + this.client.pexpire(this.prefix + key, expirationMilliseconds, (err, ok) => { + if (err) return rej(err) + if (!ok) return rej(new Error('Redis expiration result is not OK.')) + + return res() + }) }) }) } - private generateResetPasswordKey (userId: number) { - return 'reset-password-' + userId + private getObject (key: string) { + return new Promise<{ [ id: string ]: string }>((res, rej) => { + this.client.hgetall(this.prefix + key, (err, value) => { + if (err) return rej(err) + + return res(value) + }) + }) } - private buildViewKey (ip: string, videoUUID: string) { - return videoUUID + '-' + ip + private exists (key: string) { + return new Promise((res, rej) => { + this.client.exists(this.prefix + key, (err, existsNumber) => { + if (err) return rej(err) + + return res(existsNumber === 1) + }) + }) } static get Instance () {