Created
September 8, 2021 10:19
-
-
Save ahmadrosid/69c1dacab8e154d77cb391d013131ef4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@nestjs/common'; | |
import { REDIS_HOST, REDIS_PORT } from '../../config'; | |
import Redis from 'ioredis'; | |
import { get } from 'lodash'; | |
import { Logger } from '../logger/logger'; | |
import * as Sentry from '@sentry/node'; | |
@Injectable() | |
export class RedisService { | |
private client: Redis.Redis; | |
constructor() { | |
this.client = new Redis(parseInt(REDIS_PORT || '6379'), REDIS_HOST, { | |
showFriendlyErrorStack: true, | |
maxRetriesPerRequest: 0, | |
}); | |
this.client.addListener('error', this.errorListener); | |
} | |
private errorListener(error: Error) { | |
Logger.error(`Redis error syscall: ${get(error, 'syscall')}`, error); | |
Sentry.captureException(error); | |
} | |
async getHash(key: string, subKeys: string[]): Promise<string[]> { | |
return await this.client.hmget(key, subKeys).then((items) => items as string[]); | |
} | |
async getString(key: string) { | |
return await this.client.get(key).catch(() => null); | |
} | |
async setString(key: string, expiredAt: number, value: any) { | |
return await this.client.set(key, JSON.stringify(value), 'EX', expiredAt).catch(() => false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment