From 27582f9242e1911a1deab66221540e21a8ef805d Mon Sep 17 00:00:00 2001 From: j Date: Fri, 22 Mar 2024 01:27:58 -0400 Subject: [PATCH] initial commit of redis session and comment service --- src/redis/services/CommentService.ts | 24 ++++++++++++++ src/redis/session/SessionManager.ts | 48 ++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/redis/services/CommentService.ts create mode 100644 src/redis/session/SessionManager.ts diff --git a/src/redis/services/CommentService.ts b/src/redis/services/CommentService.ts new file mode 100644 index 0000000..8b74eb4 --- /dev/null +++ b/src/redis/services/CommentService.ts @@ -0,0 +1,24 @@ +import { Comment } from "../../rdrama/models/Comment"; +import RedisSessionManager from "../session/SessionManager"; + +class RedisCommentService { + + static async storeComment(comment: Comment): Promise { + const redisManager = RedisSessionManager.getInstance(); + await redisManager.storeObject('comment', comment.id.toString(), comment); + } + + static async retrieveComment(commentId: string): Promise { + const redisManager = RedisSessionManager.getInstance(); + const result = await redisManager.retrieveObject('comment', commentId); + if (result) return result as Comment; + return null; + } + + static async publishComment(comment: Comment): Promise { + const redisManager = RedisSessionManager.getInstance(); + return redisManager.publishObject('newCommentsChannel', comment); + } +} + +export default RedisCommentService \ No newline at end of file diff --git a/src/redis/session/SessionManager.ts b/src/redis/session/SessionManager.ts new file mode 100644 index 0000000..20d98ee --- /dev/null +++ b/src/redis/session/SessionManager.ts @@ -0,0 +1,48 @@ +import Redis from 'ioredis'; +import dotenv from 'dotenv'; + +dotenv.config(); + +class RedisSessionManager { + private static instance: RedisSessionManager; + public readonly client: Redis; + + private constructor() { + this.client = new Redis({ + host: process.env.REDIS_HOST, + port: Number(process.env.REDIS_PORT), + password: process.env.REDIS_PASSWORD || undefined, + showFriendlyErrorStack: true, + }); + } + + public static getInstance(): RedisSessionManager { + if (!RedisSessionManager.instance) { + RedisSessionManager.instance = new RedisSessionManager(); + } + return RedisSessionManager.instance; + } + + async storeObject(keyPrefix: string, objectId: string, data: Record, expirationSec: number = 3600): Promise { + const key = `${keyPrefix}:${objectId}`; + const dataToStore = JSON.stringify(data); + await this.client.set(key, dataToStore, 'EX', expirationSec); + } + + async retrieveObject(keyPrefix: string, objectId: string): Promise | null> { + const key = `${keyPrefix}:${objectId}`; + const data = await this.client.get(key); + return data ? JSON.parse(data) : null; + } + + async publishObject(channel: string, data: Record): Promise { + const dataToPublish = JSON.stringify(data); + return this.client.publish(channel, dataToPublish); + } + + async shutdown(): Promise { + await this.client.quit(); + } +} + +export default RedisSessionManager;