Nestjs 服务级别缓存

gal*_*gal 8 nestjs

查看 Netsjs 文档,我可以看到一般方法是利用 CacheInterceptor 进行控制器级缓存,

我希望实现的是服务/数据库级缓存 - 用例主要用于其他服务所需的静态数据库数据,是否有办法扩展提供的缓存模块以在服务内使用?

ner*_*ast -3

您可以使用此处列出的缓存存储之一:https ://github.com/BryanDonovan/node-cache-manager#store-engines

然后,您只需在缓存模块中注册该存储(https://docs.nestjs.com/techniques/caching# Different- stores):

import * as redisStore from 'cache-manager-redis-store';
import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';

@Module({
  imports: [
    CacheModule.register({
      store: redisStore,
      host: 'localhost',
      port: 6379,
    }),
  ],
  controllers: [AppController],
})
export class ApplicationModule {}

Run Code Online (Sandbox Code Playgroud)