mirror of
https://github.com/samsonjs/retrogit.git
synced 2026-04-27 15:07:43 +00:00
Make sure memcache keys are under the 250 byte limit.
This commit is contained in:
parent
4a45ada620
commit
5b5cf2ab6a
1 changed files with 12 additions and 3 deletions
|
|
@ -3,6 +3,9 @@ package retrogit
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -24,13 +27,19 @@ func (t *CachingTransport) RoundTrip(req *http.Request) (resp *http.Response, er
|
||||||
if req.Method != "GET" && req.Method != "HEAD" {
|
if req.Method != "GET" && req.Method != "HEAD" {
|
||||||
return t.Transport.RoundTrip(req)
|
return t.Transport.RoundTrip(req)
|
||||||
}
|
}
|
||||||
cacheKey := "CachingTransport:" + req.URL.String() + "#"
|
// The Go App Engine runtime has a 250 byte limit for memcache keys, so we
|
||||||
|
// need to hash the URL to make sure we stay under it.
|
||||||
|
cacheHash := md5.New()
|
||||||
|
io.WriteString(cacheHash, req.URL.String())
|
||||||
authorizationHeaders, ok := req.Header["Authorization"]
|
authorizationHeaders, ok := req.Header["Authorization"]
|
||||||
if ok {
|
if ok {
|
||||||
cacheKey += strings.Join(authorizationHeaders, "#")
|
for i := range authorizationHeaders {
|
||||||
|
io.WriteString(cacheHash, authorizationHeaders[i])
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
cacheKey += "Unauthorized"
|
io.WriteString(cacheHash, "Unauthorized")
|
||||||
}
|
}
|
||||||
|
cacheKey := fmt.Sprintf("CachingTransport:%x", cacheHash.Sum(nil))
|
||||||
|
|
||||||
cachedRespItem, err := memcache.Get(t.Context, cacheKey)
|
cachedRespItem, err := memcache.Get(t.Context, cacheKey)
|
||||||
if err != nil && err != memcache.ErrCacheMiss {
|
if err != nil && err != memcache.ErrCacheMiss {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue