Skip to content

Commit

Permalink
Merge pull request #36 from 2manymws/fix-store-sig
Browse files Browse the repository at this point in the history
Fix Cacher.Store signature
  • Loading branch information
k1LoW authored Dec 19, 2023
2 parents 8cc4de2 + 2389218 commit 4ad3acb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
6 changes: 3 additions & 3 deletions rc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Cacher interface {
// If the cache is expired, it returns ErrCacheExpired.
Load(req *http.Request) (cachedReq *http.Request, cachedRes *http.Response, err error)
// Store stores the response cache.
Store(req *http.Request, res *http.Response) error
Store(req *http.Request, res *http.Response, expires time.Time) error
}

type Handler interface {
Expand Down Expand Up @@ -124,7 +124,7 @@ func (m *cacheMw) Handler(next http.Handler) http.Handler {
m.logger.Debug("cache used", slog.String("method", req.Method), slog.String("url", req.URL.String()))
return
}
ok, _ := m.cacher.Storable(req, res, now)
ok, expires := m.cacher.Storable(req, res, now)
if !ok {
m.logger.Debug("cache not storable", slog.String("method", req.Method), slog.String("url", req.URL.String()))
return
Expand All @@ -133,7 +133,7 @@ func (m *cacheMw) Handler(next http.Handler) http.Handler {
res.Body = io.NopCloser(bytes.NewReader(body))

// Store response as cache
if err := m.cacher.Store(req, res); err != nil {
if err := m.cacher.Store(req, res, expires); err != nil {
m.logger.Error("failed to store cache", err, slog.String("method", req.Method), slog.String("url", req.URL.String()))
}
m.logger.Debug("cache stored", slog.String("method", req.Method), slog.String("url", req.URL.String()))
Expand Down
7 changes: 4 additions & 3 deletions testutil/cacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"sync"
"testing"
"time"
)

var (
Expand All @@ -24,7 +25,7 @@ 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) error
Store(req *http.Request, res *http.Response, expires time.Time) error
Hit() int
}

Expand Down Expand Up @@ -71,7 +72,7 @@ func (c *AllCache) Load(req *http.Request) (*http.Request, *http.Response, error
return cachedReq, cachedRes, nil
}

func (c *AllCache) Store(req *http.Request, res *http.Response) error {
func (c *AllCache) Store(req *http.Request, res *http.Response, expires time.Time) error {
c.t.Helper()
key := reqToKey(req)
cc, err := encodeReqRes(req, res)
Expand Down Expand Up @@ -118,7 +119,7 @@ func (c *GetOnlyCache) Load(req *http.Request) (*http.Request, *http.Response, e
return cachedReq, cachedRes, nil
}

func (c *GetOnlyCache) Store(req *http.Request, res *http.Response) error {
func (c *GetOnlyCache) Store(req *http.Request, res *http.Response, expires time.Time) error {
c.t.Helper()
if req.Method != http.MethodGet {
return errNoCache
Expand Down

0 comments on commit 4ad3acb

Please sign in to comment.