Cache empty files & fix #303 (missing content cache)

This commit is contained in:
Moritz Marquardt 2024-04-01 21:59:09 +02:00
parent 46c8daacba
commit 48e919a7bf
2 changed files with 12 additions and 36 deletions

View file

@ -99,6 +99,7 @@ type writeCacheReader struct {
cacheKey string
cache cache.ICache
hasError bool
doNotCache bool
}
func (t *writeCacheReader) Read(p []byte) (n int, err error) {
@ -108,19 +109,20 @@ func (t *writeCacheReader) Read(p []byte) (n int, err error) {
log.Trace().Err(err).Msgf("[cache] original reader for %q has returned an error", t.cacheKey)
t.hasError = true
} else if n > 0 {
_, _ = t.buffer.Write(p[:n])
if t.buffer.Len()+n > int(fileCacheSizeLimit) {
t.doNotCache = true
t.buffer.Reset()
} else {
_, _ = t.buffer.Write(p[:n])
}
}
return
}
func (t *writeCacheReader) Close() error {
doWrite := !t.hasError
doWrite := !t.hasError && !t.doNotCache
fc := *t.fileResponse
fc.Body = t.buffer.Bytes()
if fc.IsEmpty() {
log.Trace().Msg("[cache] file response is empty")
doWrite = false
}
if doWrite {
err := t.cache.Set(t.cacheKey+"|Metadata", []byte(fc.MetadataAsString()), fileCacheTimeout)
if err != nil {