mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-04 13:54:08 +00:00
cache more
This commit is contained in:
parent
92c88cff03
commit
8e008c895a
2 changed files with 31 additions and 13 deletions
|
@ -68,8 +68,9 @@ func (f FileResponse) createHttpResponse(cacheKey string) (header http.Header, s
|
|||
}
|
||||
|
||||
type BranchTimestamp struct {
|
||||
Branch string `json:"branch"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
NotFound bool `json:"notFound"`
|
||||
Branch string `json:"branch,omitempty"`
|
||||
Timestamp time.Time `json:"timestamp,omitempty"`
|
||||
}
|
||||
|
||||
type writeCacheReader struct {
|
||||
|
|
|
@ -122,12 +122,17 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
|
|||
err := json.Unmarshal(cacheMetadata.([]byte), &cache)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("[cache] failed to unmarshal metadata for: %s", cacheKey)
|
||||
return nil, nil, http.StatusInternalServerError, err
|
||||
return nil, nil, http.StatusNotFound, err
|
||||
}
|
||||
|
||||
if !cache.Exists {
|
||||
return nil, nil, http.StatusNotFound, ErrorNotFound
|
||||
}
|
||||
|
||||
body, ok := client.responseCache.Get(cacheKey + "|Body")
|
||||
if !ok {
|
||||
log.Error().Msgf("[cache] failed to get body for: %s", cacheKey)
|
||||
return nil, nil, http.StatusInternalServerError, ErrorNotFound
|
||||
return nil, nil, http.StatusNotFound, ErrorNotFound
|
||||
}
|
||||
cache.Body = body.([]byte)
|
||||
|
||||
|
@ -228,21 +233,33 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam
|
|||
var stamp BranchTimestamp
|
||||
err := json.Unmarshal(stampRaw.([]byte), &stamp)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("[cache] failed to unmarshal metadata for: %s", cacheKey)
|
||||
log.Error().Err(err).Bytes("stamp", stampRaw.([]byte)).Msgf("[cache] failed to unmarshal timestamp for: %s", cacheKey)
|
||||
return &BranchTimestamp{}, ErrorNotFound
|
||||
}
|
||||
log.Trace().Msgf("[cache] use branch %q exist", branchName)
|
||||
// This comes from the refactoring of the caching library.
|
||||
// The branch as reported by the API was stored in the cache, and I'm not sure if there are
|
||||
// situations where it differs from the name in the request, hence this is left here.
|
||||
return &stamp, nil
|
||||
|
||||
if stamp.NotFound {
|
||||
log.Trace().Msgf("[cache] branch %q does not exist", branchName)
|
||||
|
||||
return &BranchTimestamp{}, ErrorNotFound
|
||||
|
||||
} else {
|
||||
log.Trace().Msgf("[cache] use branch %q exist", branchName)
|
||||
// This comes from the refactoring of the caching library.
|
||||
// The branch as reported by the API was stored in the cache, and I'm not sure if there are
|
||||
// situations where it differs from the name in the request, hence this is left here.
|
||||
return &stamp, nil
|
||||
}
|
||||
}
|
||||
|
||||
branch, resp, err := client.sdkClient.GetRepoBranch(repoOwner, repoName, branchName)
|
||||
if err != nil {
|
||||
if resp != nil && resp.StatusCode == http.StatusNotFound {
|
||||
log.Trace().Msgf("[cache] set cache branch %q not found", branchName)
|
||||
if err := client.responseCache.Set(cacheKey, []byte{}, branchExistenceCacheTimeout); err != nil {
|
||||
jsonToCache, err := json.Marshal(BranchTimestamp{NotFound: true})
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("[cache] marshaling empty timestamp has returned an error", cacheKey)
|
||||
}
|
||||
if err := client.responseCache.Set(cacheKey, jsonToCache, branchExistenceCacheTimeout); err != nil {
|
||||
log.Error().Err(err).Msg("[cache] error on cache write")
|
||||
}
|
||||
return &BranchTimestamp{}, ErrorNotFound
|
||||
|
@ -261,7 +278,7 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam
|
|||
log.Trace().Msgf("set cache branch [%s] exist", branchName)
|
||||
jsonToCache, err := json.Marshal(stamp)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("[cache] marshaling json timestamp for %q has returned an error", cacheKey)
|
||||
log.Error().Err(err).Msgf("[cache] marshaling timestamp for %q has returned an error", cacheKey)
|
||||
}
|
||||
if err := client.responseCache.Set(cacheKey, jsonToCache, branchExistenceCacheTimeout); err != nil {
|
||||
log.Error().Err(err).Msg("[cache] error on cache write")
|
||||
|
@ -273,7 +290,7 @@ func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (str
|
|||
cacheKey := fmt.Sprintf("%s/%s/%s", defaultBranchCacheKeyPrefix, repoOwner, repoName)
|
||||
|
||||
if branch, ok := client.responseCache.Get(cacheKey); ok {
|
||||
return branch.(string), nil
|
||||
return string(branch.([]byte)), nil
|
||||
}
|
||||
|
||||
repo, resp, err := client.sdkClient.GetRepo(repoOwner, repoName)
|
||||
|
|
Loading…
Add table
Reference in a new issue