Skip to content

Commit cbf840c

Browse files
leorossigithub-actions[bot]
authored andcommitted
[automated commit] Bump docs to versions 2.18.0, 1.53.4
1 parent 38db0a7 commit cbf840c

File tree

4 files changed

+259
-1
lines changed

4 files changed

+259
-1
lines changed

docs/guides/http-cache.md

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
# Configure HTTP Caching for Platformatic App
2+
3+
Platformatic Runtime supports HTTP requests caching out of the box. This feature is useful to reduce the number of requests to the same resource and improve the performance of your application.
4+
5+
## Enable HTTP Caching
6+
7+
Let's create a simple Platformatic app to demonstrate how to enable HTTP caching.
8+
9+
```bash
10+
npx create-platformatic@latest
11+
```
12+
13+
Here are the answers to the questions to create a Platformatic app with one Platformatic Composer service named `main` and one Platformatic Service service named `internal`.
14+
15+
```bash
16+
? Where would you like to create your project? .
17+
? Which kind of project do you want to create? @platformatic/composer
18+
? What is the name of the service? main
19+
? Do you want to create another service? yes
20+
? Which kind of project do you want to create? @platformatic/service
21+
? What is the name of the service? internal
22+
? Do you want to create another service? no
23+
? Which service should be exposed? main
24+
? Do you want to use TypeScript? no
25+
? What port do you want to use? 3042
26+
? Do you want to init the git repository? no
27+
```
28+
29+
To enable HTTP caching, let's set the `httpCache` property to `true` in the root `platformatic.json` file.
30+
31+
```json
32+
{
33+
"$schema": "https://schemas.platformatic.dev/@platformatic/runtime/2.9.0.json",
34+
"entrypoint": "main",
35+
"watch": true,
36+
"autoload": {
37+
"path": "services",
38+
"exclude": [
39+
"docs"
40+
]
41+
},
42+
"logger": {
43+
"level": "{PLT_SERVER_LOGGER_LEVEL}"
44+
},
45+
"server": {
46+
"hostname": "{PLT_SERVER_HOSTNAME}",
47+
"port": "{PORT}"
48+
},
49+
"managementApi": "{PLT_MANAGEMENT_API}",
50+
"httpCache": true
51+
}
52+
```
53+
54+
Now when the httpCache is enabled, let's add a cache control header to the response of the API. You can do this by adding a `cacheControl` property to the response object.
55+
56+
Let's add a cached route to the `./services/internal/routes/root.js` file.
57+
58+
```js
59+
/// <reference path="../global.d.ts" />
60+
'use strict'
61+
/** @param {import('fastify').FastifyInstance} fastify */
62+
module.exports = async function (fastify, opts) {
63+
fastify.get('/example', async (request, reply) => {
64+
return { hello: fastify.example }
65+
})
66+
67+
let counter = 0
68+
fastify.get('/cached-counter', async (request, reply) => {
69+
reply.header('Cache-Control', 'public, max-age=10')
70+
return { counter: counter++ }
71+
})
72+
}
73+
```
74+
75+
Now, let's start the Platformatic app.
76+
77+
```bash
78+
npm run start
79+
```
80+
81+
Let's test the `/cached-counter` route using `curl`.
82+
83+
```bash
84+
curl http://localhost:3042/internal/cached-counter
85+
{"counter":0}
86+
```
87+
88+
```bash
89+
curl http://localhost:3042/internal/cached-counter
90+
{"counter":0}
91+
```
92+
93+
...and after 10 seconds...
94+
95+
```bash
96+
curl http://localhost:3042/internal/cached-counter
97+
{"counter":1}
98+
```
99+
100+
## Http Cache invalidation
101+
102+
By default the cache is invalidated when the `max-age` is reached. However, you can invalidate the cache manually by calling the `globalThis.platformatic.invalidateHttpCache` method.
103+
104+
Let's add another route to the `./services/internal/routes/root.js` file that invalidates the cache for the `/cached-counter` route. And also increase the `max-age` to 180 seconds to be sure that the cache is invalidated and not just expired.
105+
106+
```js
107+
/// <reference path="../global.d.ts" />
108+
'use strict'
109+
/** @param {import('fastify').FastifyInstance} fastify */
110+
module.exports = async function (fastify, opts) {
111+
fastify.get('/example', async (request, reply) => {
112+
return { hello: fastify.example }
113+
})
114+
115+
let counter = 0
116+
fastify.get('/cached-counter', async (request, reply) => {
117+
reply.header('Cache-Control', 'public, max-age=180')
118+
return { counter: counter++ }
119+
})
120+
121+
fastify.delete('/invalidate-cached-counter', async () => {
122+
await globalThis.platformatic.invalidateHttpCache({
123+
keys: [
124+
{
125+
origin: 'http://internal.plt.local',
126+
path: '/cached-counter',
127+
method: 'GET'
128+
}
129+
]
130+
})
131+
})
132+
}
133+
```
134+
135+
Let's start the Platformatic app.
136+
137+
```bash
138+
npm run start
139+
```
140+
141+
Let's test the `/cached-counter` route using `curl`.
142+
143+
```bash
144+
curl http://localhost:3042/internal/cached-counter
145+
{"counter":0}
146+
```
147+
148+
Now, let's invalidate the cache for the `/cached-counter` route.
149+
150+
```bash
151+
curl -X DELETE http://localhost:3042/internal/invalidate-cached-counter
152+
```
153+
154+
And test the `/cached-counter` route again.
155+
156+
```bash
157+
curl http://localhost:3042/internal/cached-counter
158+
{"counter":1}
159+
```
160+
161+
## Invalidating cache by cache tags
162+
163+
Platformatic Runtime supports cache tags to invalidate related cache entries. Cache tags
164+
should be set in one of the response headers. Cache tags should be globally unique.
165+
166+
Let's set the `X-Cache-Tags` header in the root `platformatic.json` file.
167+
168+
```json
169+
{
170+
"$schema": "https://schemas.platformatic.dev/@platformatic/runtime/2.9.0.json",
171+
"entrypoint": "main",
172+
"watch": true,
173+
"autoload": {
174+
"path": "services",
175+
"exclude": [
176+
"docs"
177+
]
178+
},
179+
"logger": {
180+
"level": "{PLT_SERVER_LOGGER_LEVEL}"
181+
},
182+
"server": {
183+
"hostname": "{PLT_SERVER_HOSTNAME}",
184+
"port": "{PORT}"
185+
},
186+
"managementApi": "{PLT_MANAGEMENT_API}",
187+
"httpCache": {
188+
"cacheTagsHeader": "X-Cache-Tags"
189+
}
190+
}
191+
```
192+
193+
Now, let's add a cache tag to the response of the API. And also modify the `/invalidate-cached-counter` route to invalidate the cache by the cache tag.
194+
195+
```js
196+
/// <reference path="../global.d.ts" />
197+
'use strict'
198+
/** @param {import('fastify').FastifyInstance} fastify */
199+
module.exports = async function (fastify, opts) {
200+
fastify.get('/example', async (request, reply) => {
201+
return { hello: fastify.example }
202+
})
203+
204+
let counter = 0
205+
fastify.get('/cached-counter', async (request, reply) => {
206+
reply.header('Cache-Control', 'public, s-maxage=180')
207+
reply.header('X-Cache-Tags', 'cached-counter-tag')
208+
return { counter: counter++ }
209+
})
210+
211+
fastify.post('/invalidate-cached-counter', async () => {
212+
await globalThis.platformatic.invalidateHttpCache({
213+
tags: ['cached-counter-tag']
214+
})
215+
})
216+
}
217+
```
218+
219+
Let's start the Platformatic app.
220+
221+
```bash
222+
npm run start
223+
```
224+
225+
Let's test the `/cached-counter` route using `curl`.
226+
227+
```bash
228+
curl http://localhost:3042/internal/cached-counter
229+
{"counter":0}
230+
```
231+
232+
```bash
233+
curl http://localhost:3042/internal/cached-counter
234+
{"counter":0}
235+
```
236+
237+
Now, let's invalidate the cache for the `/cached-counter` route by the cache tag.
238+
239+
```bash
240+
curl -X POST http://localhost:3042/internal/invalidate-cached-counter
241+
```
242+
243+
And test the `/cached-counter` route again.
244+
245+
```bash
246+
curl http://localhost:3042/internal/cached-counter
247+
{"counter":1}
248+
```

docs/runtime/_shared-configuration.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ OTLP traces can be consumed by different solutions, like [Jaeger](https://www.ja
171171
}
172172
```
173173

174+
## httpCache
175+
176+
The `httpCache` configuration is used to enable the HTTP cache for the Platformatic Runtime.
177+
It can be a boolean or an object with the following settings:
178+
179+
- **`store`** (`string`) - The store to use for the cache. Set an npm package name to use a custom store or path to a file to use a custom store from a file. By default, the `memory` store is used.
180+
- **`methods`** (`array`) - The HTTP methods to cache. By default, GET and HEAD methods are cached.
181+
- **`cacheTagsHeader`** (`string`) - The header to use for cache tags.
182+
174183
### `server`
175184

176185
This configures the Platformatic Runtime entrypoint `server`.

docs/runtime/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ The Platformatic CLI automatically detects and loads configuration files found i
1111

1212
Alternatively, you can use the `--config` option to specify a configuration file path for most `platformatic runtime` CLI commands. The configuration examples in this reference use the JSON format.
1313

14-
<SharedConfiguration/>
14+
<SharedConfiguration/>

sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@
232232
},
233233
"guides/securing-platformatic-db",
234234
"guides/jwt-auth0",
235+
"guides/http-cache",
235236
"guides/monitoring",
236237
"guides/debug-platformatic-db",
237238
"guides/environment-variables",

0 commit comments

Comments
 (0)