You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/middleware/cache.md
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,12 +4,14 @@ id: cache
4
4
5
5
# Cache
6
6
7
-
Cache middleware for [Fiber](https://github.com/gofiber/fiber) designed to intercept responses and cache them. This middleware will cache the `Body`, `Content-Type` and `StatusCode` using the `c.Path()` as unique identifier. Special thanks to [@codemicro](https://github.com/codemicro/fiber-cache) for creating this middleware for Fiber core!
7
+
Cache middleware for [Fiber](https://github.com/gofiber/fiber) designed to intercept responses and cache them. This middleware caches the `Body`, `Content-Type`, and `StatusCode` using a key built from the request path and method. Special thanks to [@codemicro](https://github.com/codemicro/fiber-cache) for creating this middleware for Fiber core!
8
8
9
9
Request Directives<br />
10
10
`Cache-Control: no-cache` will return the up-to-date response but still caches it. You will always get a `miss` cache status.<br />
11
11
`Cache-Control: no-store` will refrain from caching. You will always get the up-to-date response.
12
12
13
+
If the server response contains a `Cache-Control: max-age` directive, its value is used as the expiration time for the cache entry.
14
+
13
15
Cacheable Status Codes<br />
14
16
15
17
This middleware caches responses with the following status codes according to RFC7231:
@@ -67,7 +69,7 @@ app.Use(cache.New(cache.Config{
67
69
}))
68
70
```
69
71
70
-
Or you can custom key and expire time like this:
72
+
Or you can customize the key and expiration time like this (the HTTP method is automatically appended to the key):
71
73
72
74
```go
73
75
app.Use(cache.New(cache.Config{
@@ -107,9 +109,9 @@ The `CacheInvalidator` function allows you to define custom conditions for cache
107
109
| CacheHeader |`string`| CacheHeader is the header on the response header that indicates the cache status, with the possible return values "hit," "miss," or "unreachable." |`X-Cache`|
108
110
| CacheControl |`bool`| CacheControl enables client-side caching if set to true. |`false`|
109
111
| CacheInvalidator |`func(fiber.Ctx) bool`| CacheInvalidator defines a function that is executed before checking the cache entry. It can be used to invalidate the existing cache manually by returning true. |`nil`|
110
-
| KeyGenerator |`func(fiber.Ctx) string`| Key allows you to generate custom keys. |`func(c fiber.Ctx) string { return utils.CopyString(c.Path()) }`|
112
+
| KeyGenerator |`func(fiber.Ctx) string`| Key allows you to generate custom keys. The HTTP method is appended automatically. |`func(c fiber.Ctx) string { return utils.CopyString(c.Path()) }`|
111
113
| ExpirationGenerator |`func(fiber.Ctx, *cache.Config) time.Duration`| ExpirationGenerator allows you to generate custom expiration keys based on the request. |`nil`|
112
-
| Storage |`fiber.Storage`|Store is used to store the state of the middleware. | In-memory store |
114
+
| Storage |`fiber.Storage`|Storage is used to store the state of the middleware. | In-memory store |
113
115
| StoreResponseHeaders |`bool`| StoreResponseHeaders allows you to store additional headers generated by next middlewares & handler. |`false`|
114
116
| MaxBytes |`uint`| MaxBytes is the maximum number of bytes of response bodies simultaneously stored in cache. |`0` (No limit) |
115
117
| Methods |`[]string`| Methods specifies the HTTP methods to cache. |`[]string{fiber.MethodGet, fiber.MethodHead}`|
0 commit comments