Skip to content

Instantly share code, notes, and snippets.

@ahmadrosid
Created September 8, 2021 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadrosid/69c1dacab8e154d77cb391d013131ef4 to your computer and use it in GitHub Desktop.
Save ahmadrosid/69c1dacab8e154d77cb391d013131ef4 to your computer and use it in GitHub Desktop.
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