Skip to content

Commit

Permalink
Finish cache middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
toshinari123 committed Feb 5, 2024
1 parent 09366ae commit 20c4ac0
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions internal/handler/site/middleware/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 20c4ac0

Please sign in to comment.