implementação do logger
This commit is contained in:
6
src/core/configs/cache/IRedisClient.ts
vendored
Normal file
6
src/core/configs/cache/IRedisClient.ts
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface IRedisClient {
|
||||
get<T>(key: string): Promise<T | null>;
|
||||
set<T>(key: string, value: T, ttlSeconds?: number): Promise<void>;
|
||||
del(key: string): Promise<void>;
|
||||
}
|
||||
|
||||
8
src/core/configs/cache/redis-client.adapter.provider.ts
vendored
Normal file
8
src/core/configs/cache/redis-client.adapter.provider.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
import { RedisClientAdapter } from './redis-client.adapter';
|
||||
export const RedisClientToken = 'RedisClientInterface';
|
||||
|
||||
export const RedisClientAdapterProvider = {
|
||||
provide: RedisClientToken,
|
||||
useClass: RedisClientAdapter,
|
||||
};
|
||||
24
src/core/configs/cache/redis-client.adapter.ts
vendored
Normal file
24
src/core/configs/cache/redis-client.adapter.ts
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { Redis } from 'ioredis';
|
||||
import { IRedisClient } from './IRedisClient';
|
||||
|
||||
@Injectable()
|
||||
export class RedisClientAdapter implements IRedisClient {
|
||||
constructor(
|
||||
@Inject('REDIS_CLIENT')
|
||||
private readonly redis: Redis
|
||||
) {}
|
||||
|
||||
async get<T>(key: string): Promise<T | null> {
|
||||
const data = await this.redis.get(key);
|
||||
return data ? JSON.parse(data) : null;
|
||||
}
|
||||
|
||||
async set<T>(key: string, value: T, ttlSeconds = 300): Promise<void> {
|
||||
await this.redis.set(key, JSON.stringify(value), 'EX', ttlSeconds);
|
||||
}
|
||||
|
||||
async del(key: string): Promise<void> {
|
||||
await this.redis.del(key);
|
||||
}
|
||||
}
|
||||
12
src/core/configs/cache/redis.module.ts
vendored
Normal file
12
src/core/configs/cache/redis.module.ts
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module, Global } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { RedisProvider } from './redis.provider';
|
||||
import { RedisClientAdapterProvider } from './redis-client.adapter.provider';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
imports: [ConfigModule],
|
||||
providers: [RedisProvider, RedisClientAdapterProvider],
|
||||
exports: [RedisProvider, RedisClientAdapterProvider],
|
||||
})
|
||||
export class CacheModule {}
|
||||
21
src/core/configs/cache/redis.provider.ts
vendored
Normal file
21
src/core/configs/cache/redis.provider.ts
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Provider } from '@nestjs/common';
|
||||
import Redis from 'ioredis';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
export const RedisProvider: Provider = {
|
||||
provide: 'REDIS_CLIENT',
|
||||
useFactory: (configService: ConfigService) => {
|
||||
const redis = new Redis({
|
||||
host: configService.get<string>('REDIS_HOST', '10.1.1.109'),
|
||||
port: configService.get<number>('REDIS_PORT', 6379),
|
||||
// password: configService.get<string>('REDIS_PASSWORD', ''),
|
||||
});
|
||||
|
||||
redis.on('error', (err) => {
|
||||
console.error('Erro ao conectar ao Redis:', err);
|
||||
});
|
||||
|
||||
return redis;
|
||||
},
|
||||
inject: [ConfigService],
|
||||
};
|
||||
Reference in New Issue
Block a user