How to cache api response using curl, based on returned etag?

亡梦爱人 提交于 2019-12-08 02:04:18

问题


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?

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?

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

Is this correct?

来源:https://stackoverflow.com/questions/17061535/how-to-cache-api-response-using-curl-based-on-returned-etag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!