From ab04bfc07bf169aaf50e403156c1a1cad65e60e9 Mon Sep 17 00:00:00 2001 From: shreeharsha-factly Date: Tue, 13 Dec 2022 16:53:57 +0530 Subject: [PATCH] fix cache issue --- api/util/cache/middleware.go | 7 +++---- api/util/cache/response.go | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/api/util/cache/middleware.go b/api/util/cache/middleware.go index 3c22ea802..5f20983ad 100644 --- a/api/util/cache/middleware.go +++ b/api/util/cache/middleware.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "io/ioutil" - "log" "net/http" "strings" @@ -24,8 +23,9 @@ type requestBody struct { func CachingMiddleware() func(http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - log.Println(" CachingMiddleware entry") + body := requestBody{} + spaceId := r.Header.Get("x-space") bodyBytes, _ := ioutil.ReadAll(r.Body) err := json.Unmarshal(bodyBytes, &body) if err != nil { @@ -50,7 +50,7 @@ func CachingMiddleware() func(http.Handler) http.Handler { // hash query h := md5.New() - _, _ = io.WriteString(h, fmt.Sprint(queryStr, varString)) + io.WriteString(h, fmt.Sprint(queryStr, varString, spaceId)) hash := hex.EncodeToString(h.Sum(nil)) respBodyBytes, err := GlobalCache.Get(r.Context(), hash) @@ -64,7 +64,6 @@ func CachingMiddleware() func(http.Handler) http.Handler { r.Body.Close() r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) - log.Println(" CachingMiddleware exit") next.ServeHTTP(w, r) }) } diff --git a/api/util/cache/response.go b/api/util/cache/response.go index 92996653c..b38017fb7 100644 --- a/api/util/cache/response.go +++ b/api/util/cache/response.go @@ -27,7 +27,6 @@ func (myrw *CacheResponseWriter) WriteHeader(header int) { func RespMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - log.Println(" RespMiddleware entry") // Create a response writer: crw := &CacheResponseWriter{ ResponseWriter: w, @@ -36,6 +35,7 @@ func RespMiddleware(next http.Handler) http.Handler { body := requestBody{} bodyBytes, _ := ioutil.ReadAll(r.Body) + spaceId := r.Header.Get("x-space") err := json.Unmarshal(bodyBytes, &body) if err != nil { w.WriteHeader(http.StatusInternalServerError) @@ -58,7 +58,7 @@ func RespMiddleware(next http.Handler) http.Handler { _ = json.Unmarshal(saveBytes, &data) - err = SaveToCache(r.Context(), fmt.Sprint(queryStr, varString), data) + err = SaveToCache(r.Context(), fmt.Sprint(queryStr, varString, spaceId), data) if err != nil { log.Println(err.Error()) } @@ -67,6 +67,5 @@ func RespMiddleware(next http.Handler) http.Handler { log.Printf("Failed to send out response: %v", err) } - log.Println(" RespMiddleware exit") }) }