|
1
|
import { ICachingModuleService, Logger, MedusaContainer } from "@medusajs/types" |
|
2
|
import { MedusaContextType, Modules } from "../modules-sdk" |
|
3
|
import { FeatureFlag } from "../feature-flags" |
|
4
|
import { ContainerRegistrationKeys, isObject } from "../common" |
|
5
|
|
|
6
|
/** |
|
7
|
* This function is used to cache the result of a function call. |
|
8
|
* |
|
9
|
* @param cb - The callback to execute. |
|
10
|
* @param options - The options for the cache. |
|
11
|
* @returns The result of the callback. |
|
12
|
*/ |
|
13
|
export async function useCache<T>( |
|
14
|
cb: (...args: any[]) => Promise<T>, |
|
15
|
options: { |
|
16
|
enable?: boolean |
|
17
|
key: string | any[] |
|
18
|
tags?: string[] |
|
19
|
/** |
|
20
|
* Whether the automatically computed tags should be applied along with the ones |
|
21
|
* given in `tags`, rather than `tags` being used on its own. No effect when `tags` |
|
22
|
* is omitted. |
|
23
|
*/ |
|
24
|
computeAutomaticTags?: boolean |
|
25
|
ttl?: number |
|
26
|
/** |
|
27
|
* Whethere the default strategy should auto invalidate the cache whenever it is possible. |
|
28
|
*/ |
|
29
|
autoInvalidate?: boolean |
|
30
|
providers?: string[] |
|
31
|
container: MedusaContainer |
|
32
|
} |
|
33
|
): Promise<T> { |
|
34
|
const cachingModule = options.container.resolve<ICachingModuleService>( |
|
35
|
Modules.CACHING, |
|
36
|
{ |
|
37
|
allowUnregistered: true, |
|
38
|
} |
|
39
|
) |
|
40
|
|
|
41
|
if ( |
|
42
|
!options.enable || |
|
43
|
!FeatureFlag.isFeatureEnabled("caching") || |
|
44
|
!cachingModule |
|
45
|
) { |
|
46
|
return await cb() |
|
47
|
} |
|
48
|
|
|
49
|
let key: string |
|
50
|
if (typeof options.key === "string") { |
|
51
|
key = options.key |
|
52
|
} else { |
|
53
|
key = await cachingModule.computeKey(options.key) |
|
54
|
} |
|
55
|
|
|
56
|
const data = await cachingModule.get({ |
|
57
|
key, |
|
58
|
tags: options.tags, |
|
59
|
providers: options.providers, |
|
60
|
}) |
|
61
|
|
|
62
|
if (data) { |
|
63
|
return data as T |
|
64
|
} |
|
65
|
|
|
66
|
const result = await cb() |
|
67
|
|
|
68
|
void cachingModule |
|
69
|
.set({ |
|
70
|
key, |
|
71
|
tags: options.tags, |
|
72
|
computeAutomaticTags: options.computeAutomaticTags, |
|
73
|
ttl: options.ttl, |
|
74
|
data: result as object, |
|
75
|
options: { autoInvalidate: options.autoInvalidate }, |
|
76
|
providers: options.providers, |
|
77
|
}) |
|
78
|
.catch((e) => { |
|
79
|
const logger = |
|
80
|
options.container.resolve<Logger>(ContainerRegistrationKeys.LOGGER, { |
|
81
|
allowUnregistered: true, |
|
82
|
}) ?? (console as unknown as Logger) |
|
83
|
logger.error( |
|
84
|
`An error occurred while setting cache for key: ${key}\n${e.message}\n${e.stack}` |
|
85
|
) |
|
86
|
}) |
|
87
|
|
|
88
|
return result |
|
89
|
} |
|
90
|
|
|
91
|
type TargetMethodArgs<Target, PropertyKey> = Target[PropertyKey & |
|
92
|
keyof Target] extends (...args: any[]) => any |
|
93
|
? Parameters<Target[PropertyKey & keyof Target]> |
|
94
|
: never |
|
95
|
|
|
96
|
/** |
|
97
|
* This function is used to cache the result of a method call. |
|
98
|
* |
|
99
|
* @param options - The options for the cache. |
|
100
|
* @returns The original method with the cache applied. |
|
101
|
*/ |
|
102
|
export function Cached< |
|
103
|
const Target extends object, |
|
104
|
const PropertyKey extends keyof Target |
|
105
|
>(options: { |
|
106
|
/** |
|
107
|
* The key to use for the cache. |
|
108
|
* If a function is provided, it will be called with the arguments as the first argument and the |
|
109
|
* container as the second argument. |
|
110
|
*/ |
|
111
|
key?: |
|
112
|
| string |
|
113
|
| (( |
|
114
|
args: TargetMethodArgs<Target, PropertyKey>, |
|
115
|
cachingModule: ICachingModuleService |
|
116
|
) => string | Promise<string> | Promise<any[]> | any[]) |
|
117
|
/** |
|
118
|
* Whether to enable the cache. This is only useful if you want to enable without providing any |
|
119
|
* other options. |
|
120
|
*/ |
|
121
|
enable?: |
|
122
|
| boolean |
|
123
|
| ((args: TargetMethodArgs<Target, PropertyKey>) => boolean | undefined) |
|
124
|
/** |
|
125
|
* The tags to use for the cache. |
|
126
|
*/ |
|
127
|
tags?: |
|
128
|
| string[] |
|
129
|
| ((args: TargetMethodArgs<Target, PropertyKey>) => string[] | undefined) |
|
130
|
/** |
|
131
|
* Whether the automatically computed tags should be applied along with the ones given |
|
132
|
* in `tags`, rather than `tags` being used on its own. No effect when `tags` is |
|
133
|
* omitted. |
|
134
|
* |
|
135
|
* The function form exists so the option can be resolved from the decorated method's |
|
136
|
* arguments, the same way `tags` is. |
|
137
|
*/ |
|
138
|
computeAutomaticTags?: |
|
139
|
| boolean |
|
140
|
| ((args: TargetMethodArgs<Target, PropertyKey>) => boolean | undefined) |
|
141
|
/** |
|
142
|
* The time-to-live (TTL) value in seconds. |
|
143
|
*/ |
|
144
|
ttl?: |
|
145
|
| number |
|
146
|
| ((args: TargetMethodArgs<Target, PropertyKey>) => number | undefined) |
|
147
|
/** |
|
148
|
* Whether to auto invalidate the cache whenever it is possible. |
|
149
|
*/ |
|
150
|
autoInvalidate?: |
|
151
|
| boolean |
|
152
|
| ((args: TargetMethodArgs<Target, PropertyKey>) => boolean | undefined) |
|
153
|
/** |
|
154
|
* The providers to use for the cache. |
|
155
|
*/ |
|
156
|
providers?: |
|
157
|
| string[] |
|
158
|
| ((args: TargetMethodArgs<Target, PropertyKey>) => string[] | undefined) |
|
159
|
|
|
160
|
container: MedusaContainer | ((this: Target) => MedusaContainer) |
|
161
|
}) { |
|
162
|
return function ( |
|
163
|
target: Target, |
|
164
|
propertyKey: PropertyKey, |
|
165
|
descriptor: PropertyDescriptor |
|
166
|
) { |
|
167
|
const originalMethod = descriptor.value |
|
168
|
|
|
169
|
if (typeof originalMethod !== "function") { |
|
170
|
throw new Error("@cached can only be applied to methods") |
|
171
|
} |
|
172
|
|
|
173
|
descriptor.value = async function ( |
|
174
|
...args: Target[PropertyKey & keyof Target] extends ( |
|
175
|
...args: any[] |
|
176
|
) => any |
|
177
|
? Parameters<Target[PropertyKey & keyof Target]> |
|
178
|
: never |
|
179
|
) { |
|
180
|
const container: MedusaContainer = |
|
181
|
typeof options.container === "function" |
|
182
|
? options.container.call(this) |
|
183
|
: options.container |
|
184
|
|
|
185
|
const cachingModule = container.resolve<ICachingModuleService>( |
|
186
|
Modules.CACHING, |
|
187
|
{ |
|
188
|
allowUnregistered: true, |
|
189
|
} |
|
190
|
) |
|
191
|
|
|
192
|
if (!FeatureFlag.isFeatureEnabled("caching") || !cachingModule) { |
|
193
|
return await originalMethod.apply(this, args) |
|
194
|
} |
|
195
|
|
|
196
|
if (!options.key) { |
|
197
|
options.key = await cachingModule.computeKey( |
|
198
|
args |
|
199
|
.map((arg) => { |
|
200
|
if (isObject(arg)) { |
|
201
|
// Prevent any container, manager, transactionManager, etc from being included in the key |
|
202
|
const { |
|
203
|
container, |
|
204
|
manager, |
|
205
|
transactionManager, |
|
206
|
__type, |
|
207
|
...rest |
|
208
|
} = arg as any |
|
209
|
if (__type === MedusaContextType) { |
|
210
|
return |
|
211
|
} |
|
212
|
return rest |
|
213
|
} |
|
214
|
return arg |
|
215
|
}) |
|
216
|
.filter(Boolean) |
|
217
|
) |
|
218
|
} |
|
219
|
|
|
220
|
const resolvableKeys = [ |
|
221
|
"enable", |
|
222
|
"key", |
|
223
|
"tags", |
|
224
|
"computeAutomaticTags", |
|
225
|
"ttl", |
|
226
|
"autoInvalidate", |
|
227
|
"providers", |
|
228
|
] |
|
229
|
|
|
230
|
const cacheOptions = {} as Parameters<typeof useCache>[1] |
|
231
|
|
|
232
|
const promises: Promise<any>[] = [] |
|
233
|
for (const key of resolvableKeys) { |
|
234
|
if (typeof options[key] === "function") { |
|
235
|
const res = options[key](args, cachingModule) |
|
236
|
if (res instanceof Promise) { |
|
237
|
promises.push( |
|
238
|
res.then((value) => { |
|
239
|
cacheOptions[key] = value |
|
240
|
}) |
|
241
|
) |
|
242
|
} else { |
|
243
|
cacheOptions[key] = res |
|
244
|
} |
|
245
|
} else { |
|
246
|
cacheOptions[key] = options[key] |
|
247
|
} |
|
248
|
} |
|
249
|
|
|
250
|
await Promise.all(promises) |
|
251
|
|
|
252
|
if (!cacheOptions.enable) { |
|
253
|
return await originalMethod.apply(this, args) |
|
254
|
} |
|
255
|
|
|
256
|
Object.assign(cacheOptions, { |
|
257
|
container, |
|
258
|
}) |
|
259
|
|
|
260
|
return await useCache( |
|
261
|
() => originalMethod.apply(this, args), |
|
262
|
cacheOptions as Parameters<typeof useCache>[1] |
|
263
|
) |
|
264
|
} |
|
265
|
|
|
266
|
return descriptor |
|
267
|
} |
|
268
|
} |