perf(core): implement multi-level LRU caching for expensive computations and data retrieval (fixes #210)#221
Conversation
…putations and data retrieval (fixes Sitaram8472#210)
…putations and data retrieval (fixes Sitaram8472#210)
…ons and data retrieval (fixes Sitaram8472#210)
|
Hi maintainers, could you please review this PR? Technical Analysis for ECSoC26 Labeling:This PR implements Core/Architecture optimization by introducing a generic \LRUCache\ interface and integrating it across two primary data retrieval bottlenecks (Teacher Dashboard and Report Card Generation). By intercepting concurrent \countDocuments\ operations and heavy sequential MongoDB aggregations (User, Course, Submission trees, and Attendances), we significantly reduce database load and algorithmic latency. Since this involves core backend logic changes, performance optimization through caching layers, and reducing algorithmic complexity overhead on hot computational paths, it aligns perfectly with the Level 3 criteria. Could you please assign the \ECSoC26\ label along with \Level 3\ and \good-backend\ tags? Thank you! |
Summary
This PR implements an LRU (Least Recently Used) caching strategy across the core data-retrieval layers of the backend, specifically targeting high-latency reporting and high-frequency dashboard queries.
Motivation
Closes #210
The system was performing full database queries for every request to
generateReportCard(which sequentially queries User, Course, Submissions with populated relationships, and Attendance docs) andgetDashboardStats(which issues three concurrentcountDocumentsoperations). This lack of caching caused slow response times and redundant DB processing. By implementing an LRU cache with TTL limits and invalidation hooks, we significantly drop the aggregate query load on the database.Changes
backend/utils/cache.js:CacheInterfaceand an in-memoryLRUCacheclass that handles capacity-based eviction and configurable TTL (Time-To-Live).backend/utils/cache.test.js:set(),get(), LRU reordering, automatic eviction, TTL limits, and manualinvalidate()operations behave correctly.backend/controllers/reportController.js:generateReportCardwithmemoryCache.get(). Results are cached dynamically per student for 5 minutes.backend/controllers/teacherController.js:getDashboardStatsfor 1 minute.postNotice,deleteNotice,uploadResource,deleteResource,markAttendance) usingmemoryCache.invalidate()so that any mutation immediately clears stale cached statistics.Acceptance Criteria
Impact & Side Effects
/reportsand/teacher/statsendpoints.How to Test
node backend/utils/cache.test.jsto verify caching mechanics execute smoothly.