⚡ Performance & Monitoring

Caching

Caching strategies using Redis and in-memory caching.

Redis Configuration

REDIS_ENABLED=true
REDIS_URL=redis://localhost:6379

Cache Layers

LayerScopeTTLUse Case
In-memoryPer-instanceShort (1-5 min)Frequently accessed configs
RedisSharedMedium (5-60 min)Session data, computed results
ResponsePer-requestVariesHTTP cache headers

NestJS Cache Manager

@Injectable()
export class EmployeeService {
  constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {}

  async getEmployee(id: string): Promise<IEmployee> {
    const cacheKey = `employee:${id}`;
    const cached = await this.cacheManager.get<IEmployee>(cacheKey);
    if (cached) return cached;

    const employee = await this.repository.findOne(id);
    await this.cacheManager.set(cacheKey, employee, 300); // 5 min TTL
    return employee;
  }
}

Cache Invalidation

Invalidate on mutations:

async updateEmployee(id: string, input: UpdateEmployeeDTO) {
  const result = await this.repository.update(id, input);
  await this.cacheManager.del(`employee:${id}`);
  return result;
}

Edit this page

Copyright © 2023-Present Ever Co. LTD.Docs by Sourcey