Skip to main content

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 the cache query parameter:

Bypass Cache

This forces a fresh database query, bypassing any cached results.

Use Cache

This uses cached results if available (default behavior).
The cache query parameter allows clients to refresh stale data when needed.

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
Create the cache table:

Cache Behavior

What Gets Cached

The following operations are cached:
  • getMany - List queries
  • getOne - 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

In-memory caching can consume significant memory. Use Redis or database caching for production applications.

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:
  1. Verify TypeORM cache is configured
  2. Check that cache option is set in query configuration
  3. Ensure cache provider (Redis/database) is running
  4. Verify no ?cache=0 parameter is being sent

Stale Data Issues

If you’re seeing stale data:
  1. Reduce cache duration
  2. Implement cache invalidation on updates
  3. Use ?cache=0 to bypass cache when needed
  4. Clear cache after write operations

Memory Issues

If experiencing memory problems:
  1. Switch from in-memory to Redis caching
  2. Reduce cache duration
  3. Set Redis maxmemory policy
  4. Monitor and clear old cache entries