From c1523333034513263711b2bd379c657369f51db6 Mon Sep 17 00:00:00 2001 From: j Date: Fri, 22 Mar 2024 01:27:28 -0400 Subject: [PATCH] refactored from db to redis --- src/rdrama/services/CommentProcessor.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/rdrama/services/CommentProcessor.ts b/src/rdrama/services/CommentProcessor.ts index 68952b9..d692f1b 100644 --- a/src/rdrama/services/CommentProcessor.ts +++ b/src/rdrama/services/CommentProcessor.ts @@ -1,6 +1,6 @@ import { Comment } from '../models/Comment'; -import { DatabaseService } from '../../db/services/Database'; import { CommentFetcher } from './CommentFetcher'; +import RedisCommentService from '../../redis/services/CommentService'; /** * CommentProcessor handles the retrieval and processing of comments from the r/Drama API. @@ -14,10 +14,11 @@ export class CommentProcessor { * Iterates through pages starting from the first page until the maximum page limit is reached * or there are no more comments to fetch. Each page's fetch operation is timed for performance analysis. * - * @returns {Promise} A promise that resolves to an array of comments fetched from the API. + * @returns {Promise} A promise that resolves void. * @throws {Error} Throws an error if there is a failure in fetching comments from the API. */ - static async processComments(maxPages: number = 10): Promise { + static async processComments(maxPages: number = 10): Promise { + let comments: Comment[] = []; let stopFetching = false; @@ -29,18 +30,24 @@ export class CommentProcessor { // Check if the comment was already processed in this batch if (comments.some(c => c.id === comment.id)) continue; - const exists = await DatabaseService.commentExists(comment.id.toString()); + const exists = await RedisCommentService.retrieveComment(comment.id.toString()); if (exists) { + console.log(`Comment ${comment.id} exists`) stopFetching = true; break; // Stop processing this batch of comments } - await DatabaseService.insertComment(comment) + // Cache new comment with a 3600-second expiration + await RedisCommentService.storeComment(comment); + + // Optionally publish the comment to a Redis channel for subscribers + await RedisCommentService.publishComment(comment); + console.log(`Published Comment ${comment.id}`) + // prevent accidental recognition of a previous comment comments.push(comment); } if (newComments.length === 0) break; // No more comments to fetch } - return comments; } }