如何基于返回的etag使用curl缓存api响应? [英] How to cache api response using curl, based on returned etag?

查看:114
本文介绍了如何基于返回的etag使用curl缓存api响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个可能对其他人也有帮助的快速问题。

我有一个REST服务,它将为端点上的每个GET查询返回一个正确的ETAG标头。

现在,我还需要创建一个curl http客户端来查询这些端点并使用etags。

根据我的理解,在对curl发出第一个请求后,我需要保存返回的etag供以后使用,这样在
上所有后续请求(直到服务器上的etag无效)都使用了缓存版本,因为服务器将返回304标头和空响应。

这是正确的吗?

This is a quick question that might help other too.
I have a rest service that will return a proper ETAG header for each GET query on it's endpoints.
Now, i also need to create a curl http client to query these endpoints and make use of the etags.
From my understanding, after making the first request with curl, i need to save the returned etag for later use, so that on all subsequent requests (till the etag invalidates on server) i use the cached version because the server will return a 304 header and an empty response.
Is this correct?

请记住,此http客户端将分布在许多服务器上,curl本身无法保存返回的
内容,我想我需要使用某种方式自己保存它如sqlite或fs缓存。

这是正确的吗?

Having in mind that this http client will be distributed across many servers and curl itself cannot save the returned content i assume i need to save it myself using something like sqlite or fs cache.
Is this correct?

基本上我想的工作流程是:

1.向服务器端点发出第一个curl请求(GET)

2.服务器返回带有正确etag和内容的http代码200

3.使用我从服务器得到的响应,使用sqlite或fs

保存内容和etag 4.接下来调用同一端点,发送带有etag值的If-None-Match头文件

5。服务器验证If-None-Match头文件

5.a-内容具有如果未更改,则会发送回304标头,并且没有响应。基于此,我从sqlite或fs中加载了本地缓存的内容。

5.b-内容已更改,一个200标头连同一个新的etag被发送回。基于此,我将保存新的etag和下一次的内容。

6.循环重复

Basically the workflow i am thinking would be like:
1. make first curl request (GET) to server endpoint
2. server returns http code 200 with proper etag and content
3. using this response i got from server, save the content and the etag using sqlite or fs
4. next call to the same endpoint, send a If-None-Match header with the etag value
5. server validates the If-None-Match header
5.a - the content has not been changed, a 304 header is sent back and no response. Based on that, i load the local cached content from sqlite or fs.
5.b - the content has been changed, a 200 header is sent back, along with a new etag. Based on that, i save the new etag and the content for next time.
6. the cycle repeats

这正确吗?

推荐答案

基于最近发生的事件和对此问题的自己思考,我会说是。

I would say yes, based on recent events and own pondering about the issue.

选择它,如果有的话,它可能对您有用,并且您很高兴。

Go for it, if any, it might work for you and you are happy.

这篇关于如何基于返回的etag使用curl缓存api响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆