fixed cache middleware writing to cache when http response status != 200. closes
@@ -54,6 +54,11 @@ func TileCacheHandler(next http.Handler) http.Handler {
return
}
+ // if nothing has been written to the buffer, don't write to the cache
+ if buff.Len() == 0 {
+ return
+ }
+
if err := cacher.Set(key, buff.Bytes()); err != nil {
log.Printf("cache response writer err: %v", err)
}
@@ -85,6 +90,8 @@ func newTileCacheResponseWriter(resp http.ResponseWriter, w io.Writer) http.Resp
// tileCacheResponsWriter wraps http.ResponseWriter (https://golang.org/pkg/net/http/#ResponseWriter)
// to additionally write the response to a cache when there is a cache MISS
type tileCacheResponseWriter struct {
+ // status response code
+ status int
resp http.ResponseWriter
multi io.Writer
}
@@ -97,10 +104,18 @@ func (w *tileCacheResponseWriter) Header() http.Header {
}
func (w *tileCacheResponseWriter) Write(b []byte) (int, error) {
+ // only write to the multi writer when http response == StatusOK
+ if w.status == http.StatusOK {
// write to our multi writer
return w.multi.Write(b)
}
+ // write to the original response writer
+ return w.resp.Write(b)
+}
+
func (w *tileCacheResponseWriter) WriteHeader(i int) {
+ w.status = i
+
w.resp.WriteHeader(i)
}
