S SmartDocs
Chuỗi bài: nest.js typescript 27 dòng · Cập nhật 2026-02-03

cache.module.ts

nest.js/03-advanced/code/cache/cache.module.ts

import { DynamicModule, Module } from '@nestjs/common';
import { CacheService } from './cache.service';

export interface CacheModuleOptions {
  host: string;
  port: number;
  ttl?: number;
}

@Module({})
export class CacheModule {
  static forRoot(options: CacheModuleOptions): DynamicModule {
    return {
      module: CacheModule,
      providers: [
        {
          provide: 'CACHE_OPTIONS',
          useValue: options,
        },
        CacheService,
      ],
      exports: [CacheService],
      global: true,
    };
  }
}

Bài viết liên quan