【发布时间】:2020-08-17 20:00:40
【问题描述】:
我创建了两个函数,第一个 - Get 通过 id 从数据库中获取数据并缓存它,第二个 - POST 更新数据库中的数据。问题在于,我必须在获取后和更新后缓存。
谁能告诉我这是怎么做的?
我使用 NestJS,文档告诉我,缓存仅在 GET 控制器上工作。
https://docs.nestjs.com/techniques/caching
我的代码:
服务:
async getUser(userID: number): Promise<UserEntity> {
const user = await this.conn.getRepository(UserEntity).findOne(userID);
return user;
}
async updateUser(userID: number, dto: UserEntityDto): Promise<ResultDTO> {
const user = await this.conn.getRepository(UserEntity).findOne(configId);
//all logic
}
控制器:
@UseInterceptors(CacheInterceptor)
export class UserControleer{
constructor(
private readonly userService: UserService
){}
@Get('/:userID')
@CacheKey('custom_key')
@CacheTTL(20)
async getUser(
@Param('userID') userID: number
): Promise<UserEntity> {
return await this.userService.getUser(userID);
}
@Post('/:userID')
async updateUser(
@Param('userID') userID: number,
@Body() dto: UserDTO
): Promise<ResultDTO> {
return await this.userService.updateUser(userID, dto);
}
感谢您的帮助。
【问题讨论】:
标签: javascript node.js typescript caching nestjs