diff --git a/content/en/docs/hertz/tutorials/basic-feature/middleware/cache.md b/content/en/docs/hertz/tutorials/basic-feature/middleware/cache.md index 8a850693a7..c1dacf6fcc 100644 --- a/content/en/docs/hertz/tutorials/basic-feature/middleware/cache.md +++ b/content/en/docs/hertz/tutorials/basic-feature/middleware/cache.md @@ -155,6 +155,41 @@ func main() { } ``` +### NewCacheByRequestURIWithIgnoreQueryOrder + +Create caching middleware that uses URIs as keys and ignores the order of query parameters + +Function Signature: + +```go +func NewCacheByRequestURIWithIgnoreQueryOrder(defaultCacheStore persist.CacheStore, defaultExpire time.Duration, opts ...Option) app.HandlerFunc +``` + +Sample Code: + +```go +func main() { + h := server.New() + + memoryStore := persist.NewMemoryStore(1 * time.Minute) + + h.Use(cache.NewCacheByRequestURIWithIgnoreQueryOrder( + memoryStore, + 2*time.Second, + cache.WithCacheStrategyByRequest(func(ctx context.Context, c *app.RequestContext) (bool, cache.Strategy) { + return true, cache.Strategy{ + CacheKey: c.Request.URI().String(), + } + }), + )) + h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { + c.String(http.StatusOK, "hello world") + }) + + h.Spin() +} +``` + ## Configuration ### Generic Configuration @@ -168,21 +203,8 @@ func main() { | WithSingleFlightForgetTimeout | 0 | Used to set the timeout for SingleFlight | | WithPrefixKey | "" | Used to set the prefix of the cache response key | | WithoutHeader | false | Used to set whether response headers need to be cached | - -### Additional Configuration for Each Mode - -#### NewCache Mode - -| Configuration | Default | Description | -| -------------------------- | ------- | ----------------------------------- | | WithCacheStrategyByRequest | nil | Used to set custom caching policies | -#### NewCacheByRequestURI Mode - -| Configuration | Default | Description | -| -------------------- | ------- | ------------------------------------------------------------ | -| WithIgnoreQueryOrder | false | Used to set the order in which query parameters are ignored when using a URI as the cached Key | - ### WithCacheStrategyByRequest Customize the cache policy by using `WithCacheStrategyByRequest`, including the cache key, storage medium, and expiration time. @@ -340,43 +362,6 @@ func main() { } ``` -### WithIgnoreQueryOrder - -Set the query parameter order of the URI to be ignored when creating a cache middleware using the `NewCacheByRequestURI` method by using `WithIgnoreQueryOrder`. - -Function Signature: - -```go -func WithIgnoreQueryOrder(b bool) Option -``` - -Sample Code: - -```go -func main() { - h := server.New() - - memoryStore := persist.NewMemoryStore(1 * time.Minute) - - h.Use(cache.NewCacheByRequestPath( - memoryStore, - 60*time.Second, - cache.WithIgnoreQueryOrder(true), - cache.WithOnHitCache(func(c context.Context, ctx *app.RequestContext) { - hlog.Infof("hit cache IgnoreQueryOrder") - }), - cache.WithOnMissCache(func(c context.Context, ctx *app.RequestContext) { - hlog.Infof("miss cache IgnoreQueryOrder") - }), - )) - h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { - c.String(http.StatusOK, "hello world") - }) - - h.Spin() -} -``` - ### WithPrefixKey Set the prefix of the response key by using `WithPrefixKey`.