Caching Github API calls

前端 未结 1 1059
清歌不尽
清歌不尽 2021-02-05 17:17

I have a general question related to caching of API calls, in this instance calls to the Github API.

Let\'s say I have a page in my app that shows the filenames of a re

相关标签:
1条回答
  • 2021-02-05 18:08

    Would just using HTTP caching be good enough for your use case? The purpose of HTTP caching is not just to provide a way of not making requests if you already have a fresh response, rather - it also enables you to quickly validate if the response you already have in cache is valid (without the server sending the complete response again if it is fresh).

    Looking at GitHub API responses, I can see that GitHub is correctly setting the relevant HTTP headers (ETag, Last-modified, Cache-control).

    So, you just do a GET, e.g. for:

    GET https://api.github.com/users/izuzak/repos
    

    and this returns:

    200 OK
    ...
    ETag:"df739f00c5053d12ef3c625ad6b0fd08"
    Last-Modified:Thu, 14 Feb 2013 22:31:14 GMT
    ...
    

    Next time - you do a GET for the same resource, but also supply the relevant HTTP caching headers so that it is actually a conditional GET:

    GET https://api.github.com/users/izuzak/repos
    ...
    If-Modified-Since:Thu, 14 Feb 2013 22:31:14 GMT
    If-None-Match:"df739f00c5053d12ef3c625ad6b0fd08"
    ...
    

    And lo and behold - the server returns a 304 Not modified response and your HTTP client will pull the response from its cache:

    304 Not Modified
    

    So, GitHub API does HTTP caching right and you should use it. Granted, you have to use an HTTP client that supports HTTP caching also. The best thing is that if you get a 304 Not modified response - GitHub does not decrease your remaining API calls quota. See: http://developer.github.com/v3/#conditional-requests

    GitHub API also sets the Cache-Control: private, max-age=60 header, so you have 60 seconds of freshness -- which means that requests for the same resource made less than 60 seconds apart will not even be made to the server.

    Your reasoning about using a single conditional GET request to a resource that surely changes if anything in the repo changed (a resource showing the sha of HEAD, for example) sounds reasonable -- since if that resource hasn't changed, then you don't have to check the individual files since they haven't surely changed.

    0 讨论(0)
提交回复
热议问题