Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// Note: AX-6 — no core equivalent for durations or wall-clock timestamps.
"time"

"dappco.re/go/core"
core "dappco.re/go"
coreio "dappco.re/go/io"
)

Expand Down Expand Up @@ -391,7 +391,9 @@
}

if err := cache.medium.Write(path, entryJSON); err != nil {
_ = restoreFileSnapshot(cache.medium, snapshot)
if restoreErr := restoreFileSnapshot(cache.medium, snapshot); restoreErr != nil {
return core.E("cache.set", "failed to restore cache file after write failure", core.ErrorJoin(err, restoreErr))
}
return core.E("cache.set", "failed to write cache file", err)
}
return nil
Expand Down Expand Up @@ -468,7 +470,7 @@
return cache.setBinary(key, data, contentType, ttl, false)
}

func (cache *Cache) setBinary(key string, data []byte, contentType string, ttl time.Duration, useDefaultTTL bool) error {

Check failure on line 473 in cache.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=dAppCore_go-cache&issues=AZ3VUExlBIKHU6rmSo9Y&open=AZ3VUExlBIKHU6rmSo9Y&pullRequest=6
if err := cache.ensureReady("cache.setBinary"); err != nil {
return err
}
Expand Down Expand Up @@ -515,14 +517,22 @@
}

if err := cache.medium.Write(binaryPath, string(data)); err != nil {
_ = restoreFileSnapshot(cache.medium, jsonSnapshot)
_ = restoreFileSnapshot(cache.medium, binarySnapshot)
if restoreErr := restoreFileSnapshot(cache.medium, jsonSnapshot); restoreErr != nil {
return core.E("cache.setBinary", "failed to restore binary metadata after payload write failure", core.ErrorJoin(err, restoreErr))
}
if restoreErr := restoreFileSnapshot(cache.medium, binarySnapshot); restoreErr != nil {
return core.E("cache.setBinary", "failed to restore binary payload after payload write failure", core.ErrorJoin(err, restoreErr))
}
return core.E("cache.setBinary", "failed to write binary payload", err)
}

if err := cache.medium.Write(jsonPath, metaJSON); err != nil {
_ = restoreFileSnapshot(cache.medium, binarySnapshot)
_ = restoreFileSnapshot(cache.medium, jsonSnapshot)
if restoreErr := restoreFileSnapshot(cache.medium, binarySnapshot); restoreErr != nil {
return core.E("cache.setBinary", "failed to restore binary payload after metadata write failure", core.ErrorJoin(err, restoreErr))
}
if restoreErr := restoreFileSnapshot(cache.medium, jsonSnapshot); restoreErr != nil {
return core.E("cache.setBinary", "failed to restore binary metadata after metadata write failure", core.ErrorJoin(err, restoreErr))
}
return core.E("cache.setBinary", "failed to write binary metadata", err)
}

Expand Down Expand Up @@ -1641,7 +1651,7 @@
// cache.CachedResponse{Status: 200, Headers: headers},
// bodyBytes,
// )
func (httpCache *HTTPCache) Put(req CachedRequest, resp CachedResponse, body []byte) error {

Check failure on line 1654 in cache.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=dAppCore_go-cache&issues=AZ3VUExlBIKHU6rmSo9Z&open=AZ3VUExlBIKHU6rmSo9Z&pullRequest=6
if err := httpCache.ensureReady("cache.HTTPCache.Put"); err != nil {
return err
}
Expand Down Expand Up @@ -1686,13 +1696,21 @@
}

if err := httpCache.medium.Write(binaryPath, string(body)); err != nil {
_ = restoreFileSnapshot(httpCache.medium, metaSnapshot)
_ = restoreFileSnapshot(httpCache.medium, binarySnapshot)
if restoreErr := restoreFileSnapshot(httpCache.medium, metaSnapshot); restoreErr != nil {
return core.E("cache.HTTPCache.Put", "failed to restore response metadata after body write failure", core.ErrorJoin(err, restoreErr))
}
if restoreErr := restoreFileSnapshot(httpCache.medium, binarySnapshot); restoreErr != nil {
return core.E("cache.HTTPCache.Put", "failed to restore response body after body write failure", core.ErrorJoin(err, restoreErr))
}
return core.E("cache.HTTPCache.Put", "failed to write cached response body", err)
}
if err := httpCache.medium.Write(metaPath, meta); err != nil {
_ = restoreFileSnapshot(httpCache.medium, binarySnapshot)
_ = restoreFileSnapshot(httpCache.medium, metaSnapshot)
if restoreErr := restoreFileSnapshot(httpCache.medium, binarySnapshot); restoreErr != nil {
return core.E("cache.HTTPCache.Put", "failed to restore response body after metadata write failure", core.ErrorJoin(err, restoreErr))
}
if restoreErr := restoreFileSnapshot(httpCache.medium, metaSnapshot); restoreErr != nil {
return core.E("cache.HTTPCache.Put", "failed to restore response metadata after metadata write failure", core.ErrorJoin(err, restoreErr))
}
return core.E("cache.HTTPCache.Put", "failed to write cached response metadata", err)
}

Expand Down
Loading
Loading