I'm implementing caching following the NestJS docs.
Create new project for this so there's no difference between docs example and this.
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;
}
but I get this error, I don't know why:
TypeError: store.get is not a function
at Object.get (.../node_modules/cache-manager/src/caching.ts:88:36)
at AppController.getHello
This is a simple job, I think.
So there's no one seems to get this error (I looked it up).
I came across the same error and could not figure it out either.
node-cache-manager was only updated to version 5 within the last day. https://github.com/node-cache-manager/node-cache-manager/tags
So I updated the package.json to use version 4.x
"cache-manager": "^4.0.0",
And now the caching works as expected.
Keep an eye on the package issue queue for further updates.
package.json
"dependencies": {
"cache-manager": "^4.1.0",
"cache-manager-redis-store": "^2.0.0"
}
"devDependencies": {
"@types/cache-manager": "^4.0.1"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With