From 23892184ee7afc2c6d17f4cb8cb4658fd4be03cb Mon Sep 17 00:00:00 2001 From: k1LoW Date: Tue, 19 Dec 2023 15:21:11 +0900 Subject: [PATCH] Fix Cacher.Store signature --- rc.go | 6 +++--- testutil/cacher.go | 7 ++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/rc.go b/rc.go index 422482f..c367c77 100644 --- a/rc.go +++ b/rc.go @@ -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 { @@ -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 @@ -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())) diff --git a/testutil/cacher.go b/testutil/cacher.go index d7f6aca..39abda6 100644 --- a/testutil/cacher.go +++ b/testutil/cacher.go @@ -9,6 +9,7 @@ import ( "strings" "sync" "testing" + "time" ) var ( @@ -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 } @@ -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) @@ -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