Skip to content

Commit

Permalink
docs: optimize cache doc configuration show (#698)
Browse files Browse the repository at this point in the history
Co-authored-by: Jiun Lee <[email protected]>
  • Loading branch information
Hanser001 and Skyenought authored Oct 26, 2023
1 parent 5f85111 commit fb880b0
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 88 deletions.
79 changes: 39 additions & 40 deletions content/en/docs/hertz/tutorials/basic-feature/middleware/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,55 @@ 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

| Configuration | Default | Description |
| ----------------------------- | ------- | ------------------------------------------------------------ |
| WithCacheStrategyByRequest | nil | Used to set custom caching policies |
| :---------------------------- | ------- | ------------------------------------------------------------ |
| WithOnHitCache | nil | Used to set the callback function after a cache hits |
| WithOnMissCache | nil | Used to set the callback function for cache misses |
| WithBeforeReplyWithCache | nil | Used to set the callback function before returning the cached response |
| WithOnShareSingleFlight | nil | Used to set the callback function when the result of a SingleFlight is shared by the request |
| WithSingleFlightForgetTimeout | 0 | Used to set the timeout for SingleFlight |
| WithIgnoreQueryOrder | false | Used to set the order in which query parameters are ignored when using a URI as the cached Key |
| WithPrefixKey | "" | Used to set the prefix of the cache response key |
| WithoutHeader | false | Used to set whether response headers need to be cached |
| WithCacheStrategyByRequest | nil | Used to set custom caching policies |

### WithCacheStrategyByRequest

Expand Down Expand Up @@ -326,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
92 changes: 44 additions & 48 deletions content/zh/docs/hertz/tutorials/basic-feature/middleware/cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,52 @@ func main() {
}
```

### NewCacheByRequestURIWithIgnoreQueryOrder

创建使用 URI 作为 key 并忽略查询参数顺序的缓存中间件

函数签名
```go
func NewCacheByRequestURIWithIgnoreQueryOrder(defaultCacheStore persist.CacheStore, defaultExpire time.Duration, opts ...Option) app.HandlerFunc
```

示例代码:

```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()
}
```

## 配置

| 配置 | 默认值 | 介绍 |
| ----------------------------- | ------ | --------------------------------------------------------- |
| WithCacheStrategyByRequest | nil | 用于设置自定义的缓存策略 |
| WithOnHitCache | nil | 用于设置缓存命中的回调函数 |
| WithOnMissCache | nil | 用于设置缓存未命中的回调函数 |
| WithBeforeReplyWithCache | nil | 用于设置返回缓存响应前的回调函数 |
| WithOnShareSingleFlight | nil | 用于设置请求共享 SingleFlight 结果时的回调函数 |
| WithSingleFlightForgetTimeout | 0 | 用于设置 SingleFlight 的超时时间 |
| WithIgnoreQueryOrder | false | 用于设置当使用 URI 为缓存的 Key 时,忽略 query 参数的顺序 |
| WithPrefixKey | "" | 用于设置缓存响应 Key 的前缀 |
| WithoutHeader | false | 用于设置是否需要缓存响应头 |
| 配置 | 默认值 | 介绍 |
| ----------------------------- | ------ | ---------------------------------------------- |
| WithOnHitCache | nil | 用于设置缓存命中的回调函数 |
| WithOnMissCache | nil | 用于设置缓存未命中的回调函数 |
| WithBeforeReplyWithCache | nil | 用于设置返回缓存响应前的回调函数 |
| WithOnShareSingleFlight | nil | 用于设置请求共享 SingleFlight 结果时的回调函数 |
| WithSingleFlightForgetTimeout | 0 | 设置 SingleFlight 的超时时间,以控制并发操作的行为,确保请求在一定时间内被处理或者超时被取消 |
| WithPrefixKey | "" | 用于设置缓存响应 Key 的前缀 |
| WithoutHeader | false | 用于设置是否需要缓存响应头 |
| WithCacheStrategyByRequest | nil | 用于设置自定义的缓存策略 |

### WithCacheStrategyByRequest

Expand Down Expand Up @@ -326,43 +359,6 @@ func main() {
}
```

### WithIgnoreQueryOrder

通过使用 `WithIgnoreQueryOrder` 设置当使用 `NewCacheByRequestURI` 方法创建缓存中间件时,忽略 URI 的 query 参数顺序(为 true 触发参数排序)。

函数签名:

```go
func WithIgnoreQueryOrder(b bool) Option
```

示例代码:

```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

通过使用 `WithPrefixKey` 设置响应 Key 的前缀。
Expand Down

1 comment on commit fb880b0

@vercel
Copy link

@vercel vercel bot commented on fb880b0 Oct 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.