数据缓存

大部分接口应该在响应头中携带 Last-Modified, ETag, Vary, Date 信息,客户端可以在随后请求这些资源的时候,在请求头中使用 If-Modified-Since, If-None-Match 等请求头来确认资源是否经过修改。

如果资源没有进行过修改,那么就可以响应 304 Not Modified 并且不在响应实体中返回任何内容。

  1. $ curl -i http://api.example.com/#{RESOURCE_URI}
  2. HTTP/1.1 200 OK
  3. Cache-Control: public, max-age=60
  4. Date: Thu, 05 Jul 2012 15:31:30 GMT
  5. Vary: Accept, Authorization
  6. ETag: "644b5b0155e6404a9cc4bd9d8b1ae730"
  7. Last-Modified: Thu, 05 Jul 2012 15:31:30 GMT
  8. Content
  1. $ curl -i http://api.example.com/#{RESOURCE_URI} -H "If-Modified-Since: Thu, 05 Jul 2012 15:31:30 GMT"
  2. HTTP/1.1 304 Not Modified
  3. Cache-Control: public, max-age=60
  4. Date: Thu, 05 Jul 2012 15:31:45 GMT
  5. Vary: Accept, Authorization
  6. Last-Modified: Thu, 05 Jul 2012 15:31:30 GMT
  1. $ curl -i http://api.example.com/#{RESOURCE_URI} -H 'If-None-Match: "644b5b0155e6404a9cc4bd9d8b1ae730"'
  2. HTTP/1.1 304 Not Modified
  3. Cache-Control: public, max-age=60
  4. Date: Thu, 05 Jul 2012 15:31:55 GMT
  5. Vary: Accept, Authorization
  6. ETag: "644b5b0155e6404a9cc4bd9d8b1ae730"
  7. Last-Modified: Thu, 05 Jul 2012 15:31:30 GMT

相关资料: