我在NestJS文档之后实现缓存。
为此创建新项目,这样docs示例和这个没有区别。
import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [CacheModule.register()],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}import { CACHE_MANAGER, Controller, Get, Inject } from '@nestjs/common';
import { Cache } from 'cache-manager';
@Controller()
export class AppController {
constructor(
@Inject(CACHE_MANAGER) private cacheManager: Cache,
) {}
} @Get()
async getHello(): Promise<string> {
const value: string = await this.cacheManager.get('hello');
if (value === null) {
await this.cacheManager.set('hello', 'cache item', 60);
return 'no cache exists';
}
return value;
}但是我知道这个错误,我不知道为什么:
TypeError: store.get is not a function
at Object.get (.../node_modules/cache-manager/src/caching.ts:88:36)
at AppController.getHello我认为这是一份简单的工作。
所以似乎没有人知道这个错误(我查过了)。
发布于 2022-09-30 15:09:36
我也遇到了同样的错误,也搞不清楚。
node-cache-manager在最后一天才被更新到第5版。https://github.com/node-cache-manager/node-cache-manager/tags
因此,我更新了package.json以使用4.x版本
"cache-manager": "^4.0.0",现在,缓存按预期的方式工作。
密切关注包问题队列,以便进行进一步更新。
发布于 2022-10-16 08:42:47
我纠正了你的错误!请换衣服
await this.cacheManager.set('cache', 'cache', 25000);
至
const memoryCache = await memoryStore({ttl: 25000})
await memoryCache.set('cache', 'cache')
和
import { memoryStore } from 'cache-manager';
https://stackoverflow.com/questions/73908197
复制相似问题