Overview
NestJS CRUD provides built-in caching capabilities for query results, helping you improve API performance by reducing database calls. Caching is particularly useful for:- Frequently accessed data
- Complex queries with joins
- Read-heavy applications
- Reducing database load
Prerequisites
Caching requires TypeORM’s cache configuration. First, configure your TypeORM connection:orm.config.ts
TypeORM supports multiple cache providers including in-memory, Redis, and database caching.
Basic Configuration
Enable caching for specific controllers:users.controller.ts
Global Configuration
Set default cache duration globally:main.ts
Cache Duration
Specify cache duration in milliseconds:Common Durations
Disabling Cache
Disable caching for specific controllers:Cache Control via Query Parameter
Clients can control caching using thecache query parameter:
Bypass Cache
Use Cache
Cache with TypeORM
In-Memory Caching
Default in-memory cache (development only):Redis Caching
Production-ready caching with Redis:orm.config.ts
Database Caching
Use database table for caching:orm.config.ts
Cache Behavior
What Gets Cached
The following operations are cached:getMany- List queriesgetOne- Single entity retrieval
Cache Keys
Cache keys are automatically generated based on:- Entity type
- Query parameters (filters, joins, sort, etc.)
- Field selection
- Pagination settings
Different query parameters result in different cache entries.
Cache Invalidation
Cache is automatically invalidated when:- Cache duration expires
- Client requests
?cache=0 - You manually clear the cache (see below)
Manual Cache Control
Clear cache programmatically:users.service.ts
Complete Example
Here’s a production-ready caching setup:TypeORM Configuration
orm.config.ts
App Module
app.module.ts
Controller with Caching
products.controller.ts
Global Configuration
main.ts
Performance Considerations
Cache Hit Ratio
Monitor cache effectiveness:Memory Usage
Cache Warming
Pre-populate cache on startup:Best Practices
1
Choose Appropriate Duration
Set cache duration based on how frequently data changes. Frequently updated data should have shorter cache times.
2
Use Redis in Production
Always use Redis or another external cache provider in production environments.
3
Monitor Cache Performance
Track cache hit rates and adjust durations accordingly.
4
Implement Cache Invalidation
Clear cache when data is updated to prevent serving stale data.
5
Consider Memory Limits
Set appropriate memory limits for your cache provider to prevent out-of-memory issues.
Troubleshooting
Cache Not Working
If caching isn’t working:- Verify TypeORM cache is configured
- Check that
cacheoption is set in query configuration - Ensure cache provider (Redis/database) is running
- Verify no
?cache=0parameter is being sent
Stale Data Issues
If you’re seeing stale data:- Reduce cache duration
- Implement cache invalidation on updates
- Use
?cache=0to bypass cache when needed - Clear cache after write operations
Memory Issues
If experiencing memory problems:- Switch from in-memory to Redis caching
- Reduce cache duration
- Set Redis
maxmemorypolicy - Monitor and clear old cache entries
Related Resources
- Global Configuration - Set global cache settings
- Query Options - Learn about query configuration
- TypeORM Caching Documentation - Official TypeORM cache guide