@@ -33,11 +33,7 @@ func Cache(
3333 defaultExpire time.Duration ,
3434 opts ... Option ,
3535) gin.HandlerFunc {
36- cfg := & Config {
37- logger : Discard {},
38- hitCacheCallback : defaultHitCacheCallback ,
39- shareSingleFlightCallback : defaultShareSingleFlightCallback ,
40- }
36+ cfg := newConfig ()
4137
4238 for _ , opt := range opts {
4339 opt (cfg )
@@ -67,7 +63,7 @@ func Cache(
6763
6864 // read cache first
6965 {
70- respCache := & responseCache {}
66+ respCache := & ResponseCache {}
7167 err := cacheStore .Get (cacheKey , & respCache )
7268 if err == nil {
7369 replyWithCache (c , cfg , respCache )
@@ -99,7 +95,7 @@ func Cache(
9995
10096 inFlight = true
10197
102- respCache := & responseCache {}
98+ respCache := & ResponseCache {}
10399 respCache .fillWithCacheWriter (cacheWriter )
104100
105101 // only cache 2xx response
@@ -113,7 +109,7 @@ func Cache(
113109 })
114110
115111 if ! inFlight {
116- replyWithCache (c , cfg , rawRespCache .(* responseCache ))
112+ replyWithCache (c , cfg , rawRespCache .(* ResponseCache ))
117113 cfg .shareSingleFlightCallback (c )
118114 }
119115 }
@@ -141,16 +137,17 @@ func CacheByRequestPath(defaultCacheStore persist.CacheStore, defaultExpire time
141137}
142138
143139func init () {
144- gob .Register (& responseCache {})
140+ gob .Register (& ResponseCache {})
145141}
146142
147- type responseCache struct {
143+ // ResponseCache record the http response cache
144+ type ResponseCache struct {
148145 Status int
149146 Header http.Header
150147 Data []byte
151148}
152149
153- func (c * responseCache ) fillWithCacheWriter (cacheWriter * responseCacheWriter ) {
150+ func (c * ResponseCache ) fillWithCacheWriter (cacheWriter * responseCacheWriter ) {
154151 c .Status = cacheWriter .Status ()
155152 c .Data = cacheWriter .body .Bytes ()
156153 c .Header = cacheWriter .Header ().Clone ()
@@ -175,8 +172,10 @@ func (w *responseCacheWriter) WriteString(s string) (int, error) {
175172func replyWithCache (
176173 c * gin.Context ,
177174 cfg * Config ,
178- respCache * responseCache ,
175+ respCache * ResponseCache ,
179176) {
177+ cfg .beforeReplyWithCacheCallback (c , respCache )
178+
180179 c .Writer .WriteHeader (respCache .Status )
181180
182181 for key , values := range respCache .Header {
0 commit comments