Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import IORedis, { Redis } from 'ioredis'
- import { v4 as uuid } from 'uuid'
- import chalk from 'chalk'
- import { IPublisher } from '../interface/interface.publisher'
- import { ISubscriber } from '../interface/interface.subscriber'
- /**
- * more example implementation check here my repository this link below
- * https://github.com/restuwahyu13/express-microservices-jobs-portal/tree/main/companies-service
- */
- // publisher custom method
- export class Publisher {
- private static key: string
- private static unique: string
- constructor(configs: Readonly<IPublisher>) {
- Publisher.key = configs.key
- Publisher.unique = uuid()
- Publisher.set({ key: configs.key, unique: Publisher.unique })
- }
- public static get(): Record<string, any> {
- const options: Record<string, any> = {
- key: Publisher.key,
- unique: Publisher.unique
- }
- return options
- }
- private static set(config: Record<string, any>): void {
- Publisher.key = config.key
- Publisher.unique = config.unique
- }
- private redisConnect(): Redis {
- const ioRedis = new IORedis({
- host: process.env.REDIS_HOST,
- port: +process.env.REDIS_PORT,
- maxRetriesPerRequest: 50,
- connectTimeout: 25000,
- enableReadyCheck: true,
- enableAutoPipelining: true
- }) as IORedis.Redis
- return ioRedis
- }
- public async setString(keyName: string, data: string, expiredAt: number): Promise<void> {
- const ioRedis = this.redisConnect() as Redis
- await ioRedis.set(`${keyName}:${Publisher.get().unique}`, data)
- await ioRedis.expire(`${keyName}:${Publisher.get().unique}`, expiredAt)
- }
- public async setMap(keyName: string, data: Record<string, any>, expiredAt: number): Promise<void> {
- const ioRedis = this.redisConnect() as Redis
- await ioRedis.hset(`${keyName}:${Publisher.get().unique}`, { payload: JSON.stringify(data) })
- await ioRedis.expire(`${keyName}:${Publisher.get().unique}`, expiredAt)
- }
- }
- // Subscriber custom method
- export class Subscriber {
- private keyTo: string
- private keyFrom: string
- private uniqueId: string
- constructor(config: Readonly<ISubscriber>) {
- this.keyTo = config.key
- this.keyFrom = Publisher.get().key
- this.uniqueId = Publisher.get().unique
- }
- private redisConnect(): Redis {
- const ioRedis = new IORedis({
- host: process.env.REDIS_HOST,
- port: +process.env.REDIS_PORT,
- maxRetriesPerRequest: 50,
- connectTimeout: 25000,
- enableReadyCheck: true,
- enableAutoPipelining: true
- }) as Redis
- return ioRedis
- }
- public async getString(keyName: string): Promise<any> {
- if (this.keyTo == this.keyFrom) {
- const ioRedis = this.redisConnect() as Redis
- const response: string = await ioRedis.get(`${keyName}:${this.uniqueId}`)
- if (response) {
- return Promise.resolve(response)
- }
- return {}
- } else {
- return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this.keyTo} and Publisher: ${this.keyFrom}`)))
- }
- }
- public async getMap(keyName: string): Promise<any> {
- if (this.keyTo == this.keyFrom) {
- const ioRedis = this.redisConnect() as Redis
- const response: Record<string, any> = await ioRedis.hgetall(`${keyName}:${this.uniqueId}`)
- if (response) {
- return Promise.resolve(JSON.parse(response.payload))
- }
- return {}
- } else {
- return Promise.reject(chalk.red(new Error(`invalid key Subscriber: ${this.keyTo} and Publisher: ${this.keyFrom}`)))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement