|
1
|
import { Table, Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Redis Caching Module Provider`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
The Redis Caching Module Provider is a robust caching solution that leverages [Redis](https://redis.io/) to store cached data. Redis offers high performance, scalability, and data persistence, making it an ideal choice for production environments. |
|
10
|
|
|
11
|
<Note> |
|
12
|
|
|
13
|
The Caching Module and its providers are available starting [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0). |
|
14
|
|
|
15
|
</Note> |
|
16
|
|
|
17
|
--- |
|
18
|
|
|
19
|
## Register the Redis Caching Module |
|
20
|
|
|
21
|
<Prerequisites items={[ |
|
22
|
{ |
|
23
|
text: "Redis installed and Redis server running", |
|
24
|
link: "https://redis.io/docs/getting-started/installation/" |
|
25
|
} |
|
26
|
]} /> |
|
27
|
|
|
28
|
To use the Redis Caching Module Provider, you need to register it in the `providers` array of the Caching Module in your `medusa-config.ts`. |
|
29
|
|
|
30
|
```ts title="medusa-config.ts" |
|
31
|
module.exports = defineConfig({ |
|
32
|
// ... |
|
33
|
modules: [ |
|
34
|
{ |
|
35
|
resolve: "@medusajs/medusa/caching", |
|
36
|
options: { |
|
37
|
providers: [ |
|
38
|
{ |
|
39
|
resolve: "@medusajs/caching-redis", |
|
40
|
id: "caching-redis", |
|
41
|
// Optional, makes this the default caching provider |
|
42
|
is_default: true, |
|
43
|
options: { |
|
44
|
redisUrl: process.env.CACHE_REDIS_URL, |
|
45
|
redisOptions: { |
|
46
|
// ioredis options... |
|
47
|
}, |
|
48
|
}, |
|
49
|
}, |
|
50
|
// other caching providers... |
|
51
|
], |
|
52
|
}, |
|
53
|
}, |
|
54
|
], |
|
55
|
}) |
|
56
|
``` |
|
57
|
|
|
58
|
Notice that you pass an `id` property to the provider. The provider will be registered with that ID, which you can use to explicitly [specify the provider when caching data](../page.mdx#how-to-use-the-caching-module). |
|
59
|
|
|
60
|
### Environment Variables |
|
61
|
|
|
62
|
Make sure to add the following environment variable: |
|
63
|
|
|
64
|
```bash |
|
65
|
CACHE_REDIS_URL=redis://localhost:6379 |
|
66
|
``` |
|
67
|
|
|
68
|
### Redis Caching Module Options |
|
69
|
|
|
70
|
<Table> |
|
71
|
<Table.Header> |
|
72
|
<Table.Row> |
|
73
|
<Table.HeaderCell> |
|
74
|
Option |
|
75
|
</Table.HeaderCell> |
|
76
|
<Table.HeaderCell> |
|
77
|
Description |
|
78
|
</Table.HeaderCell> |
|
79
|
<Table.HeaderCell> |
|
80
|
Default |
|
81
|
</Table.HeaderCell> |
|
82
|
</Table.Row> |
|
83
|
</Table.Header> |
|
84
|
<Table.Body> |
|
85
|
<Table.Row> |
|
86
|
<Table.Cell> |
|
87
|
`redisUrl` |
|
88
|
</Table.Cell> |
|
89
|
<Table.Cell> |
|
90
|
The connection URL for the Redis server. |
|
91
|
</Table.Cell> |
|
92
|
<Table.Cell> |
|
93
|
Required. An error is thrown if not provided. |
|
94
|
</Table.Cell> |
|
95
|
</Table.Row> |
|
96
|
<Table.Row> |
|
97
|
<Table.Cell> |
|
98
|
`ttl` |
|
99
|
</Table.Cell> |
|
100
|
<Table.Cell> |
|
101
|
A number indicating the default time-to-live (TTL) in seconds for cached items. After this period, cached items will be removed from the cache. |
|
102
|
</Table.Cell> |
|
103
|
<Table.Cell> |
|
104
|
`3600` (1 hour) |
|
105
|
</Table.Cell> |
|
106
|
</Table.Row> |
|
107
|
<Table.Row> |
|
108
|
<Table.Cell> |
|
109
|
`prefix` |
|
110
|
</Table.Cell> |
|
111
|
<Table.Cell> |
|
112
|
A string to prefix all cache keys with. This is useful for namespacing your cache keys, especially when sharing a Redis instance with other applications or modules. |
|
113
|
</Table.Cell> |
|
114
|
<Table.Cell> |
|
115
|
No prefix by default |
|
116
|
</Table.Cell> |
|
117
|
</Table.Row> |
|
118
|
<Table.Row> |
|
119
|
<Table.Cell> |
|
120
|
`compressionThreshold` |
|
121
|
</Table.Cell> |
|
122
|
<Table.Cell> |
|
123
|
A number indicating the size threshold in bytes above which cached items will be compressed before being stored in Redis. This helps save memory when caching large items. |
|
124
|
</Table.Cell> |
|
125
|
<Table.Cell> |
|
126
|
`1024` (1 KB) |
|
127
|
</Table.Cell> |
|
128
|
</Table.Row> |
|
129
|
<Table.Row> |
|
130
|
<Table.Cell> |
|
131
|
`redisOptions` |
|
132
|
</Table.Cell> |
|
133
|
<Table.Cell> |
|
134
|
An object of [ioredis options](https://github.com/redis/ioredis/blob/main/lib/redis/RedisOptions.ts) to pass to the Redis client. Available since Medusa v2.18.0. |
|
135
|
</Table.Cell> |
|
136
|
<Table.Cell> |
|
137
|
- |
|
138
|
</Table.Cell> |
|
139
|
</Table.Row> |
|
140
|
</Table.Body> |
|
141
|
</Table> |
|
142
|
|
|
143
|
--- |
|
144
|
|
|
145
|
## Test the Redis Caching Module |
|
146
|
|
|
147
|
You can test the Redis Caching Module by caching data using the Query or Index Module, or by directly using the Caching Module's service, as described in the [Caching Module guide](../page.mdx#how-to-use-the-caching-module). |
|
148
|
|
|
149
|
If you don't set the Redis Caching Module Provider as the default, you can explicitly specify its provider ID `caching-redis` when caching data with Query, the Index Module, or directly with the Caching Module's service. |
|
150
|
|
|
151
|
<Note title="Tip"> |
|
152
|
|
|
153
|
`caching-redis` is the ID you set in the `medusa-config.ts` file when registering the Redis Caching Module Provider. |
|
154
|
|
|
155
|
</Note> |
|
156
|
|
|
157
|
For example, you can create a workflow in `src/workflows/cache-products.ts` that caches products using the Redis Caching Module Provider: |
|
158
|
|
|
159
|
```ts title="src/workflows/cache-products.ts" highlights={[["14"], ["15"], ["16"], ["17"]]} |
|
160
|
import { |
|
161
|
createWorkflow, |
|
162
|
WorkflowResponse, |
|
163
|
} from "@medusajs/framework/workflows-sdk" |
|
164
|
import { useQueryGraphStep } from "@medusajs/medusa/core-flows" |
|
165
|
|
|
166
|
export const cacheProductsWorkflow = createWorkflow( |
|
167
|
"cache-products", |
|
168
|
() => { |
|
169
|
const { data: products } = useQueryGraphStep({ |
|
170
|
entity: "product", |
|
171
|
fields: ["id", "title"], |
|
172
|
options: { |
|
173
|
cache: { |
|
174
|
enable: true, |
|
175
|
providers: ["caching-redis"], |
|
176
|
}, |
|
177
|
}, |
|
178
|
}) |
|
179
|
|
|
180
|
return new WorkflowResponse(products) |
|
181
|
} |
|
182
|
) |
|
183
|
``` |
|
184
|
|
|
185
|
Next, execute that workflow in an [API route](!docs!/learn/fundamentals/api-routes). For example, create a route at `src/api/cache-product/route.ts` with the following content: |
|
186
|
|
|
187
|
```ts title="src/api/cache-product/route.ts" |
|
188
|
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http" |
|
189
|
import { cacheProductsWorkflow } from "../../workflows/cache-products" |
|
190
|
|
|
191
|
export const GET = async (req: MedusaRequest, res: MedusaResponse) => { |
|
192
|
const { result } = await cacheProductsWorkflow(req.scope) |
|
193
|
.run({}) |
|
194
|
|
|
195
|
res.status(200).json(result) |
|
196
|
} |
|
197
|
``` |
|
198
|
|
|
199
|
Finally, start your Medusa server with the following command: |
|
200
|
|
|
201
|
```bash npm2yarn |
|
202
|
npm run dev |
|
203
|
``` |
|
204
|
|
|
205
|
Then, make a `GET` request to the `/cache-product` endpoint: |
|
206
|
|
|
207
|
```bash |
|
208
|
curl http://localhost:9000/cache-product |
|
209
|
``` |
|
210
|
|
|
211
|
You should receive a response with the list of products. The first time you make this request, the products will be fetched from the database and cached in Redis. Subsequent requests will retrieve the products from the cache, resulting in improved performance. |