From 32a3a94050a293e0e6de0ab2958b8662bf0f36ac Mon Sep 17 00:00:00 2001 From: theSoenke Date: Fri, 19 Oct 2018 14:32:18 +0200 Subject: [PATCH 1/2] Use request params in cache key --- phraseapp/http_cache.go | 54 ++++++++++++++++++++++-------------- phraseapp/http_cache_test.go | 3 +- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/phraseapp/http_cache.go b/phraseapp/http_cache.go index d71cdb6..8f09843 100644 --- a/phraseapp/http_cache.go +++ b/phraseapp/http_cache.go @@ -83,7 +83,8 @@ func (client *httpCacheClient) RoundTrip(req *http.Request) (*http.Response, err } url := req.URL.String() - cachedResponse, err := client.getCache(req, url) + requestParams := requestParams(req) + cachedResponse, err := client.readCache(url, requestParams) if err != nil { if err.Error() != "no cache entry" { return nil, err @@ -100,18 +101,12 @@ func (client *httpCacheClient) RoundTrip(req *http.Request) (*http.Response, err if rsp.StatusCode == http.StatusNotModified { if client.debug { - log.Println("found in cache and returning cached body") + log.Println("found cache and returning cached body") } cachedResponse.setCachedResponse(rsp) return rsp, nil } - body, err := ioutil.ReadAll(rsp.Body) - if err != nil { - return nil, err - } - - rsp.Body = ioutil.NopCloser(bytes.NewReader(body)) err = handleResponseStatus(rsp, 200) if err != nil { return rsp, err @@ -121,24 +116,33 @@ func (client *httpCacheClient) RoundTrip(req *http.Request) (*http.Response, err if err != nil { return nil, err } - if cacheSize > client.cacheSizeMax { client.cache.EraseAll() } - etag := rsp.Header.Get("Etag") - cacheKey := md5sum(url) - encodedCache := cachedResponse.encode(rsp, url, etag, body) - err = client.cache.Write(cacheKey, encodedCache) - if err != nil { - return nil, err + err = client.writeCache(rsp, requestParams, url) + return rsp, err +} + +func requestParams(req *http.Request) string { + if req.Body != nil { + body, err := req.GetBody() + if err != nil { + return "" + } + requestBody, err := ioutil.ReadAll(body) + if err != nil { + return "" + } + + return string(requestBody) } - return rsp, nil + return "" } -func (client *httpCacheClient) getCache(req *http.Request, url string) (*cacheRecord, error) { - cache, err := client.cache.Read(md5sum(url)) +func (client *httpCacheClient) readCache(url string, requestParams string) (*cacheRecord, error) { + cache, err := client.cache.Read(md5sum(url + requestParams)) if err != nil { if client.debug { log.Println("doing request without etag") @@ -161,7 +165,15 @@ func (client *httpCacheClient) getCache(req *http.Request, url string) (*cacheRe return cachedResponse, nil } -func (record *cacheRecord) encode(rsp *http.Response, url string, etag string, body []byte) []byte { +func (client *httpCacheClient) writeCache(rsp *http.Response, requestParams string, url string) error { + body, err := ioutil.ReadAll(rsp.Body) + if err != nil { + return err + } + + rsp.Body = ioutil.NopCloser(bytes.NewReader(body)) + etag := rsp.Header.Get("Etag") + cacheKey := md5sum(url + requestParams) var buf bytes.Buffer encoder := gob.NewEncoder(&buf) encoder.Encode(cacheRecord{ @@ -179,8 +191,8 @@ func (record *cacheRecord) encode(rsp *http.Response, url string, etag string, b TransferEncoding: rsp.TransferEncoding, Trailer: rsp.Header, }}) - - return buf.Bytes() + err = client.cache.Write(cacheKey, buf.Bytes()) + return err } func (record *cacheRecord) setCachedResponse(rsp *http.Response) { diff --git a/phraseapp/http_cache_test.go b/phraseapp/http_cache_test.go index 970f27c..741a5c1 100644 --- a/phraseapp/http_cache_test.go +++ b/phraseapp/http_cache_test.go @@ -1,7 +1,6 @@ package phraseapp import ( - "fmt" "io" "io/ioutil" "net/http" @@ -20,6 +19,7 @@ func TestLocaleDownloadCaching(t *testing.T) { if etag != "123" { t.Errorf("etag should be '123' but is: '%s'", etag) } + w.WriteHeader(http.StatusNotModified) } else { w.WriteHeader(http.StatusOK) @@ -33,7 +33,6 @@ func TestLocaleDownloadCaching(t *testing.T) { client, _ := NewClient(Credentials{Host: server.URL}, false) cacheDir, _ := ioutil.TempDir("", "") - fmt.Println(cacheDir) client.EnableCaching(CacheConfig{ CacheDir: cacheDir, }) From 5c77c32cbd92d26e47589cb8425aa5c2d4cd6276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Behrendt?= Date: Sat, 20 Oct 2018 16:27:39 +0200 Subject: [PATCH 2/2] Extract cache key generation --- phraseapp/http_cache.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/phraseapp/http_cache.go b/phraseapp/http_cache.go index 8f09843..b3623ad 100644 --- a/phraseapp/http_cache.go +++ b/phraseapp/http_cache.go @@ -82,9 +82,8 @@ func (client *httpCacheClient) RoundTrip(req *http.Request) (*http.Response, err return http.DefaultTransport.RoundTrip(req) } - url := req.URL.String() - requestParams := requestParams(req) - cachedResponse, err := client.readCache(url, requestParams) + cacheKey := cacheKey(req) + cachedResponse, err := client.readCache(cacheKey) if err != nil { if err.Error() != "no cache entry" { return nil, err @@ -120,10 +119,16 @@ func (client *httpCacheClient) RoundTrip(req *http.Request) (*http.Response, err client.cache.EraseAll() } - err = client.writeCache(rsp, requestParams, url) + err = client.writeCache(cacheKey, req.URL.String(), rsp) return rsp, err } +func cacheKey(req *http.Request) string { + url := req.URL.String() + requestParams := requestParams(req) + return md5sum(url + requestParams) +} + func requestParams(req *http.Request) string { if req.Body != nil { body, err := req.GetBody() @@ -141,8 +146,8 @@ func requestParams(req *http.Request) string { return "" } -func (client *httpCacheClient) readCache(url string, requestParams string) (*cacheRecord, error) { - cache, err := client.cache.Read(md5sum(url + requestParams)) +func (client *httpCacheClient) readCache(cacheKey string) (*cacheRecord, error) { + cache, err := client.cache.Read(cacheKey) if err != nil { if client.debug { log.Println("doing request without etag") @@ -165,7 +170,7 @@ func (client *httpCacheClient) readCache(url string, requestParams string) (*cac return cachedResponse, nil } -func (client *httpCacheClient) writeCache(rsp *http.Response, requestParams string, url string) error { +func (client *httpCacheClient) writeCache(cacheKey string, url string, rsp *http.Response) error { body, err := ioutil.ReadAll(rsp.Body) if err != nil { return err @@ -173,7 +178,6 @@ func (client *httpCacheClient) writeCache(rsp *http.Response, requestParams stri rsp.Body = ioutil.NopCloser(bytes.NewReader(body)) etag := rsp.Header.Get("Etag") - cacheKey := md5sum(url + requestParams) var buf bytes.Buffer encoder := gob.NewEncoder(&buf) encoder.Encode(cacheRecord{