pages-server/server/gitea/cache.go

107 lines
2.5 KiB
Go
Raw Normal View History

package gitea
2022-09-18 19:02:55 +00:00
import (
2022-09-18 20:41:52 +00:00
"bytes"
"fmt"
2022-09-18 20:41:52 +00:00
"io"
2022-09-18 19:02:55 +00:00
"net/http"
"time"
2022-09-18 20:41:52 +00:00
"codeberg.org/codeberg/pages/server/cache"
)
const (
// defaultBranchCacheTimeout specifies the timeout for the default branch cache. It can be quite long.
defaultBranchCacheTimeout = 15 * time.Minute
// branchExistenceCacheTimeout specifies the timeout for the branch timestamp & existence cache. It should be shorter
// than fileCacheTimeout, as that gets invalidated if the branch timestamp has changed. That way, repo changes will be
// picked up faster, while still allowing the content to be cached longer if nothing changes.
branchExistenceCacheTimeout = 5 * time.Minute
// fileCacheTimeout specifies the timeout for the file content cache - you might want to make this quite long, depending
// on your available memory.
// TODO: move as option into cache interface
fileCacheTimeout = 5 * time.Minute
// fileCacheSizeLimit limits the maximum file size that will be cached, and is set to 1 MB by default.
fileCacheSizeLimit = int64(1024 * 1024)
2022-09-18 19:02:55 +00:00
)
2022-07-27 13:39:46 +00:00
type FileResponse struct {
2022-09-18 19:02:55 +00:00
Exists bool
IsSymlink bool
ETag string
MimeType string
Body []byte
}
func (f FileResponse) IsEmpty() bool {
return len(f.Body) != 0
}
2022-07-27 13:39:46 +00:00
2022-09-18 19:02:55 +00:00
func (f FileResponse) createHttpResponse() *http.Response {
resp := &http.Response{
Header: make(http.Header),
}
if f.Exists {
resp.StatusCode = http.StatusOK
} else {
resp.StatusCode = http.StatusNotFound
}
if f.IsSymlink {
resp.Header.Set(giteaObjectTypeHeader, objTypeSymlink)
}
2022-11-07 22:21:35 +00:00
resp.Header.Set(ETagHeader, f.ETag)
resp.Header.Set(ContentTypeHeader, f.MimeType)
resp.Header.Set(ContentLengthHeader, fmt.Sprint(len(f.Body)))
resp.Header.Set(PagesCacheIndicatorHeader, "true")
2022-09-18 19:02:55 +00:00
return resp
}
2022-07-27 13:39:46 +00:00
type BranchTimestamp struct {
Branch string
Timestamp time.Time
}
2022-09-18 20:41:52 +00:00
type writeCacheReader struct {
2022-09-19 10:15:14 +00:00
rc io.ReadCloser
2022-09-18 20:41:52 +00:00
buff *bytes.Buffer
f *FileResponse
cacheKey string
cache cache.SetGetKey
hasErr bool
}
2022-07-27 13:39:46 +00:00
2022-09-18 20:41:52 +00:00
func (t *writeCacheReader) Read(p []byte) (n int, err error) {
2022-09-19 10:15:14 +00:00
n, err = t.rc.Read(p)
2022-09-18 20:41:52 +00:00
if err != nil {
t.hasErr = true
} else if n > 0 {
_, _ = t.buff.Write(p[:n])
}
return
}
2022-07-27 15:25:08 +00:00
2022-09-18 20:41:52 +00:00
func (t *writeCacheReader) Close() error {
if !t.hasErr {
2022-09-19 10:15:14 +00:00
fc := *t.f
fc.Body = t.buff.Bytes()
_ = t.cache.Set(t.cacheKey, fc, fileCacheTimeout)
2022-09-18 20:41:52 +00:00
}
2022-09-19 10:15:14 +00:00
return t.rc.Close()
2022-09-18 20:41:52 +00:00
}
2022-07-27 15:25:08 +00:00
2022-09-18 20:41:52 +00:00
func (f FileResponse) CreateCacheReader(r io.ReadCloser, cache cache.SetGetKey, cacheKey string) io.ReadCloser {
buf := []byte{}
return &writeCacheReader{
2022-09-19 10:15:14 +00:00
rc: r,
2022-09-18 20:41:52 +00:00
buff: bytes.NewBuffer(buf),
f: &f,
cacheKey: cacheKey,
}
}