【问题标题】:Caching the response to a POST request made to a Firebase function缓存对 Firebase 函数发出的 POST 请求的响应
【发布时间】:2017-07-27 05:47:42
【问题描述】:

我在 Firebase 之上构建了一个 GraphQL API。 API 使用连接到 Firebase 托管的云函数来处理请求。

我的目的是缓存发送到 GraphQL API 的请求的响应。

一个ExpressJS中间件配置如下:

function setCacheHeaders(req, res, next) {
    res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
    return next();
}

如果我向 API 发送 GET 请求,我会看到它到达 CDN:

X-Cache: HIT
X-Cache-Hits: 1
X-Served-By: cache-lhr6343-LHR

但是,当我发送 POST 请求(这是与 GraphQL 端点交互的常用方式,即使它是只读操作)时,我注意到没有请求访问缓存。但是,响应包含以下标头:

HTTP/1.1 200 OK
Accept-Ranges: bytes
Connection: keep-alive
Content-Length: 311
Content-Type: application/json
Date: Thu, 27 Jul 2017 05:41:02 GMT
Server: nginx
Strict-Transport-Security: max-age=31556926
Vary: Accept-Encoding, Authorization, Cookie
Via: 1.1 varnish
X-Cache: MISS
X-Cache-Hits: 0
X-Served-By: cache-lcy1133-LCY
X-Timer: S1501134062.745716,VS0,VE640
cache-control: public, max-age=300, s-maxage=600
function-execution-id: xg0qjlghrtrv
x-cloud-trace-context: 02434a5f73159e24d949e74026bb8843, 02434a5f73159e24d949e74026bb8843
x-powered-by: Express

是否无法使用 Firebase 托管缓存对 POST 请求的响应?

【问题讨论】:

    标签: javascript firebase google-cloud-functions firebase-hosting


    【解决方案1】:

    没有。 POST 不是幂等的,并且由于您无法访问 Fastly(Firebase 托管 CDN 提供商)varnish 规则,您甚至无法“破解”来实现它。

    【讨论】:

      【解决方案2】:

      Firebase 托管不缓存 POST 响应。

      我不是 GraphQL 专家,但据我所知,他们允许使用 GET verb for non-mutating requests。虽然它需要您更改代码,但它允许您缓存响应。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        • 1970-01-01
        • 2015-04-20
        • 2017-11-30
        相关资源
        最近更新 更多