Код: Выделить всё
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('cache.host') || 'localhost',
port: configService.get('cache.port') || 6379,
ttl: 60,
}),
inject: [ConfigService],
}),
],
})
export class 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
}
}
< /code>
Это простая служба, в которой я пытаюсь использовать кэш. Однако проблема, с которой я продолжаю столкнуться, заключается в том, чтобы убедиться, что аргумент «cache_manager» в индексе [0] доступен в контексте ItemModule Подробнее здесь: https://stackoverflow.com/questions/794 ... port-error