Website Configuration Cache Implementation Summary
Overview
Implemented a comprehensive caching system to eliminate redundant database queries for website management configuration across the entire codebase.
Performance Impact
Before
- Multiple DB queries per request to fetch website configuration
- Each service/controller queried
websiteManagementModelindependently - Estimated 5-10 DB queries eliminated per request cycle
After
- Single DB query at application startup
- All services use cached configuration
- Zero DB queries during normal operation
- Cache auto-refreshes when settings are updated
Files Modified
1. Core Cache Utility (src/utils/websiteConfig.util.ts)
- Enhanced to cache complete website configuration:
baseUrl- Website URLsiteName- Site namesiteLogo- Site logo URLcontactEmail- Contact notification emailsmtp- Complete SMTP configuration (host, port, secure, auth)
- Exports:
initializeWebsiteConfig()- Initialize cache at startupgetWebsiteConfig()- Get full cached configgetWebsiteBaseUrl()- Get just the base URLbuildThumbnailUrl()- Build thumbnail URLsrefreshWebsiteConfig()- Refresh cache after updates
2. Email Service (src/services/email/email.service.ts)
- Removed: Database query for SMTP settings
- Now uses: Cached SMTP configuration
- Impact: Eliminates 1 DB query per email sent
3. Contact Notification Service (src/services/email/contactNotification.service.ts)
- Removed: Database query for contact email
- Now uses: Cached contact email
- Impact: Eliminates 1 DB query per contact notification
4. Newsletter Cron (src/services/crons/newsletter.cron.ts)
- Removed: Database query for site info (name, logo, URL)
- Now uses: Cached site configuration
- Changed:
getNewsletterConfig()from async to sync - Impact: Eliminates 1 DB query per newsletter batch
5. Credential Controller (src/controllers/credential.controller.ts)
- Removed: 2 database queries:
- Site name for TOTP QR code generation
- Site URL for password reset links
- Now uses: Cached site configuration
- Impact: Eliminates 2 DB queries per authentication flow
6. Base Controller (src/controllers/base.controller.ts)
- Removed: Database query for website URL in thumbnail generation
- Now uses: Cached base URL + denormalized module_slug
- Impact: Eliminates 2 DB queries per item update with files
7. Website Management Controller (src/controllers/websiteManagement.controller.ts)
- Added: Auto-refresh cache on create/update
- Ensures cache stays synchronized with database
8. Application Startup (src/index.ts)
- Added: Cache initialization after DB connection
- Runs before server starts accepting requests
9. News Model & Interface
- Added:
module_slugfield for denormalization - Reduces need for module lookups
Cache Behavior
Initialization
// In src/index.ts
await connectDB();
await initializeWebsiteConfig(); // Cache initialized here
app.listen(PORT);
Auto-Refresh
Cache automatically refreshes when:
- Website settings are created
- Website settings are updated
Manual Refresh
import { refreshWebsiteConfig } from "./utils/websiteConfig.util";
await refreshWebsiteConfig();
Database Queries Eliminated
| Operation | Before | After | Savings |
|---|---|---|---|
| Email sending | 1 query | 0 queries | 100% |
| Contact notification | 1 query | 0 queries | 100% |
| Newsletter batch | 1 query | 0 queries | 100% |
| TOTP generation | 1 query | 0 queries | 100% |
| Password reset | 1 query | 0 queries | 100% |
| Thumbnail generation | 2 queries | 0 queries* | 100% |
*With fallback for backward compatibility
Total Impact
- Startup: +1 DB query (one-time initialization)
- Runtime: -5 to -10 DB queries per request cycle
- Update operations: +1 DB query (cache refresh)
- Net benefit: Massive reduction in database load
Testing Checklist
- Restart server and verify cache initialization log
- Test email sending (contact form, password reset)
- Test newsletter sending
- Test TOTP authentication
- Test file upload with thumbnail generation
- Update website settings and verify cache refresh
- Test with empty/missing website configuration
Monitoring
Watch for this log on startup:
[WebsiteConfig] Initialized with base URL: <your-domain>
If you see warnings, check that websiteManagement collection has proper data.
Future Enhancements
Consider caching other frequently accessed singleton data:
- Module list (if relatively static)
- System-wide settings
- Feature flags
- Rate limit configurations