So I am trying to use caching in my nestjs project with redis as a store but I am having a lot of issues with implementation errors.
This is my cache module
import { Module } from '@nestjs/common'
import { CacheModule } from '@nestjs/cache-manager'
import { ConfigModule, ConfigService } from '@nestjs/config'
import * as redisStore from 'cache-manager-redis-store'
@Module({
imports: [
CacheModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
isGlobal: true,
store: redisStore,
host: configService.get<string>('cache.host') || 'localhost',
port: configService.get<number>('cache.port') || 6379,
ttl: 60,
}),
inject: [ConfigService],
}),
],
})
export class MyCacheModule {}
I am importing this into my app.module.ts
simply as MyCacheModule
.
Service
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Inject, Injectable } from '@nestjs/common'
import { Cache } from 'cache-manager'
@Injectable()
export class ItemService {
constructor(@Inject(CACHE_MANAGER) private readonly _cache: Cache) {}
async getItem(id: string) {
const cachedUser = await this._cache.get(`item_${id}`)
if (cachedUser) {
return cachedUser
}
const item = {
id: 50,
name: 'item',
} /**This would be a database call */
await this._cache.set(`item_${id}`, item, 60) // Cache for 60 seconds
return item
}
}
This is a simple service where I am trying to use cache. However the problem I keep running into is Please make sure that the argument "CACHE_MANAGER" at index [0] is available in the ItemModule context
. I have added MyCacheModule into the ItemModule imports and still recieve the same error.
Does anyone have a simple caching in nestjs w/redis store solution?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744872067a4598318.html
评论列表(0条)