import { createClient, RedisClient } from 'redis'
import { logger } from '../helpers/logger'
import { generateRandomString } from '../helpers/utils'
-import { CONFIG, USER_PASSWORD_RESET_LIFETIME } from '../initializers'
+import { CONFIG, USER_PASSWORD_RESET_LIFETIME, VIDEO_VIEW_LIFETIME } from '../initializers'
class Redis {
})
this.client.on('error', err => {
- logger.error('Error in Redis client.', err)
+ logger.error('Error in Redis client.', { err })
process.exit(-1)
})
return this.getValue(this.generateResetPasswordKey(userId))
}
+ setView (ip: string, videoUUID: string) {
+ return this.setValue(this.buildViewKey(ip, videoUUID), '1', VIDEO_VIEW_LIFETIME)
+ }
+
+ async isViewExists (ip: string, videoUUID: string) {
+ return this.exists(this.buildViewKey(ip, videoUUID))
+ }
+
+ 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) => {
+ if (err) return rej(err)
+
+ return res(values)
+ })
+ })
+ }
+
private getValue (key: string) {
return new Promise<string>((res, rej) => {
this.client.get(this.prefix + key, (err, value) => {
})
}
+ 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)
+ })
+ })
+ }
+
private generateResetPasswordKey (userId: number) {
return 'reset-password-' + userId
}
+ private buildViewKey (ip: string, videoUUID: string) {
+ return videoUUID + '-' + ip
+ }
+
static get Instance () {
return this.instance || (this.instance = new this())
}