]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/redis.ts
Update credits
[github/Chocobozzz/PeerTube.git] / server / lib / redis.ts
index 1fd366401c31445d455e6027983c90e2c5542e01..5bd55109c7b7c05065a9dfca1228b05752f15aff 100644 (file)
@@ -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
@@ -19,11 +26,12 @@ class Redis {
 
     this.client = createClient({
       host: CONFIG.REDIS.HOSTNAME,
-      port: CONFIG.REDIS.PORT
+      port: CONFIG.REDIS.PORT,
+      db: CONFIG.REDIS.DB
     })
 
     this.client.on('error', err => {
-      logger.error('Error in Redis client.', err)
+      logger.error('Error in Redis client.', { err })
       process.exit(-1)
     })
 
@@ -54,6 +62,22 @@ 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, lifetime: number, contentType?: string, statusCode?: number) {
+    const cached: CachedRoute = {
+      body: body.toString(),
+      contentType,
+      statusCode: statusCode.toString()
+    }
+
+    return this.setObject(this.buildCachedRouteKey(req), cached, lifetime)
+  }
+
   listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) {
     return new Promise<string[]>((res, rej) => {
       this.client.sort(jobsPrefix + ':jobs:' + state, 'by', mode, order, 'LIMIT', offset.toString(), count.toString(), (err, values) => {
@@ -64,6 +88,18 @@ class Redis {
     })
   }
 
+  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) {
     return new Promise<string>((res, rej) => {
       this.client.get(this.prefix + key, (err, value) => {
@@ -79,29 +115,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<boolean>((res, rej) => {
-      this.client.exists(this.prefix + key, (err, existsNumber) => {
+  private setObject (key: string, obj: { [ id: string ]: string }, expirationMilliseconds: number) {
+    return new Promise<void>((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<boolean>((res, rej) => {
+      this.client.exists(this.prefix + key, (err, existsNumber) => {
+        if (err) return rej(err)
+
+        return res(existsNumber === 1)
+      })
+    })
   }
 
   static get Instance () {