Caching NestJS ClassSerializerInterceptor未应用于缓存结果?

Caching NestJS ClassSerializerInterceptor未应用于缓存结果?,caching,serialization,nestjs,Caching,Serialization,Nestjs,ClassSerializerInterceptor未应用于缓存结果。请考虑下面的示例代码。当我请求api端点时,私钥字段不可用。但是,如果缓存可用,我可以在结果中看到private_key字段 应用模块 @Module({ controllers: [...], exports: [AppConfigService, ...], imports: [ ... CacheModule.registerAsync({ // eslint-disable-ne

ClassSerializerInterceptor未应用于缓存结果。请考虑下面的示例代码。当我请求api端点时,私钥字段不可用。但是,如果缓存可用,我可以在结果中看到private_key字段

应用模块

@Module({
  controllers: [...],
  exports: [AppConfigService, ...],
  imports: [
    ...
    CacheModule.registerAsync({
      // eslint-disable-next-line @typescript-eslint/no-use-before-define
      imports: [AppModule],
      useClass: AppCacheOptionsFactory
    }),
    ...
  ],
  providers: [AppConfigService, ...]
})
export class AppModule {}

缓存工厂

@Injectable()
export class AppCacheOptionsFactory implements CacheOptionsFactory {
  constructor(private readonly appConfigService: AppConfigService) {}

  createCacheOptions(): Promise<CacheModuleOptions> | CacheModuleOptions {
    return {
      host: this.appConfigService.cacheHost,
      max: this.appConfigService.cacheMax,
      port: this.appConfigService.cachePort,
      store: redisStore,
      ttl: this.appConfigService.cacheTTL
    };
  }
}

@UseInterceptors(HttpCacheInterceptor,ClassSerializerInterceptor)

确保序列化程序侦听器在缓存侦听器之后运行,缓存将使用响应的序列化版本

如果您使用的是全局拦截器,那么还必须将HttpCacheInterceptor用作全局拦截器。
@UseInterceptors(HttpCacheInterceptor,ClassSerializerInterceptor)

确保序列化程序侦听器在缓存侦听器之后运行,缓存将使用响应的序列化版本

如果您使用的是全局拦截器,那么还必须将HttpCacheInterceptor用作全局拦截器。

您的SerializationInterceptor如何绑定到服务器/路由?这可能是一个问题,因为RxJS上下文是如何解析的。SerializationInterceptor是如何绑定到服务器/路由的?这可能是一个问题,因为RxJS上下文是如何解决的
...
@Get()
@UseInterceptors(HttpCacheInterceptor)
async find(): Promise<JwksEntity[]> {
  return this.userService.find();
}
...
@Injectable()
export class HttpCacheInterceptor extends CacheInterceptor {
  trackBy(context: ExecutionContext): string | undefined {
    const request = context.switchToHttp().getRequest<express.Request>();
    return request.path;
  }
}
@Entity({ name: "jwkss", orderBy: { id: "ASC" } })
export class JwksEntity {
  constructor(id: string, public_key: string, private_key: string) {
    this.id = id;
    this.public_key = public_key;
    this.private_key = private_key;
  }

  @PrimaryGeneratedColumn("uuid")
  @IsUUID()
  id: string;

  @Column({ type: "text" })
  @Exclude()
  @IsString()
  private_key: string;

  @Column({ type: "text" })
  @IsString()
  public_key: string;
}