| 7 | import { redisClient } from './rediscache.client'; |
| 8 | |
| 9 | export class RedisCache implements ICache { |
| 10 | private readonly logger = new Logger('RedisCache'); |
| 11 | private client: RedisClientType; |
| 12 | private conf: CacheConfRedis; |
| 13 | |
| 14 | constructor( |
| 15 | private readonly configService: ConfigService, |
| 16 | private readonly module: string, |
| 17 | ) { |
| 18 | this.conf = this.configService.get<CacheConf>('CACHE')?.REDIS; |
| 19 | this.client = redisClient.getConnection(); |
| 20 | } |
| 21 | async get(key: string): Promise<any> { |
| 22 | try { |
| 23 | return JSON.parse(await this.client.get(this.buildKey(key))); |
| 24 | } catch (error) { |
| 25 | this.logger.error(error); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | async hGet(key: string, field: string) { |
| 30 | try { |
| 31 | const data = await this.client.hGet(this.buildKey(key), field); |
| 32 | |
| 33 | if (data) { |
| 34 | return JSON.parse(data, BufferJSON.reviver); |
| 35 | } |
| 36 | |
| 37 | return null; |
| 38 | } catch (error) { |
| 39 | this.logger.error(error); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | async set(key: string, value: any, ttl?: number) { |
| 44 | try { |
| 45 | await this.client.setEx(this.buildKey(key), ttl || this.conf?.TTL, JSON.stringify(value)); |
| 46 | } catch (error) { |
| 47 | this.logger.error(error); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | async hSet(key: string, field: string, value: any) { |
| 52 | try { |
| 53 | const json = JSON.stringify(value, BufferJSON.replacer); |
| 54 | |
| 55 | await this.client.hSet(this.buildKey(key), field, json); |
| 56 | } catch (error) { |
| 57 | this.logger.error(error); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | async has(key: string) { |
| 62 | try { |
| 63 | return (await this.client.exists(this.buildKey(key))) > 0; |
| 64 | } catch (error) { |
| 65 | this.logger.error(error); |
| 66 | } |
nothing calls this directly
no outgoing calls
no test coverage detected