Less aggressive caching of GitHub API responses.

Only cache 200s, and only cache most responses for 1 hour.
This commit is contained in:
Mihai Parparita 2014-09-06 14:29:31 -07:00
parent bbd75638f1
commit abb4822197

View file

@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httputil"
"strings"
"time"
"appengine"
"appengine/memcache"
@ -46,7 +47,7 @@ func (t *CachingTransport) RoundTrip(req *http.Request) (resp *http.Response, er
}
}
resp, err = t.Transport.RoundTrip(req)
if err != nil {
if err != nil || resp.StatusCode != 200 {
return
}
respBytes, err := httputil.DumpResponse(resp, true)
@ -54,7 +55,19 @@ func (t *CachingTransport) RoundTrip(req *http.Request) (resp *http.Response, er
t.Context.Errorf("Error dumping bytes for cached response: %v", err)
return resp, nil
}
err = memcache.Set(t.Context, &memcache.Item{Key: cacheKey, Value: respBytes})
var expiration time.Duration = time.Hour
if strings.HasPrefix(req.URL.Path, "/repos/") &&
(strings.HasSuffix(req.URL.Path, "/commits") ||
strings.HasSuffix(req.URL.Path, "/stats/contributors")) {
expiration = 0
}
err = memcache.Set(
t.Context,
&memcache.Item{
Key: cacheKey,
Value: respBytes,
Expiration: expiration,
})
if err != nil {
t.Context.Errorf("Error setting cached response: %v", err)
}