mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 14:26:58 +00:00
rm cache from fasthttp
This commit is contained in:
parent
33298aa8ff
commit
6fd9cbfafb
12 changed files with 425 additions and 250 deletions
|
@ -26,4 +26,12 @@ var (
|
|||
// 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 = 1024 * 1024
|
||||
)
|
||||
|
|
|
@ -37,35 +37,61 @@ func NewClient(giteaRoot, giteaAPIToken string, respCache cache.SetGetKey, follo
|
|||
}
|
||||
|
||||
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {
|
||||
rawBytes, resp, err := client.sdkClient.GetFile(targetOwner, targetRepo, ref, resource, client.supportLFS)
|
||||
reader, _, err := client.ServeRawContent(targetOwner, targetRepo, ref, resource)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
return rawBytes, nil
|
||||
case http.StatusNotFound:
|
||||
return nil, ErrorNotFound
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
|
||||
}
|
||||
defer reader.Close()
|
||||
return io.ReadAll(reader)
|
||||
}
|
||||
|
||||
func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource string) (io.ReadCloser, error) {
|
||||
func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource string) (io.ReadCloser, *http.Response, error) {
|
||||
// if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + o.timestamp()); ok && !cachedValue.(gitea.FileResponse).IsEmpty() {
|
||||
// cachedResponse = cachedValue.(gitea.FileResponse)
|
||||
reader, resp, err := client.sdkClient.GetFileReader(targetOwner, targetRepo, ref, resource, client.supportLFS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp != nil {
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
return reader, nil
|
||||
case http.StatusNotFound:
|
||||
return nil, ErrorNotFound
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
|
||||
// add caching
|
||||
|
||||
// Write the response body to the original request
|
||||
// var cacheBodyWriter bytes.Buffer
|
||||
// if res != nil {
|
||||
// if res.Header.ContentLength() > fileCacheSizeLimit {
|
||||
// // fasthttp else will set "Content-Length: 0"
|
||||
// ctx.Response().SetBodyStream(&strings.Reader{}, -1)
|
||||
//
|
||||
// err = res.BodyWriteTo(ctx.Response.BodyWriter())
|
||||
// } else {
|
||||
// // TODO: cache is half-empty if request is cancelled - does the ctx.Err() below do the trick?
|
||||
// err = res.BodyWriteTo(io.MultiWriter(ctx.Response().BodyWriter(), &cacheBodyWriter))
|
||||
// }
|
||||
// } else {
|
||||
// _, err = ctx.Write(cachedResponse.Body)
|
||||
// }
|
||||
|
||||
// if res != nil && res.Header.ContentLength() <= fileCacheSizeLimit && ctx.Err() == nil {
|
||||
// cachedResponse.Exists = true
|
||||
// cachedResponse.MimeType = mimeType
|
||||
// cachedResponse.Body = cacheBodyWriter.Bytes()
|
||||
// _ = fileResponseCache.Set(uri+"?timestamp="+o.timestamp(), cachedResponse, fileCacheTimeout)
|
||||
// }
|
||||
|
||||
return reader, resp.Response, err
|
||||
case http.StatusNotFound:
|
||||
|
||||
// add not exist caching
|
||||
// _ = fileResponseCache.Set(uri+"?timestamp="+o.timestamp(), gitea.FileResponse{
|
||||
// Exists: false,
|
||||
// }, fileCacheTimeout)
|
||||
|
||||
return nil, resp.Response, ErrorNotFound
|
||||
default:
|
||||
return nil, resp.Response, fmt.Errorf("unexpected status code '%d'", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (*BranchTimestamp, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue