X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Fredis.ts;h=1e7c0a8211ebe791b1a1e27cdca0f2e2d7e87407;hb=0030284b0df2983914291d6fe83675e2aa892e6a;hp=b284cab8fde5ed45c6d47e4573c7322473a4a53f;hpb=b5c0e95544cec5a33cee3df41c1607d2a0cd5403;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/redis.ts b/server/lib/redis.ts index b284cab8f..1e7c0a821 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -1,7 +1,14 @@ +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' +import { CONFIG, FEEDS, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers' + +type CachedRoute = { + body: string, + contentType?: string + statusCode?: string +} class Redis { @@ -23,7 +30,7 @@ class Redis { }) this.client.on('error', err => { - logger.error('Error in Redis client.', err) + logger.error('Error in Redis client.', { err }) process.exit(-1) }) @@ -54,6 +61,32 @@ class Redis { return this.exists(this.buildViewKey(ip, videoUUID)) } + async getCachedRoute (req: express.Request) { + const cached = await this.getObject(this.buildCachedRouteKey(req)) + + return cached as CachedRoute + } + + setCachedRoute (req: express.Request, body: any, contentType?: string, statusCode?: number) { + const cached: CachedRoute = { + body: body.toString(), + contentType, + statusCode: statusCode.toString() + } + + return this.setObject(this.buildCachedRouteKey(req), cached, FEEDS.CACHE_LIFETIME) + } + + 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) + + return res(values) + }) + }) + } + private getValue (key: string) { return new Promise((res, rej) => { this.client.get(this.prefix + key, (err, value) => { @@ -69,13 +102,39 @@ 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 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.')) + + 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 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 exists (key: string) { return new Promise((res, rej) => { this.client.exists(this.prefix + key, (err, existsNumber) => { @@ -94,6 +153,10 @@ class Redis { return videoUUID + '-' + ip } + private buildCachedRouteKey (req: express.Request) { + return req.method + '-' + req.originalUrl + } + static get Instance () { return this.instance || (this.instance = new this()) }