diff --git a/.gitignore b/.gitignore index 42cd73d..efbba5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ -/vendor/ \ No newline at end of file +/vendor/ +.idea/ +.vs/ +.vscode/ diff --git a/cache.go b/cache.go index 3bf46cf..d9ead4e 100644 --- a/cache.go +++ b/cache.go @@ -159,6 +159,44 @@ func (c *Client) Middleware(next http.Handler) http.Handler { }) } +// Drop releases the underlying cache for the request's URL. +func (c *Client) Drop(r *http.Request) { + params := r.URL.Query() + key := generateKey(r.URL.String()) + + if r.Method == http.MethodPost && r.Body != nil { + body, err := io.ReadAll(r.Body) + defer r.Body.Close() + if err != nil { + return + } + + reader := io.NopCloser(bytes.NewBuffer(body)) + key = generateKeyWithBody(r.URL.String(), body) + r.Body = reader + } + + if _, ok := params[c.refreshKey]; ok { + delete(params, c.refreshKey) + + r.URL.RawQuery = params.Encode() + key = generateKey(r.URL.String()) + } + + c.adapter.Release(key) +} + +// AddOptions allows the addition or re-application of options after a client has been created. +func (c *Client) AddOptions(opts ...ClientOption) error { + for _, opt := range opts { + if err := opt(c); err != nil { + return err + } + } + + return nil +} + func (c *Client) cacheableMethod(method string) bool { for _, m := range c.methods { if method == m {