|
1
|
import { Prerequisites, Table } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Medusa Cache`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this guide, you'll learn about Medusa Cache. |
|
10
|
|
|
11
|
## What is Medusa Cache? |
|
12
|
|
|
13
|
Medusa Cache is a caching layer on the Medusa backend that improves your application's performance and reduces costs. It's available to all environments and plans out of the box at no additional cost. |
|
14
|
|
|
15
|
Medusa Cache relies on the existing key/value [Redis](../redis/page.mdx) store already provisioned for environments. Each environment has its own isolated cache. |
|
16
|
|
|
17
|
 |
|
18
|
|
|
19
|
--- |
|
20
|
|
|
21
|
## Enable Medusa Cache |
|
22
|
|
|
23
|
<Prerequisites |
|
24
|
items={[ |
|
25
|
{ |
|
26
|
text: "Medusa v2.11.0+", |
|
27
|
href: "https://github.com/medusajs/medusa/releases/tag/v2.11.0", |
|
28
|
} |
|
29
|
]} |
|
30
|
/> |
|
31
|
|
|
32
|
To enable Medusa Cache for your Medusa project deployed on Cloud, set the `caching` feature flag in `medusa-config.ts`: |
|
33
|
|
|
34
|
```ts title="medusa-config.ts" |
|
35
|
module.exports = defineConfig({ |
|
36
|
// ... |
|
37
|
featureFlags: { |
|
38
|
caching: true, |
|
39
|
}, |
|
40
|
}) |
|
41
|
``` |
|
42
|
|
|
43
|
This will enable all Medusa Cache functionality in your project. |
|
44
|
|
|
45
|
--- |
|
46
|
|
|
47
|
## How Does Medusa Cache Work? |
|
48
|
|
|
49
|
### Cached Data in API Routes |
|
50
|
|
|
51
|
By using Medusa Cache, data is cached across several business-critical APIs to boost performance and throughput. This includes all cart-related operations, where the following data is cached: |
|
52
|
|
|
53
|
- Regions |
|
54
|
- Promotion codes |
|
55
|
- Variant price sets |
|
56
|
- Variants |
|
57
|
- Shipping options |
|
58
|
- Sales channels |
|
59
|
- Customers |
|
60
|
|
|
61
|
### Integrated into Query |
|
62
|
|
|
63
|
Medusa Cache is built on the new [Caching Module](!resources!/infrastructure-modules/caching). The module has been integrated into [Query](!docs!/learn/fundamentals/query) so that `query.graph` calls can be cached easily. |
|
64
|
|
|
65
|
You can enable caching for your custom `query.graph` calls by passing a `cache` option. For example: |
|
66
|
|
|
67
|
```ts |
|
68
|
const { data: products } = await query.graph({ |
|
69
|
entity: "product", |
|
70
|
fields: ["id", "title", "handle"], |
|
71
|
}, { |
|
72
|
cache: { |
|
73
|
enable: true, |
|
74
|
}, |
|
75
|
}) |
|
76
|
``` |
|
77
|
|
|
78
|
This code caches the query result. When the same query is made again, the cached result is returned instead of querying the database. |
|
79
|
|
|
80
|
 |
|
81
|
|
|
82
|
### Automatic Invalidation |
|
83
|
|
|
84
|
By default, data is cached for one hour or until invalidated. Invalidation occurs automatically when related data is created, updated, or deleted. |
|
85
|
|
|
86
|
Learn more about automatic invalidation in the [Caching Module documentation](!resources!/infrastructure-modules/caching/concepts#automatic-cache-invalidation). |
|
87
|
|
|
88
|
--- |
|
89
|
|
|
90
|
## Performance Benchmark Comparisons |
|
91
|
|
|
92
|
The following benchmark comparisons demonstrate the performance improvements you can expect with Medusa Cache. These were conducted on a Cloud environment with a standard setup. |
|
93
|
|
|
94
|
<Table> |
|
95
|
<Table.Header> |
|
96
|
<Table.Row> |
|
97
|
<Table.HeaderCell className="w-1/2"> |
|
98
|
Endpoint |
|
99
|
</Table.HeaderCell> |
|
100
|
<Table.HeaderCell> |
|
101
|
Without Medusa Cache (ms) |
|
102
|
</Table.HeaderCell> |
|
103
|
<Table.HeaderCell> |
|
104
|
With Medusa Cache (ms) |
|
105
|
</Table.HeaderCell> |
|
106
|
<Table.HeaderCell> |
|
107
|
Improvement |
|
108
|
</Table.HeaderCell> |
|
109
|
</Table.Row> |
|
110
|
</Table.Header> |
|
111
|
<Table.Body> |
|
112
|
<Table.Row> |
|
113
|
<Table.Cell colSpan={4} className="bg-medusa-bg-subtle"> |
|
114
|
**Product Operations** |
|
115
|
</Table.Cell> |
|
116
|
</Table.Row> |
|
117
|
<Table.Row> |
|
118
|
<Table.Cell> |
|
119
|
`GET /store/products` |
|
120
|
</Table.Cell> |
|
121
|
<Table.Cell> |
|
122
|
`354.00` |
|
123
|
</Table.Cell> |
|
124
|
<Table.Cell> |
|
125
|
`83.00` |
|
126
|
</Table.Cell> |
|
127
|
<Table.Cell> |
|
128
|
`~77%` |
|
129
|
</Table.Cell> |
|
130
|
</Table.Row> |
|
131
|
<Table.Row> |
|
132
|
<Table.Cell> |
|
133
|
`GET /store/product-categories` |
|
134
|
</Table.Cell> |
|
135
|
<Table.Cell> |
|
136
|
`88.00` |
|
137
|
</Table.Cell> |
|
138
|
<Table.Cell> |
|
139
|
`51.00` |
|
140
|
</Table.Cell> |
|
141
|
<Table.Cell> |
|
142
|
`~42%` |
|
143
|
</Table.Cell> |
|
144
|
</Table.Row> |
|
145
|
<Table.Row> |
|
146
|
<Table.Cell> |
|
147
|
`GET /store/products/:id` |
|
148
|
</Table.Cell> |
|
149
|
<Table.Cell> |
|
150
|
`161.00` |
|
151
|
</Table.Cell> |
|
152
|
<Table.Cell> |
|
153
|
`123.00` |
|
154
|
</Table.Cell> |
|
155
|
<Table.Cell> |
|
156
|
`~24%` |
|
157
|
</Table.Cell> |
|
158
|
</Table.Row> |
|
159
|
<Table.Row> |
|
160
|
<Table.Cell colSpan={4} className="bg-medusa-bg-subtle"> |
|
161
|
**Cart Operations** |
|
162
|
</Table.Cell> |
|
163
|
</Table.Row> |
|
164
|
<Table.Row> |
|
165
|
<Table.Cell> |
|
166
|
`POST /store/carts/:id/line-items` |
|
167
|
</Table.Cell> |
|
168
|
<Table.Cell> |
|
169
|
`697.00` |
|
170
|
</Table.Cell> |
|
171
|
<Table.Cell> |
|
172
|
`427.00` |
|
173
|
</Table.Cell> |
|
174
|
<Table.Cell> |
|
175
|
`~39%` |
|
176
|
</Table.Cell> |
|
177
|
</Table.Row> |
|
178
|
<Table.Row> |
|
179
|
<Table.Cell> |
|
180
|
`POST /store/carts/:id/shipping-methods` |
|
181
|
</Table.Cell> |
|
182
|
<Table.Cell> |
|
183
|
`1002.00` |
|
184
|
</Table.Cell> |
|
185
|
<Table.Cell> |
|
186
|
`670.00` |
|
187
|
</Table.Cell> |
|
188
|
<Table.Cell> |
|
189
|
`~33%` |
|
190
|
</Table.Cell> |
|
191
|
</Table.Row> |
|
192
|
<Table.Row> |
|
193
|
<Table.Cell> |
|
194
|
`POST /store/carts/:id/promotions` |
|
195
|
</Table.Cell> |
|
196
|
<Table.Cell> |
|
197
|
`302.00` |
|
198
|
</Table.Cell> |
|
199
|
<Table.Cell> |
|
200
|
`203.00` |
|
201
|
</Table.Cell> |
|
202
|
<Table.Cell> |
|
203
|
`~33%` |
|
204
|
</Table.Cell> |
|
205
|
</Table.Row> |
|
206
|
<Table.Row> |
|
207
|
<Table.Cell> |
|
208
|
`GET /store/carts/:id` |
|
209
|
</Table.Cell> |
|
210
|
<Table.Cell> |
|
211
|
`109.00` |
|
212
|
</Table.Cell> |
|
213
|
<Table.Cell> |
|
214
|
`73.00` |
|
215
|
</Table.Cell> |
|
216
|
<Table.Cell> |
|
217
|
`~33%` |
|
218
|
</Table.Cell> |
|
219
|
</Table.Row> |
|
220
|
<Table.Row> |
|
221
|
<Table.Cell> |
|
222
|
`POST /store/carts/:id` |
|
223
|
</Table.Cell> |
|
224
|
<Table.Cell> |
|
225
|
`909.00` |
|
226
|
</Table.Cell> |
|
227
|
<Table.Cell> |
|
228
|
`656.00` |
|
229
|
</Table.Cell> |
|
230
|
<Table.Cell> |
|
231
|
`~28%` |
|
232
|
</Table.Cell> |
|
233
|
</Table.Row> |
|
234
|
<Table.Row> |
|
235
|
<Table.Cell> |
|
236
|
`POST /store/carts` |
|
237
|
</Table.Cell> |
|
238
|
<Table.Cell> |
|
239
|
`446.00` |
|
240
|
</Table.Cell> |
|
241
|
<Table.Cell> |
|
242
|
`329.00` |
|
243
|
</Table.Cell> |
|
244
|
<Table.Cell> |
|
245
|
`~26%` |
|
246
|
</Table.Cell> |
|
247
|
</Table.Row> |
|
248
|
<Table.Row> |
|
249
|
<Table.Cell> |
|
250
|
`POST /store/carts/:id/complete` |
|
251
|
</Table.Cell> |
|
252
|
<Table.Cell> |
|
253
|
`1357.00` |
|
254
|
</Table.Cell> |
|
255
|
<Table.Cell> |
|
256
|
`1018.00` |
|
257
|
</Table.Cell> |
|
258
|
<Table.Cell> |
|
259
|
`~25%` |
|
260
|
</Table.Cell> |
|
261
|
</Table.Row> |
|
262
|
<Table.Row> |
|
263
|
<Table.Cell colSpan={4} className="bg-medusa-bg-subtle"> |
|
264
|
**Order Operations** |
|
265
|
</Table.Cell> |
|
266
|
</Table.Row> |
|
267
|
<Table.Row> |
|
268
|
<Table.Cell> |
|
269
|
`GET /store/orders/:id` |
|
270
|
</Table.Cell> |
|
271
|
<Table.Cell> |
|
272
|
`119.00` |
|
273
|
</Table.Cell> |
|
274
|
<Table.Cell> |
|
275
|
`81.00` |
|
276
|
</Table.Cell> |
|
277
|
<Table.Cell> |
|
278
|
`~32%` |
|
279
|
</Table.Cell> |
|
280
|
</Table.Row> |
|
281
|
<Table.Row> |
|
282
|
<Table.Cell colSpan={4} className="bg-medusa-bg-subtle"> |
|
283
|
**Other Operations** |
|
284
|
</Table.Cell> |
|
285
|
</Table.Row> |
|
286
|
<Table.Row> |
|
287
|
<Table.Cell> |
|
288
|
`GET /store/regions` |
|
289
|
</Table.Cell> |
|
290
|
<Table.Cell> |
|
291
|
`89.00` |
|
292
|
</Table.Cell> |
|
293
|
<Table.Cell> |
|
294
|
`51.00` |
|
295
|
</Table.Cell> |
|
296
|
<Table.Cell> |
|
297
|
`~43%` |
|
298
|
</Table.Cell> |
|
299
|
</Table.Row> |
|
300
|
<Table.Row> |
|
301
|
<Table.Cell> |
|
302
|
`GET /store/shipping-options` |
|
303
|
</Table.Cell> |
|
304
|
<Table.Cell> |
|
305
|
`272.00` |
|
306
|
</Table.Cell> |
|
307
|
<Table.Cell> |
|
308
|
`173.00` |
|
309
|
</Table.Cell> |
|
310
|
<Table.Cell> |
|
311
|
`~36%` |
|
312
|
</Table.Cell> |
|
313
|
</Table.Row> |
|
314
|
<Table.Row> |
|
315
|
<Table.Cell> |
|
316
|
`POST /store/payment-collections` |
|
317
|
</Table.Cell> |
|
318
|
<Table.Cell> |
|
319
|
`238.00` |
|
320
|
</Table.Cell> |
|
321
|
<Table.Cell> |
|
322
|
`160.00` |
|
323
|
</Table.Cell> |
|
324
|
<Table.Cell> |
|
325
|
`~33%` |
|
326
|
</Table.Cell> |
|
327
|
</Table.Row> |
|
328
|
<Table.Row> |
|
329
|
<Table.Cell> |
|
330
|
`POST /store/payment-collections/:id/payments` |
|
331
|
</Table.Cell> |
|
332
|
<Table.Cell> |
|
333
|
`152.00` |
|
334
|
</Table.Cell> |
|
335
|
<Table.Cell> |
|
336
|
`102.00` |
|
337
|
</Table.Cell> |
|
338
|
<Table.Cell> |
|
339
|
`~33%` |
|
340
|
</Table.Cell> |
|
341
|
</Table.Row> |
|
342
|
</Table.Body> |
|
343
|
</Table> |