Raw
1 ---
2 sidebar_label: "Create Cache Module"
3 tags:
4 - cache
5 - how to
6 - server
7 ---
8
9 export const metadata = {
10 title: `How to Create a Cache Module`,
11 }
12
13 # {metadata.title}
14
15 In this guide, you’ll learn how to create a Cache Module.
16
17 {/* TODO add link */}
18
19 <Note title="Deprecation Notice">
20
21 The Cache Module is deprecated starting from [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0). [Create a Caching Module Provider](#) instead.
22
23 </Note>
24
25 ## 1. Create Module Directory
26
27 Start by creating a new directory for your module. For example, `src/modules/my-cache`.
28
29 ---
30
31 ## 2. Create the Cache Service
32
33 Create the file `src/modules/my-cache/service.ts` that holds the implementation of the cache service.
34
35 The Cache Module's main service must implement the `ICacheService` interface imported from `@medusajs/framework/types`:
36
37 ```ts title="src/modules/my-cache/service.ts"
38 import { ICacheService } from "@medusajs/framework/types"
39 import { MedusaError } from "@medusajs/framework/utils"
40
41 class MyCacheService implements ICacheService {
42 get<T>(key: string): Promise<T> {
43 throw new MedusaError(
44 MedusaError.Types.UNEXPECTED_STATE,
45 "Method not implemented."
46 )
47 }
48 set(key: string, data: unknown, ttl?: number): Promise<void> {
49 throw new MedusaError(
50 MedusaError.Types.UNEXPECTED_STATE,
51 "Method not implemented."
52 )
53 }
54 invalidate(key: string): Promise<void> {
55 throw new MedusaError(
56 MedusaError.Types.UNEXPECTED_STATE,
57 "Method not implemented."
58 )
59 }
60 }
61
62 export default MyCacheService
63 ```
64
65 The service implements the required methods based on the desired caching mechanism.
66
67 ### Implement get Method
68
69 The `get` method retrieves the value of a cached item based on its key.
70
71 The method accepts a string as a first parameter, which is the key in the cache. It either returns the cached item or `null` if it doesn’t exist.
72
73 For example, to implement this method using Memcached:
74
75 ```ts title="src/modules/my-cache/service.ts"
76 class MyCacheService implements ICacheService {
77 // ...
78 async get<T>(cacheKey: string): Promise<T | null> {
79 return new Promise((res, rej) => {
80 this.memcached.get(cacheKey, (err, data) => {
81 if (err) {
82 res(null)
83 } else {
84 if (data) {
85 res(JSON.parse(data))
86 } else {
87 res(null)
88 }
89 }
90 })
91 })
92 }
93 }
94 ```
95
96 ### Implement set Method
97
98 The `set` method is used to set an item in the cache. It accepts three parameters:
99
100 1. The first parameter is a string indicating the key of the data being added to the cache. This key can be used later to get or invalidate the cached item.
101 2. The second parameter is the data to be added to the cache. The data can be of any type.
102 3. The third parameter is optional. It’s a number indicating how long (in seconds) the data should be kept in the cache.
103
104 For example, to implement this method using Memcached:
105
106 ```ts title="src/modules/my-cache/service.ts"
107 class MyCacheService implements ICacheService {
108 protected TTL = 60
109 // ...
110 async set(
111 key: string,
112 data: Record<string, unknown>,
113 ttl: number = this.TTL // or any value
114 ): Promise<void> {
115 return new Promise((res, rej) =>
116 this.memcached.set(
117 key, JSON.stringify(data), ttl, (err) => {
118 if (err) {
119 rej(err)
120 } else {
121 res()
122 }
123 })
124 )
125 }
126 }
127 ```
128
129 ### Implement invalidate Method
130
131 The `invalidate` method removes an item from the cache using its key.
132
133 By default, items are removed from the cache when their time-to-live (ttl) expires. The `invalidate` method can be used to remove the item beforehand.
134
135 The method accepts a string as a first parameter, which is the key of the item to invalidate and remove from the cache.
136
137 For example, to implement this method using Memcached:
138
139 ```ts title="src/modules/my-cache/service.ts"
140 class MyCacheService implements ICacheService {
141 // ...
142 async invalidate(key: string): Promise<void> {
143 return new Promise((res, rej) => {
144 this.memcached.del(key, (err) => {
145 if (err) {
146 rej(err)
147 } else {
148 res()
149 }
150 })
151 })
152 }
153 }
154 ```
155
156 ---
157
158 ## 3. Create Module Definition File
159
160 Create the file `src/modules/my-cache/index.ts` with the following content:
161
162 ```ts title="src/modules/my-cache/index.ts"
163 import MyCacheService from "./service"
164 import { Module } from "@medusajs/framework/utils"
165
166 export default Module("my_cache", {
167 service: MyCacheService,
168 })
169 ```
170
171 This exports the module's definition, indicating that the `MyCacheService` is the main service of the module.
172
173 ---
174
175 ## 4. Use Module
176
177 To use your Cache Module, add it to the `modules` object exported as part of the configurations in `medusa-config.ts`. A Cache Module is added under the `cacheService` key.
178
179 For example:
180
181 ```ts title="medusa-config.ts"
182 import { Modules } from "@medusajs/framework/utils"
183
184 // ...
185
186 module.exports = defineConfig({
187 // ...
188 modules: [
189 {
190 resolve: "./src/modules/my-cache",
191 options: {
192 // any options
193 ttl: 30,
194 },
195 },
196 ],
197 })
198 ```