是否可以在HTTP中缓存POST方法

如果您想知道是否可以缓存post请求,并尝试研究该问题的答案,那么您很可能不会成功。当搜索“缓存post请求”时,第一个结果是这个StackOverflow问题。

答案是令人困惑的,包括缓存应该如何工作,缓存如何根据RFC工作,缓存应该如何根据RFC工作,以及缓存在实践中如何工作。让我们从RFC开始,逐步演示浏览器的实际工作方式,然后讨论CDN、GraphQL和其他值得关注的领域。

RFC 2616

根据RFC,POST请求必须使缓存无效:

13.10 Invalidation After Updates or Deletions

..

Some HTTP methods MUST cause a cache to invalidate an entity. This is
either the entity referred to by the Request-URI, or by the Location
or Content-Location headers (if present). These methods are:
  - PUT
  - DELETE
  - POST

复制

这种语言表明POST请求是不可缓存的,但事实并非如此(在本例中)。高速缓存仅对先前存储的数据无效。RFC (似乎)明确说明,是的,您可以缓存POST请求:

9.5 POST

..

Responses to this method are not cacheable, unless the response
includes appropriate Cache-Control or Expires header fields. However,
the 303 (See Other) response can be used to direct the user agent to
retrieve a cacheable resource.

复制

尽管使用这种语言,但设置Cache-Control时不得缓存对同一资源的后续POST请求。必须将POST请求发送到服务器:

13.11 Write-Through Mandatory

..

All methods that might be expected to cause modifications to the
origin server's resources MUST be written through to the origin
server. This currently includes all methods except for GET and HEAD.
A cache MUST NOT reply to such a request from a client before having
transmitted the request to the inbound server, and having received a
corresponding response from the inbound server. This does not prevent
a proxy cache from sending a 100 (Continue) response before the
inbound server has sent its final reply.

复制

这怎么说得通呢?您不是在缓存POST请求,而是在缓存资源。

对于对同一资源的后续GET请求,只能缓存POST响应正文。在POST响应中设置LocationContent-Location标头,以传达正文所代表的资源。因此,在技术上缓存POST请求的唯一有效方法是对相同资源的后续GET。

正确答案是两者兼而有之:

  • “是的,RFC允许您将后续GET的POST请求缓存到相同的resource"
  • "no,。RFC不允许您缓存后续POST的POST请求,因为POST不是幂等的,必须直接写入服务器”

尽管RFC允许缓存对同一资源的请求,但实际上,浏览器和CDN不会实现此行为,也不允许您缓存POST请求。

资料来源:

浏览器行为演示

给定以下示例JavaScript应用程序(index.js):

const express = require('express')
const app = express()

let count = 0

app
    .get('/asdf', (req, res) => {
        count++
        const msg = `count is ${count}`
        console.log(msg)
        res
            .set('Access-Control-Allow-Origin', '*')
            .set('Cache-Control', 'public, max-age=30')
            .send(msg)
    })
    .post('/asdf', (req, res) => {
        count++
        const msg = `count is ${count}`
        console.log(msg)
        res
            .set('Access-Control-Allow-Origin', '*')
            .set('Cache-Control', 'public, max-age=30')
            .set('Content-Location', 'http://localhost:3000/asdf')
            .set('Location', 'http://localhost:3000/asdf')
            .status(201)
            .send(msg)
    })
    .set('etag', false)
    .disable('x-powered-by')
    .listen(3000, () => {
        console.log('Example app listening on port 3000!')
    })

复制

并给出以下示例网页(index.html):

<!DOCTYPE html>
<html>

<head>
    <script>
        async function getRequest() {
            const response = await fetch('http://localhost:3000/asdf')
            const text = await response.text()
            alert(text)
        }
        async function postRequest(message) {
            const response = await fetch(
                'http://localhost:3000/asdf',
                {
                    method: 'post',
                    body: { message },
                }
            )
            const text = await response.text()
            alert(text)
        }
    </script>
</head>

<body>
    <button onclick="getRequest()">Trigger GET request</button>
    <br />
    <button onclick="postRequest('trigger1')">Trigger POST request (body 1)</button>
    <br />
    <button onclick="postRequest('trigger2')">Trigger POST request (body 2)</button>
</body>

</html>

复制

安装NodeJS、Express并启动JavaScript应用程序。在浏览器中打开网页。尝试几种不同的方案来测试浏览器行为:

每次单击"Trigger GET request“时都会显示相同的" count”(HTTP高速缓存works).

  • Clicking "Trigger POST request“每次都会触发不同的计数(用于POST的HTTP高速缓存不会work).

  • Clicking "Trigger GET request”、"Trigger GET request“和"Trigger GET request”显示POST请求使GET请求的cache.

  • Clicking "Trigger POST request“无效然后单击"Trigger GET request”则显示浏览器不会为后续GET请求高速缓存POST请求,即使这是
  • 允许的。

这表明,即使您可以设置Cache-ControlContent-Location响应头,也无法让浏览器缓存HTTP POST请求。

我必须遵循RFC吗?

浏览器行为是不可配置的,但如果您不是浏览器,则不必受RFC规则的约束。

如果您正在编写应用程序代码,没有什么可以阻止您显式缓存POST请求(伪代码):

if (cache.get('hello')) {
  return cache.get('hello')
} else {
  response = post(url = 'http://somewebsite/hello', request_body = 'world')
  cache.put('hello', response.body)
  return response.body
}

复制

CDN、代理和网关也不必遵循RFC。例如,如果您使用Fastly作为您的CDN,Fastly允许您将custom VCL逻辑写入cache POST requests

我应该缓存POST请求吗?

POST请求是否应该缓存取决于上下文。

例如,您可以使用POST查询Elasticsearch或GraphQL,其中您的底层查询是幂等的。在这些情况下,缓存响应可能有意义,也可能没有意义,具体取决于用例。

在RESTful应用程序接口中,POST请求通常创建一个资源,不应该被缓存。这也是RFC对POST的理解,它不是一个幂等操作。

GraphQL

如果您使用的是GraphQL,并且需要跨CDN和浏览器进行HTTP缓存,请考虑使用GET method而不是POST发送查询是否符合您的要求。需要注意的是,不同的浏览器和CDN可能有不同的URI长度限制,但是操作安全列表(查询白名单)作为面向外部的生产GraphQL应用程序的最佳实践,可以缩短URI。

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值