Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cached data headers #7

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,42 @@ func (c *Cache) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(cachedResponse.StatusCode)
_, _ = rw.Write(cachedResponse.Body)
return
} else {
log.Printf("Failed to serialize response for caching: %s", err.Error())
_ = respClient.Delete(req.Context(), cacheKey)
}
log.Printf("Failed to serialize response for caching: %s", err.Error())
_ = respClient.Delete(req.Context(), cacheKey)

}

// Cache miss - record the response
recorder := &responseRecorder{rw: rw}
recorder := &responseRecorder{
rw: rw,
header: rw.Header().Clone(), // Initialize with the original headers.
}
c.next.ServeHTTP(recorder, req)

// Serialize the response data
cachedResponse := CachedResponse{
StatusCode: recorder.status,
Headers: recorder.Header().Clone(), // Convert http.Header to a map for serialization
Headers: recorder.Header(), // Convert http.Header to a map for serialization
Body: recorder.body.Bytes(),
}
var buffer bytes.Buffer
enc := gob.NewEncoder(&buffer)
if err := enc.Encode(cachedResponse); err != nil {
log.Printf("Failed to serialize response for caching: %s", err)
http.Error(rw, "Internal Server Error", http.StatusInternalServerError)
return
} else {
// Store the serialized response in Redis
if err := respClient.SetWithTTL(req.Context(), cacheKey, buffer.String(), c.cacheExpiry); err != nil {
log.Printf("Failed to cache response in Redis: %s", err.Error())
}
}

// Store the serialized response in Redis as a string with an expiration time
if err := respClient.SetWithTTL(req.Context(), cacheKey, buffer.String(), c.cacheExpiry); err != nil {
log.Println("Failed to cache response in Redis:", err)
if _, err := rw.Write(recorder.body.Bytes()); err != nil {
log.Printf("Failed to write response body: %s", err)
return
}

// Write the original response
rw.WriteHeader(recorder.status)
_, _ = rw.Write(recorder.body.Bytes())
return
}
8 changes: 4 additions & 4 deletions recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ type responseRecorder struct {
rw http.ResponseWriter
status int
body bytes.Buffer
header http.Header
}

func (r *responseRecorder) Header() http.Header {
return r.rw.Header()
return r.header
}

func (r *responseRecorder) Write(b []byte) (int, error) {
r.body.Write(b)
return r.rw.Write(b)
return r.body.Write(b) // Just buffer the body, don't write to rw

}

func (r *responseRecorder) WriteHeader(statusCode int) {
r.status = statusCode
r.rw.WriteHeader(statusCode)
}