Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If rc.ErrShouldNotUseCache, skip all caching processes. #76

Merged
merged 2 commits into from
Jun 13, 2024
Merged
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
5 changes: 4 additions & 1 deletion rc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var defaultHeaderNamesToMask = []string{
type Cacher interface {
// Load loads the request/response cache.
// If the cache is not found, it returns ErrCacheNotFound.
// If not caching, it returns ErrNoCache.
// If the cache is expired, it returns ErrCacheExpired.
// If not caching, it returns ErrShouldNotUseCache.
Load(req *http.Request) (cachedReq *http.Request, cachedRes *http.Response, err error)
// Store stores the response cache.
Store(req *http.Request, res *http.Response, expires time.Time) error
Expand Down Expand Up @@ -107,6 +107,9 @@ func (m *cacheMw) Handler(next http.Handler) http.Handler {
m.logger.Debug("cache expired", slog.String("host", preq.Host), slog.String("method", preq.Method), slog.String("url", preq.URL.String()), slog.Any("headers", m.maskHeader(preq.Header)))
case errors.Is(err, ErrShouldNotUseCache):
m.logger.Debug("should not use cache", slog.String("host", preq.Host), slog.String("method", preq.Method), slog.String("url", preq.URL.String()), slog.Any("headers", m.maskHeader(preq.Header)))
// Skip caching
next.ServeHTTP(w, req)
return
default:
m.logger.Error("failed to load cache", slog.String("error", err.Error()), slog.String("host", preq.Host), slog.String("method", preq.Method), slog.String("url", preq.URL.String()), slog.Any("headers", m.maskHeader(preq.Header)))
}
Expand Down
18 changes: 5 additions & 13 deletions testutil/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@ package testutil
import (
"crypto/sha1"
"encoding/hex"
"errors"
"io"
"net/http"
"strings"
"sync"
"testing"
"time"

"github.com/2manymws/rc"
)

var (
_ Cacher = &AllCache{}
_ Cacher = &GetOnlyCache{}
)

// errCacheNotFound is returned when the cache is not found
var errCacheNotFound error = errors.New("cache not found")

// errNoCache is returned if not caching
var errNoCache error = errors.New("no cache")

type Cacher interface {
Load(req *http.Request) (cachedReq *http.Request, cachedRes *http.Response, err error)
Store(req *http.Request, res *http.Response, expires time.Time) error
Expand Down Expand Up @@ -61,7 +56,7 @@ func (c *AllCache) Load(req *http.Request) (*http.Request, *http.Response, error
cc, ok := c.m[key]
c.mu.Unlock()
if !ok {
return nil, nil, errCacheNotFound
return nil, nil, rc.ErrCacheNotFound
}
cachedReq, cachedRes, err := decodeReqRes(cc)
if err != nil {
Expand Down Expand Up @@ -101,14 +96,14 @@ func NewGetOnlyCache(t testing.TB) *GetOnlyCache {
func (c *GetOnlyCache) Load(req *http.Request) (*http.Request, *http.Response, error) {
c.t.Helper()
if req.Method != http.MethodGet {
return nil, nil, errNoCache
return nil, nil, rc.ErrShouldNotUseCache
}
key := reqToKey(req)
c.mu.Lock()
cc, ok := c.m[key]
c.mu.Unlock()
if !ok {
return nil, nil, errCacheNotFound
return nil, nil, rc.ErrCacheNotFound
}
cachedReq, cachedRes, err := decodeReqRes(cc)
if err != nil {
Expand All @@ -121,9 +116,6 @@ func (c *GetOnlyCache) Load(req *http.Request) (*http.Request, *http.Response, e

func (c *GetOnlyCache) Store(req *http.Request, res *http.Response, expires time.Time) error {
c.t.Helper()
if req.Method != http.MethodGet {
return errNoCache
}
key := reqToKey(req)
cc, err := encodeReqRes(req, res)
if err != nil {
Expand Down
Loading