http-cache.diff

· erock's pastes · raw

expires: 2025-01-26

 1diff --git a/cache.go b/cache.go
 2index 3bf46cf..0b6f8de 100644
 3--- a/cache.go
 4+++ b/cache.go
 5@@ -66,6 +66,7 @@ type Client struct {
 6 	refreshKey         string
 7 	methods            []string
 8 	writeExpiresHeader bool
 9+	cacheHitNotify chan string
10 }
11 
12 // ClientOption is used to set Client settings.
13@@ -126,6 +127,9 @@ func (c *Client) Middleware(next http.Handler) http.Handler {
14 						if c.writeExpiresHeader {
15 							w.Header().Set("Expires", response.Expiration.UTC().Format(http.TimeFormat))
16 						}
17+						if c.cacheHitNotify != nil {
18+							c.cacheHitNotify <- r.URL.String()
19+						}
20 						w.Write(response.Value)
21 						return
22 					}
23@@ -294,6 +298,15 @@ func ClientWithExpiresHeader() ClientOption {
24 	}
25 }
26 
27+// ClientWithNotify enables middleware to send the request uri through
28+// the channel specified whenever there's a cache hit
29+func ClientWithNotify(ch chan string) ClientOption {
30+	return func(c *Client) error {
31+		c.cacheHitNotify = ch
32+		return nil
33+	}
34+}
35+
36 type responseWriter struct {
37 	http.ResponseWriter
38 	statusCode int