Skip to content

Commit

Permalink
feat: query result should be cache on current request context.
Browse files Browse the repository at this point in the history
  • Loading branch information
vuongxuongminh committed Apr 28, 2022
1 parent a420d7c commit b2ceeab
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions caching_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,11 @@ func (c *Caching) handleQueryRequest(w http.ResponseWriter, r *cachingRequest, h
return err
}

bodyBuffCopy := bufferPool.Get().(*bytes.Buffer)
bodyBuffCopy.Reset()
bodyBuffCopy.Write(crw.buffer.Bytes())
err = c.cachingQueryResult(r.httpRequest.Context(), r, plan, crw.buffer.Bytes(), crw.Header().Clone())

go func(header http.Header) {
defer bufferPool.Put(bodyBuffCopy)
err := c.cachingQueryResult(c.ctxBackground, r, plan, bodyBuffCopy.Bytes(), header)

if err != nil {
c.logger.Info("fail to cache query result", zap.Error(err))
} else {
c.logger.Info("caching query result successful", zap.String("cache_key", plan.queryResultCacheKey))
}
}(crw.Header().Clone())
if err == nil {
c.logger.Info("caching query result successful", zap.String("cache_key", plan.queryResultCacheKey))
}
case CachingStatusHit:
for header, values := range result.Header {
w.Header()[header] = values
Expand All @@ -110,7 +101,7 @@ func (c *Caching) handleQueryRequest(w http.ResponseWriter, r *cachingRequest, h

go func() {
if err := c.swrQueryResult(c.ctxBackground, result, r, h); err != nil {
c.logger.Info("swr failed, can not update query result", zap.String("cache_key", plan.queryResultCacheKey), zap.Error(err))
c.logger.Error("swr failed, can not update query result", zap.String("cache_key", plan.queryResultCacheKey), zap.Error(err))
} else {
c.logger.Info("swr query result successful", zap.String("cache_key", plan.queryResultCacheKey))
}
Expand Down

0 comments on commit b2ceeab

Please sign in to comment.