Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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