cache more

This commit is contained in:
crapStone 2024-11-21 23:47:11 +01:00
parent 92c88cff03
commit 8e008c895a
No known key found for this signature in database
GPG key ID: 22D4BF0CF7CC29C8
2 changed files with 31 additions and 13 deletions

View file

@ -68,8 +68,9 @@ func (f FileResponse) createHttpResponse(cacheKey string) (header http.Header, s
} }
type BranchTimestamp struct { type BranchTimestamp struct {
Branch string `json:"branch"` NotFound bool `json:"notFound"`
Timestamp time.Time `json:"timestamp"` Branch string `json:"branch,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
} }
type writeCacheReader struct { type writeCacheReader struct {

View file

@ -122,12 +122,17 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
err := json.Unmarshal(cacheMetadata.([]byte), &cache) err := json.Unmarshal(cacheMetadata.([]byte), &cache)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("[cache] failed to unmarshal metadata for: %s", cacheKey) 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") body, ok := client.responseCache.Get(cacheKey + "|Body")
if !ok { if !ok {
log.Error().Msgf("[cache] failed to get body for: %s", cacheKey) 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) cache.Body = body.([]byte)
@ -228,21 +233,33 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam
var stamp BranchTimestamp var stamp BranchTimestamp
err := json.Unmarshal(stampRaw.([]byte), &stamp) err := json.Unmarshal(stampRaw.([]byte), &stamp)
if err != nil { 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 return &BranchTimestamp{}, ErrorNotFound
} }
log.Trace().Msgf("[cache] use branch %q exist", branchName)
// This comes from the refactoring of the caching library. if stamp.NotFound {
// The branch as reported by the API was stored in the cache, and I'm not sure if there are log.Trace().Msgf("[cache] branch %q does not exist", branchName)
// situations where it differs from the name in the request, hence this is left here.
return &stamp, nil 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) branch, resp, err := client.sdkClient.GetRepoBranch(repoOwner, repoName, branchName)
if err != nil { if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound { if resp != nil && resp.StatusCode == http.StatusNotFound {
log.Trace().Msgf("[cache] set cache branch %q not found", branchName) 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") log.Error().Err(err).Msg("[cache] error on cache write")
} }
return &BranchTimestamp{}, ErrorNotFound return &BranchTimestamp{}, ErrorNotFound
@ -261,7 +278,7 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam
log.Trace().Msgf("set cache branch [%s] exist", branchName) log.Trace().Msgf("set cache branch [%s] exist", branchName)
jsonToCache, err := json.Marshal(stamp) jsonToCache, err := json.Marshal(stamp)
if err != nil { 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 { if err := client.responseCache.Set(cacheKey, jsonToCache, branchExistenceCacheTimeout); err != nil {
log.Error().Err(err).Msg("[cache] error on cache write") 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) cacheKey := fmt.Sprintf("%s/%s/%s", defaultBranchCacheKeyPrefix, repoOwner, repoName)
if branch, ok := client.responseCache.Get(cacheKey); ok { 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) repo, resp, err := client.sdkClient.GetRepo(repoOwner, repoName)