Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.
Draft
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
10 changes: 10 additions & 0 deletions httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ type Transport struct {
// If nil, http.DefaultTransport is used
Transport http.RoundTripper
Cache Cache
// ShouldCache allows configuring non-standard caching behaviour on the basis of the
// response, allowing callers to cache things like 404 responses when that would be
// desired.
ShouldCache func(resp *http.Response) bool
// If true, responses returned from the cache will be given an extra header, X-From-Cache
MarkCachedResponses bool
}
Expand Down Expand Up @@ -198,6 +202,12 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
// In case of transport failure and stale-if-error activated, returns cached content
// when available
return cachedResp, nil
} else if t.ShouldCache != nil && t.ShouldCache(resp) {
// Should cache allows bypassing the behaviour on error, if we think the response
// should be cached.
//
// We leave this branch empty as we want to proceed onto the rest of the caching
// controlflow.
} else {
if err != nil || resp.StatusCode != http.StatusOK {
t.Cache.Delete(cacheKey)
Expand Down