1 import { Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `Migrate from Cache Module to Caching Module`,
5 }
6
7 # {metadata.title}
8
9 In this guide, you'll learn how to migrate from the deprecated [Cache Module](../../cache/page.mdx) to the new [Caching Module](../page.mdx).
10
11 <Note>
12
13 The Caching Module is available starting [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0).
14
15 </Note>
16
17 <Note title="This guide is for developers who have">
18
19 1. Set up a Cache Module in `medusa-config.ts`.
20 2. Used the Cache Module's service in their code.
21
22 If you haven't done either of these, you don't need to migrate to the Caching Module. You can refer to the [Caching Module guide](../page.mdx#install-the-caching-module) to learn how to set it up.
23
24 </Note>
25
26 ## Why Migrate to the Caching Module?
27
28 The Caching Module provides improved performance, flexibility, and scalability compared to the deprecated Cache Module.
29
30 It also offers a better developer experience, making it easier to cache Query results and other data.
31
32 Additionally, the Caching Module supports registering multiple caching providers, such as Redis and Memcached, allowing you to use different caching backends in your application.
33
34 ---
35
36 ## Architectural Module Changes
37
38 The Cache Module implements caching logic, including integration with third-party caching services like Redis. For example, the Redis Cache Module handled connecting to the Redis server and performing caching operations.
39
40 The Caching Module, on the other hand, follows a provider-based architecture. The Caching Module provides the interface you use to manage cached data, while Caching Module Providers implement the actual caching logic. You can choose which provider to use, such as the Redis Caching Module Provider or a custom provider you create.
41
42 This separation of concerns allows for greater flexibility and extensibility. You can easily switch between different caching providers or create your own custom provider without modifying the core Caching Module.
43
44 ![Diagram illustrating the change in architecture from Cache Module to Caching Module](https://res.cloudinary.com/dza7lstvk/image/upload/v1759845565/Medusa%20Resources/cache-to-caching_yxaped.jpg)
45
46 ---
47
48 ## How to Migrate to the Caching Module
49
50 <Prerequisites
51 items={[
52 {
53 text: "Updated your Medusa application to v2.11.0 or later",
54 link: "https://github.com/medusajs/medusa/releases/tag/v2.11.0"
55 }
56 ]}
57 />
58
59 ### 1. Remove the Cache Module
60
61 The first step is to remove the Cache Module from your `medusa-config.ts` file.
62
63 <Note title="Warning" type="warning">
64
65 If you use OAuth-based authentication providers such as Google or GitHub login, the Auth Module uses the Cache Module to store the OAuth state parameter between the redirect and the callback. Removing the Cache Module without a Redis provider causes Medusa to fall back to an in-memory cache, which breaks OAuth flows across multiple application instances or after a restart.
66
67 Keep the Cache Module registered with a Redis provider until the Auth Module is updated to use the Caching Module instead.
68
69 </Note>
70
71 For example, if you set up the Redis Cache Module, remove the following code from `medusa-config.ts`:
72
73 ```ts title="medusa-config.ts" highlights={[["5"], ["6"], ["7"], ["8"], ["9"], ["10"]]}
74 module.exports = defineConfig({
75 // ...
76 modules: [
77 // REMOVE THE FOLLOWING LINES
78 {
79 resolve: "@medusajs/medusa/cache-redis",
80 options: {
81 redisUrl: process.env.CACHE_REDIS_URL,
82 },
83 },
84 ],
85 })
86 ```
87
88 ### 2. Install and Register the Caching Module
89
90 The Caching Module is installed by default in your application. To use it, enable the caching feature flag and register the module in your `medusa-config.ts` file.
91
92 Refer to the [Caching Module guide](../page.mdx#install-the-caching-module) for setup instructions.
93
94 ### 3. (Optional) Update Your Code to Use the Caching Module's Service
95
96 If you're using the Cache Module's service in your code, update it to use the Caching Module's service instead.
97
98 #### Container Key Change
99
100 Previously, you resolved the Cache Module's service using the `Modules.CACHE` or `cache` key.
101
102 Now, use the `Modules.CACHING` or `caching` key to resolve the Caching Module's service. For example:
103
104 ```ts
105 const cachingModuleService = container.resolve(Modules.CACHING)
106 // or
107 const cachingModuleService = container.resolve(Modules.CACHING)
108 ```
109
110 #### Method Changes
111
112 The Caching Module's service has similar methods to the Cache Module's service:
113
114 1. `get` -> Use the Caching Module's [get method](/references/caching-service#get).
115 2. `set` -> Use the Caching Module's [set method](/references/caching-service#set).
116 3. `invalidate` -> Use the Caching Module's [clear method](/references/caching-service#clear).
117
118 ### 4. (Optional) Create Custom Caching Module Provider
119
120 If you have a custom Cache Module, recreate it as a custom Caching Module Provider. For example, recreate a custom Memcached Cache Module as a Caching Module Provider.
121
122 The Caching Module Provider's service has similar methods to the Cache Module's service. Refer to the [Create Caching Module Provider guide](/references/caching-module-provider) for instructions on creating a custom Caching Module Provider.
123
124 ---
125
126 ## Understand Caching Changes
127
128 With the Cache Module, you handled caching and invalidation manually.
129
130 The Caching Module can now handle caching and invalidation automatically for certain operations, such as caching Query results. You can still use the Caching Module's service to cache custom data.
131
132 Learn more about the Caching Module in the [Caching Module guide](../page.mdx).