Skip to content

Commit

Permalink
Update en cache.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought authored Oct 23, 2023
1 parent 569495e commit c7fa6f7
Showing 1 changed file with 35 additions and 50 deletions.
85 changes: 35 additions & 50 deletions content/en/docs/hertz/tutorials/basic-feature/middleware/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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`.
Expand Down

0 comments on commit c7fa6f7

Please sign in to comment.