refactored from db to redis

master
j 2024-03-22 01:27:28 -04:00
parent 7e2a125570
commit c152333303
1 changed files with 13 additions and 6 deletions

View File

@ -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<Comment[]>} A promise that resolves to an array of comments fetched from the API.
* @returns {Promise<void>} 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<Comment[]> {
static async processComments(maxPages: number = 10): Promise<void> {
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;
}
}