From 20c4ac0f18cd7bed98f65758fc55f09334ce3339 Mon Sep 17 00:00:00 2001 From: toshinari123 Date: Mon, 5 Feb 2024 10:50:52 +0800 Subject: [PATCH] Finish cache middleware --- internal/handler/site/middleware/cache.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/internal/handler/site/middleware/cache.go b/internal/handler/site/middleware/cache.go index 8883f38..cc25b9b 100644 --- a/internal/handler/site/middleware/cache.go +++ b/internal/handler/site/middleware/cache.go @@ -36,18 +36,20 @@ func (ctx *CacheContext) Cache(site *site.Descriptor, next http.Handler) http.Ha return } - if info.Hash != "" { - value, found := ctx.cc.GetContent(ContentCacheKey{hash: info.Hash, compression: "none"}) //TODO: find compression type - if found { - reader := bytes.NewReader(value.Bytes()) - writer := httputil.NewTimeoutResponseWriter(w, 10*time.Second) - http.ServeContent(writer, r, path.Base(r.URL.Path), info.ModTime, reader) - return - } + value, found := ctx.cc.GetContent(ContentCacheKey{hash: info.Hash, compression: "none"}) //TODO: find compression type + if found { + reader := bytes.NewReader(value.Bytes()) + writer := httputil.NewTimeoutResponseWriter(w, 10*time.Second) + http.ServeContent(writer, r, path.Base(r.URL.Path), info.ModTime, reader) + return } rec := httptest.NewRecorder() next.ServeHTTP(rec, r) - //result := rec.Result() + + reader := bytes.NewReader(rec.Body.Bytes()) + ctx.cc.SetContent(ContentCacheKey{hash: info.Hash, compression: "none"}, reader) + writer := httputil.NewTimeoutResponseWriter(w, 10*time.Second) + http.ServeContent(writer, r, path.Base(r.URL.Path), info.ModTime, reader) }) }