如何在 Nest js 中启用或禁用缓存?

Val*_*lie 5 nestjs

假设我们希望在开发服务器上运行时禁用缓存并在生产服务器上启用缓存机制,而不是添加和删除 CacheInterceptor 拦截器。我们该怎么做呢?

@Get()
@UseInterceptors(CacheInterceptor)
getData()
  return "here is your data";
}
Run Code Online (Sandbox Code Playgroud)

Авг*_*уст 2

我使用一个简单的解决方案解决了这个任务:

import { Module, CacheModule } from '@nestjs/common';

@Module({
    imports: [
        CacheModule.registerAsync({
            // just return undefined instead of cache store config
            useFactory: async () => !IS_DEV && myCacheStoreConfig,
        }),
    ],
    ...
})
Run Code Online (Sandbox Code Playgroud)