1 import { CardList, Card, Table, CodeTabs, CodeTab } from "docs-ui"
2
3 export const metadata = {
4 title: `Caching Module Providers`,
5 }
6
7 # {metadata.title}
8
9 In this guide, you'll learn about Caching Module Providers in Medusa, including how to configure and use them in your Medusa application.
10
11 ## What is a Caching Module Provider?
12
13 A Caching Module Provider implements the logic for caching data, such as integrating third-party caching services. The [Caching Module](../page.mdx) then uses the registered Caching Module Providers to handle caching data.
14
15 Medusa provides the [Redis Caching Module Provider](./redis/page.mdx) that you can use in development and production. You can also [Create a Caching Provider](/references/caching-module-provider).
16
17 <CardList
18 items={[
19 {
20 title: "Redis",
21 href: "/infrastructure-modules/caching/providers/redis",
22 badge: {
23 variant: "green",
24 children: "For Production"
25 }
26 },
27 {
28 title: "Memcached",
29 href: "/infrastructure-modules/caching/guides/memcached",
30 badge: {
31 variant: "blue",
32 children: "Tutorial"
33 }
34 }
35 ]}
36 />
37
38 ---
39
40 ## Default Caching Module Provider
41
42 You can register multiple Caching Module Providers and specify which one to use as the default. The Caching Module uses the default provider for all caching operations unless you specify a specific provider.
43
44 ### How is the Default Provider Selected?
45
46 The Caching Module determines the default Caching Module Provider based on the following scenarios:
47
48 <Table>
49 <Table.Header>
50 <Table.Row>
51 <Table.HeaderCell>
52 Scenario
53 </Table.HeaderCell>
54 <Table.HeaderCell>
55 Default Provider
56 </Table.HeaderCell>
57 </Table.Row>
58 </Table.Header>
59 <Table.Body>
60 <Table.Row>
61 <Table.Cell>
62 One provider is registered.
63 </Table.Cell>
64 <Table.Cell>
65 The registered provider.
66 </Table.Cell>
67 </Table.Row>
68 <Table.Row>
69 <Table.Cell>
70 Multiple providers and one of them has an `is_default` flag.
71 </Table.Cell>
72 <Table.Cell>
73 The provider with the `is_default` flag set to `true`.
74 </Table.Cell>
75 </Table.Row>
76 </Table.Body>
77 </Table>
78
79 If none of the above scenarios apply, the Caching Module throws an error during startup indicating that no default provider is configured.
80
81 ### Setting the Default Caching Module Provider
82
83 To specify a provider as the default, you can set its `is_default` option to `true` when registering it in the `provider` array of the Caching Module.
84
85 For example:
86
87 ```ts title="medusa-config.ts"
88 module.exports = defineConfig({
89 // ...
90 modules: [
91 {
92 resolve: "@medusajs/medusa/caching",
93 options: {
94 providers: [
95 {
96 id: "caching-redis",
97 resolve: "@medusajs/caching-redis",
98 is_default: true, // Set as the default provider
99 options: {
100 // Redis options...
101 },
102 },
103 {
104 id: "caching-memcached",
105 resolve: "./path/to/your/memcached-provider",
106 options: {
107 // Memcached options...
108 },
109 },
110 ],
111 },
112 },
113 ],
114 })
115 ```
116
117 In this example, the Redis Caching Module Provider is set as the default provider by setting `is_default: true`. The Memcached Caching Module Provider is also registered but not set as the default.
118
119 ---
120
121 ## Caching with Specific Providers
122
123 Whether you're caching data with Query, the Index Module, or directly using the Caching Module's service, you can specify an array of provider IDs to use for that specific caching operation.
124
125 For example, considering you have the [above configuration](../page.mdx#setting-the-default-caching-module-provider) with both Redis and Memcached providers registered, you can specify which provider to use when caching data:
126
127 <CodeTabs group="caching-method">
128 <CodeTab label="Query / Index Module" value="query-index">
129
130 ```ts highlights={[["7"]]}
131 const { data: products } = useQueryGraphStep({
132 entity: "product",
133 fields: ["id", "title"],
134 options: {
135 cache: {
136 enable: true,
137 providers: ["caching-memcached"], // Specify Memcached provider
138 },
139 },
140 })
141 ```
142
143 </CodeTab>
144 <CodeTab label="Caching Module Service" value="caching-service">
145
146 ```ts highlights={[["6"]]}
147 const cachingModuleService = container.resolve(Modules.CACHING)
148
149 await cachingModuleService.set({
150 key: "product-list",
151 data: products,
152 providers: ["caching-memcached"], // Specify Memcached provider
153 })
154 ```
155
156 </CodeTab>
157 </CodeTabs>
158
159 In this example, both Query and the Caching Module's service use the Memcached provider, overriding the default Redis provider.
160
161 The ID you pass is the same ID you specified in `medusa-config.ts` when registering the provider.