8000 optimize: remove igonreQueryOrder option by li-jin-gou · Pull Request #6 · hertz-contrib/cache · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

optimize: remove igonreQueryOrder option #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,32 +190,51 @@ func newCache(

// NewCacheByRequestURI a shortcut function for caching response by uri
func NewCacheByRequestURI(defaultCacheStore persist.CacheStore, defaultExpire time.Duration, opts ...Option) app.HandlerFunc {
options := newOptions(opts...)
cacheStrategy := func(ctx context.Context, c *app.RequestContext) (bool, Strategy) {
return true, Strategy{
CacheKey: string(c.Request.RequestURI()),
}
}
var o []Option
o = append(o, WithCacheStrategyByRequest(cacheStrategy))
o = append(o, opts...)
return NewCache(defaultCacheStore, defaultExpire, o...)
}

var cacheStrategy GetCacheStrategyByRequest
if options.ignoreQueryOrder {
cacheStrategy = func(ctx context.Context, c *app.RequestContext) (bool, Strategy) {
newUri, err := getRequestUriIgnoreQueryOrder(c.Request.URI().String())
if err != nil {
hlog.CtxErrorf(ctx, getRequestUriIgnoreQueryOrderErrorFormat, err)
newUri = c.Request.URI().String()
}
// NewCacheByRequestURIWithIgnoreQueryOrder a shortcut function for caching response by uri and ignore query param order.
func NewCacheByRequestURIWithIgnoreQueryOrder(defaultCacheStore persist.CacheStore, defaultExpire time.Duration, opts ...Option) app.HandlerFunc {
cacheStrategy := func(ctx context.Context, c *app.RequestContext) (bool, Strategy) {
newUri, err := getRequestUriIgnoreQueryOrder(string(c.Request.RequestURI()))
if err != nil {
hlog.CtxErrorf(ctx, getRequestUriIgnoreQueryOrderErrorFormat, err)
newUri = string(c.Request.RequestURI())
}

return true, Strategy{
CacheKey: newUri,
}
return true, Strategy{
CacheKey: newUri,
}
} else {
cacheStrategy = func(ctx context.Context, c *app.RequestContext) (bool, Strategy) {
return true, Strategy{
CacheKey: c.Request.URI().String(),
}
}

var o []Option
o = append(o, WithCacheStrategyByRequest(cacheStrategy))
o = append(o, opts...)

return NewCache(defaultCacheStore, defaultExpire, o...)
}

// NewCacheByRequestPath a shortcut function for caching response by url path, means will discard the query params
func NewCacheByRequestPath(defaultCacheStore persist.CacheStore, defaultExpire time.Duration, opts ...Option) app.HandlerFunc {
cacheStrategy := func(ctx context.Context, c *app.RequestContext) (bool, Strategy) {
return true, Strategy{
CacheKey: b2s(c.Request.Path()),
}
}

options.getCacheStrategyByRequest = cacheStrategy
var o []Option
o = append(o, WithCacheStrategyByRequest(cacheStrategy))
o = append(o, opts...)

return newCache(defaultCacheStore, defaultExpire, options)
return NewCache(defaultCacheStore, defaultExpire, o...)
}

func getRequestUriIgnoreQueryOrder(requestURI string) (string, error) {
Expand Down Expand Up @@ -247,17 +266,6 @@ func getRequestUriIgnoreQueryOrder(requestURI string) (string, error) {
return parsedUrl.Path + "?" + strings.Join(queryVals, "&"), nil
}

// NewCacheByRequestPath a shortcut function for caching response by url path, means will discard the query params
func NewCacheByRequestPath(defaultCacheStore persist.CacheStore, defaultExpire time.Duration, opts ...Option) app.HandlerFunc {
opts = append(opts, WithCacheStrategyByRequest(func(ctx context.Context, c *app.RequestContext) (bool, Strategy) {
return true, Strategy{
CacheKey: b2s(c.Request.Path()),
}
}))

return NewCache(defaultCacheStore, defaultExpire, opts...)
}

func init() {
gob.Register(&ResponseCache{})
}
Expand Down
14 changes: 2 additions & 12 deletions options.go
10000
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ type Options struct {
singleFlightForgetTimeout time.Duration
shareSingleFlightCallback OnShareSingleFlightCallback

ignoreQueryOrder bool
prefixKey string
withoutHeader bool
prefixKey string
withoutHeader bool
}

// OnHitCacheCallback define the callback when use cache
Expand Down Expand Up @@ -162,15 +161,6 @@ func WithSingleFlightForgetTimeout(forgetTimeout time.Duration) Option {
}
}

// WithIgnoreQueryOrder will ignore the queries order in url when generate cache key . This option only takes effect in CacheByRequestURI function
func WithIgnoreQueryOrder(b bool) Option {
return Option{
F: func(o *Options) {
o.ignoreQueryOrder = b
},
}
}

// WithPrefixKey will prefix the key
func WithPrefixKey(prefix string) Option {
return Option{
Expand Down
10 changes: 3 additions & 7 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func TestOptions(t *testing.T) {
beforeReplyWithCacheCallback: defaultBeforeReplyWithCacheCallback,
shareSingleFlightCallback: defaultShareSingleFlightCallback,
singleFlightForgetTimeout: 1 * time.Second,
ignoreQueryOrder: false,
prefixKey: "prefix1",
withoutHeader: false,
}
Expand All @@ -81,7 +80,6 @@ func TestOptions(t *testing.T) {
assert.DeepEqual(t, "", z)
assert.DeepEqual(t, 1*time.Second, options.singleFlightForgetTimeout)
assert.DeepEqual(t, "prefix1", options.prefixKey)
assert.False(t, options.ignoreQueryOrder)
assert.False(t, options.withoutHeader)

opts := make([]Option, 0)
Expand All @@ -91,20 +89,19 @@ func TestOptions(t *testing.T) {
CacheKey: "test-key2",
}
}),
WithOnHitCache(func(c context.Context, ctx *app.RequestContext) {
WithOnHitCache(func(ctx context.Context, cc *app.RequestContext) {
w = "W"
}),
WithOnMissCache(func(c context.Context, ctx *app.RequestContext) {
WithOnMissCache(func(ctx context.Context, cc *app.RequestContext) {
x = "X"
}),
WithBeforeReplyWithCache(func(c *app.RequestContext, cache *ResponseCache) {
y = "Y"
}),
WithSingleFlightForgetTimeout(2*time.Second),
WithOnShareSingleFlight(func(ctx context.Context, c *app.RequestContext) {
WithOnShareSingleFlight(func(ctx context.Context, cc *app.RequestContext) {
z = "Z"
}),
WithIgnoreQueryOrder(true),
WithoutHeader(true),
WithPrefixKey("prefix2"),
)
Expand All @@ -124,6 +121,5 @@ func TestOptions(t *testing.T) {
assert.DeepEqual(t, "Z", z)
assert.DeepEqual(t, 2*time.Second, options.singleFlightForgetTimeout)
assert.DeepEqual(t, "prefix2", options.prefixKey)
assert.True(t, options.ignoreQueryOrder)
assert.True(t, options.withoutHeader)
}
0