mirror of
https://github.com/samsonjs/retrogit.git
synced 2026-04-27 15:07:43 +00:00
Less aggressive caching of GitHub API responses.
Only cache 200s, and only cache most responses for 1 hour.
This commit is contained in:
parent
bbd75638f1
commit
abb4822197
1 changed files with 15 additions and 2 deletions
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"appengine"
|
"appengine"
|
||||||
"appengine/memcache"
|
"appengine/memcache"
|
||||||
|
|
@ -46,7 +47,7 @@ func (t *CachingTransport) RoundTrip(req *http.Request) (resp *http.Response, er
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
resp, err = t.Transport.RoundTrip(req)
|
resp, err = t.Transport.RoundTrip(req)
|
||||||
if err != nil {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
respBytes, err := httputil.DumpResponse(resp, true)
|
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)
|
t.Context.Errorf("Error dumping bytes for cached response: %v", err)
|
||||||
return resp, nil
|
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 {
|
if err != nil {
|
||||||
t.Context.Errorf("Error setting cached response: %v", err)
|
t.Context.Errorf("Error setting cached response: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue