简体   繁体   English

NestJs 中的 @Header('Cache-Control')

[英]@Header('Cache-Control') in NestJs

I have an NestJS REST api and noticed that Cache-Control header is not sent.我有一个 NestJS REST api 并注意到 Cache-Control header 没有发送。 I am not sure if i miss something with nestjs but asking google how to cache REST API get response with nestjs only leads me to NestJs cache manager which is on server in memory cachig.我不确定我是否错过了 Nestjs 的某些内容,但询问谷歌如何缓存 REST API 使用 nestjs 获得响应只会将我带到位于 ZCD69B4957F06CD818D7BF3D61980E2g 的服务器上的 NestJs 缓存管理器。 So I have couple of questions:所以我有几个问题:

  1. Is it not common to cache REST API data responses缓存 REST API 数据响应不常见吗
  2. Should I set @Header('Cache-Control', '..?') explicitly我应该明确设置 @Header('Cache-Control', '..?')
  3. Maybe it is better to cache on the client?也许在客户端缓存更好? (I know about data validity - data is not changing I am talking about articles) (我知道数据有效性 - 数据没有改变我在谈论文章)

Thanks for the answers感谢您的回答

Yes you can set it with the header annotation:是的,您可以使用 header 注释进行设置:

https://docs.nestjs.com/controllers#headers https://docs.nestjs.com/controllers#headers

@Get()
@Header('Cache-Control', 'max-age=3600')
get() {
  return 'This action adds a new cat';
}

btw.顺便提一句。 its in the rest definition that the should be cachable, just not every method: https://en.wikipedia.org/wiki/Representational_state_transfer#Cacheability它在 rest 定义中应该是可缓存的,但不是每种方法: https://en.wikipedia.org/wiki/Representational_state_transfer#Cacheability

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM