Skip to content

Commit

Permalink
feat: add BeforeReplyWithCacheCallback option (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyahui committed Feb 3, 2022
1 parent 4c3babe commit eca3d49
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
23 changes: 11 additions & 12 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ func Cache(
defaultExpire time.Duration,
opts ...Option,
) gin.HandlerFunc {
cfg := &Config{
logger: Discard{},
hitCacheCallback: defaultHitCacheCallback,
shareSingleFlightCallback: defaultShareSingleFlightCallback,
}
cfg := newConfig()

for _, opt := range opts {
opt(cfg)
Expand Down Expand Up @@ -67,7 +63,7 @@ func Cache(

// read cache first
{
respCache := &responseCache{}
respCache := &ResponseCache{}
err := cacheStore.Get(cacheKey, &respCache)
if err == nil {
replyWithCache(c, cfg, respCache)
Expand Down Expand Up @@ -99,7 +95,7 @@ func Cache(

inFlight = true

respCache := &responseCache{}
respCache := &ResponseCache{}
respCache.fillWithCacheWriter(cacheWriter)

// only cache 2xx response
Expand All @@ -113,7 +109,7 @@ func Cache(
})

if !inFlight {
replyWithCache(c, cfg, rawRespCache.(*responseCache))
replyWithCache(c, cfg, rawRespCache.(*ResponseCache))
cfg.shareSingleFlightCallback(c)
}
}
Expand Down Expand Up @@ -141,16 +137,17 @@ func CacheByRequestPath(defaultCacheStore persist.CacheStore, defaultExpire time
}

func init() {
gob.Register(&responseCache{})
gob.Register(&ResponseCache{})
}

type responseCache struct {
// ResponseCache record the http response cache
type ResponseCache struct {
Status int
Header http.Header
Data []byte
}

func (c *responseCache) fillWithCacheWriter(cacheWriter *responseCacheWriter) {
func (c *ResponseCache) fillWithCacheWriter(cacheWriter *responseCacheWriter) {
c.Status = cacheWriter.Status()
c.Data = cacheWriter.body.Bytes()
c.Header = cacheWriter.Header().Clone()
Expand All @@ -175,8 +172,10 @@ func (w *responseCacheWriter) WriteString(s string) (int, error) {
func replyWithCache(
c *gin.Context,
cfg *Config,
respCache *responseCache,
respCache *ResponseCache,
) {
cfg.beforeReplyWithCacheCallback(c, respCache)

c.Writer.WriteHeader(respCache.Status)

for key, values := range respCache.Header {
Expand Down
28 changes: 26 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ type Config struct {

hitCacheCallback OnHitCacheCallback

beforeReplyWithCacheCallback BeforeReplyWithCacheCallback

singleFlightForgetTimeout time.Duration
shareSingleFlightCallback OnShareSingleFlightCallback
}

func newConfig() *Config {
return &Config{
logger: Discard{},
hitCacheCallback: defaultHitCacheCallback,
beforeReplyWithCacheCallback: defaultBeforeReplyWithCacheCallback,
shareSingleFlightCallback: defaultShareSingleFlightCallback,
}
}

// Option represents the optional function.
type Option func(c *Config)

Expand Down Expand Up @@ -67,6 +78,19 @@ func WithOnHitCache(cb OnHitCacheCallback) Option {
}
}

type BeforeReplyWithCacheCallback func(c *gin.Context, cache *ResponseCache)

var defaultBeforeReplyWithCacheCallback = func(c *gin.Context, cache *ResponseCache) {}

// WithBeforeReplyWithCache will be called before replying with cache.
func WithBeforeReplyWithCache(cb BeforeReplyWithCacheCallback) Option {
return func(c *Config) {
if cb != nil {
c.beforeReplyWithCacheCallback = cb
}
}
}

// OnShareSingleFlightCallback define the callback when share the singleflight result
type OnShareSingleFlightCallback func(c *gin.Context)

Expand All @@ -81,8 +105,8 @@ func WithOnShareSingleFlight(cb OnShareSingleFlightCallback) Option {
}
}

// WithSingleFlightForgetTimeout to reduce the impact of long tail requests. when request in the singleflight,
// after the forget timeout, singleflight.Forget will be called
// WithSingleFlightForgetTimeout to reduce the impact of long tail requests.
// singleflight.Forget will be called after the timeout has reached for each backend request when timeout is greater than zero.
func WithSingleFlightForgetTimeout(forgetTimeout time.Duration) Option {
return func(c *Config) {
if forgetTimeout > 0 {
Expand Down

0 comments on commit eca3d49

Please sign in to comment.