From 3f72f2deb31e04d4dbeaafff41903e71ea0bb197 Mon Sep 17 00:00:00 2001 From: kinggo Date: Wed, 17 May 2023 14:13:22 +0800 Subject: [PATCH] optimize: remove ignoreQueryOrder option --- cache.go | 68 +++++++++++++++++++++++++++---------------------- options.go | 14 ++-------- options_test.go | 10 +++----- 3 files changed, 43 insertions(+), 49 deletions(-) diff --git a/cache.go b/cache.go index 7fa9192..d3830a3 100644 --- a/cache.go +++ b/cache.go @@ -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) { @@ -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{}) } diff --git a/options.go b/options.go index 8274c98..3f6ef53 100644 --- a/options.go +++ b/options.go @@ -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 @@ -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{ diff --git a/options_test.go b/options_test.go index 110f508..443b0fa 100644 --- a/options_test.go +++ b/options_test.go @@ -61,7 +61,6 @@ func TestOptions(t *testing.T) { beforeReplyWithCacheCallback: defaultBeforeReplyWithCacheCallback, shareSingleFlightCallback: defaultShareSingleFlightCallback, singleFlightForgetTimeout: 1 * time.Second, - ignoreQueryOrder: false, prefixKey: "prefix1", withoutHeader: false, } @@ -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) @@ -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"), ) @@ -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) }