| 1 | 63 | import { Redis } from 'ioredis'; |
| 2 | | import type { IKeyValueStore, RedisConnection } from './kv-store'; |
| 3 | | |
| 4 | | export class RedisKeyValueStore implements IKeyValueStore { |
| 5 | 1 | private readonly client: Redis; |
| 6 | | |
| 7 | 47 | constructor(connection: RedisConnection) { |
| 8 | 62 | this.client = new Redis({ |
| 9 | 56 | host: connection.host, |
| 10 | 56 | port: connection.port, |
| 11 | 60 | maxRetriesPerRequest: 3, |
| 12 | 42 | lazyConnect: true, |
| 13 | 14 | }); |
| 14 | 0 | this.client.on('error', (error: Error) => { |
| 15 | 0 | if ((error as NodeJS.ErrnoException).code !== 'ECONNREFUSED') { |
| 16 | 0 | console.error(`[redis-kv-store] unexpected connection error: ${error.message}`); |
| 17 | 6 | } |
| 18 | 17 | }); |
| 19 | | } |
| 20 | | |
| 21 | 39 | async set(key: string, value: string): Promise<void> { |
| 22 | 82 | await this.client.set(key, value); |
| 23 | | } |
| 24 | | |
| 25 | 26 | async get(key: string): Promise<string | null> { |
| 26 | 66 | return this.client.get(key); |
| 27 | | } |
| 28 | | |
| 29 | 21 | async close(): Promise<void> { |
| 30 | 58 | await this.client.quit(); |
| 31 | | } |
| 32 | 2 | } |