पाठ 22 / 33

Caching

In-memory या Redis caching से performance improve करें।

Cache decorator

Method results को automatically cache करने के लिए @Cacheable() या @Cache() का उपयोग करें।

import { Cacheable } from '@nestjs/cache-manager';

@Cacheable({ ttl: 300 }) // cache for 5 minutes
findAll() {
  return this.usersService.findAll();
}

Redis cache

Cache store को configure करके in-memory से Redis में switch करें।

import { CacheModule } from '@nestjs/cache-manager';
import type { RedisClientOptions } from 'redis';

CacheModule.register<RedisClientOptions>({
  isGlobal: true,
  store: 'redis',
  host: 'localhost',
  port: 6379
})