Skip to content

Commit

Permalink
feat: add OnShareSingleFlightCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyahui committed Oct 9, 2021
1 parent 4ecbc53 commit 4d8a8f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
6 changes: 4 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ func Cache(
opts ...Option,
) gin.HandlerFunc {
cfg := &Config{
logger: Discard{},
hitCacheCallback: defaultHitCacheCallback,
logger: Discard{},
hitCacheCallback: defaultHitCacheCallback,
shareSingleFlightCallback: defaultShareSingleFlightCallback,
}

for _, opt := range opts {
Expand Down Expand Up @@ -113,6 +114,7 @@ func Cache(

if !inFlight {
replyWithCache(c, cfg, rawRespCache.(*responseCache))
cfg.shareSingleFlightCallback(c)
}
}
}
Expand Down
21 changes: 20 additions & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Config struct {
hitCacheCallback OnHitCacheCallback

singleFlightForgetTimeout time.Duration
shareSingleFlightCallback OnShareSingleFlightCallback
}

// Option represents the optional function.
Expand All @@ -29,6 +30,7 @@ func WithLogger(l Logger) Option {
}
}

// Logger define the logger interface
type Logger interface {
Errorf(string, ...interface{})
}
Expand All @@ -37,10 +39,12 @@ type Logger interface {
type Discard struct {
}

// Errorf will output the log at error level
func (l Discard) Errorf(string, ...interface{}) {

}

// WithCacheStrategyByRequest set up the custom strategy by per request
func WithCacheStrategyByRequest(getGetCacheStrategyByRequest GetCacheStrategyByRequest) Option {
return func(c *Config) {
if getGetCacheStrategyByRequest != nil {
Expand All @@ -49,8 +53,11 @@ func WithCacheStrategyByRequest(getGetCacheStrategyByRequest GetCacheStrategyByR
}
}

// OnHitCacheCallback define the callback when use cache
type OnHitCacheCallback func(c *gin.Context)

var defaultHitCacheCallback = func(c *gin.Context) {}

// WithOnHitCache will be called when cache hit.
func WithOnHitCache(cb OnHitCacheCallback) Option {
return func(c *Config) {
Expand All @@ -60,7 +67,19 @@ func WithOnHitCache(cb OnHitCacheCallback) Option {
}
}

var defaultHitCacheCallback = func(c *gin.Context) {}
// OnShareSingleFlightCallback define the callback when share the singleflight result
type OnShareSingleFlightCallback func(c *gin.Context)

var defaultShareSingleFlightCallback = func(c *gin.Context) {}

// WithOnShareSingleFlight will be called when share the singleflight result
func WithOnShareSingleFlight(cb OnShareSingleFlightCallback) Option {
return func(c *Config) {
if cb != nil {
c.shareSingleFlightCallback = cb
}
}
}

// WithSingleFlightForgetTimeout to reduce the impact of long tail requests. when request in the singleflight,
// after the forget timeout, singleflight.Forget will be called
Expand Down

0 comments on commit 4d8a8f9

Please sign in to comment.