|
1
|
import { MedusaModule } from "@medusajs/framework/modules-sdk" |
|
2
|
import { ICachingModuleService } from "@medusajs/framework/types" |
|
3
|
import { Modules } from "@medusajs/framework/utils" |
|
4
|
import { moduleIntegrationTestRunner } from "@medusajs/test-utils" |
|
5
|
import { setTimeout } from "timers/promises" |
|
6
|
import { EventBusServiceMock } from "../../__fixtures__/event-bus-mock" |
|
7
|
|
|
8
|
jest.setTimeout(300000) |
|
9
|
|
|
10
|
jest.spyOn(MedusaModule, "getAllJoinerConfigs").mockReturnValue([ |
|
11
|
{ |
|
12
|
schema: ` |
|
13
|
type Product { |
|
14
|
id: ID |
|
15
|
title: String |
|
16
|
handle: String |
|
17
|
status: String |
|
18
|
type_id: String |
|
19
|
collection_id: String |
|
20
|
is_giftcard: Boolean |
|
21
|
external_id: String |
|
22
|
created_at: DateTime |
|
23
|
updated_at: DateTime |
|
24
|
|
|
25
|
variants: [ProductVariant] |
|
26
|
sales_channels: [SalesChannel] |
|
27
|
} |
|
28
|
|
|
29
|
type ProductVariant { |
|
30
|
id: ID |
|
31
|
product_id: String |
|
32
|
sku: String |
|
33
|
|
|
34
|
prices: [Price] |
|
35
|
} |
|
36
|
|
|
37
|
type Price { |
|
38
|
id: ID |
|
39
|
amount: Float |
|
40
|
currency_code: String |
|
41
|
variant_id: String |
|
42
|
} |
|
43
|
|
|
44
|
type SalesChannel { |
|
45
|
id: ID |
|
46
|
is_disabled: Boolean |
|
47
|
} |
|
48
|
|
|
49
|
type ProductCollection { |
|
50
|
id: ID |
|
51
|
title: String |
|
52
|
handle: String |
|
53
|
} |
|
54
|
`, |
|
55
|
}, |
|
56
|
]) |
|
57
|
|
|
58
|
const DEFAULT_WAIT_INTERVAL = 50 |
|
59
|
const mockEventBus = new EventBusServiceMock() |
|
60
|
|
|
61
|
moduleIntegrationTestRunner<ICachingModuleService>({ |
|
62
|
moduleName: Modules.CACHING, |
|
63
|
injectedDependencies: { |
|
64
|
[Modules.EVENT_BUS]: mockEventBus, |
|
65
|
}, |
|
66
|
moduleOptions: { |
|
67
|
providers: [ |
|
68
|
{ |
|
69
|
id: "cache-redis", |
|
70
|
resolve: require.resolve("../../../../providers/caching-redis/src"), |
|
71
|
is_default: true, |
|
72
|
options: { |
|
73
|
redisUrl: "localhost:6379", |
|
74
|
}, |
|
75
|
}, |
|
76
|
], |
|
77
|
}, |
|
78
|
testSuite: ({ service }) => { |
|
79
|
describe("Cache Invalidation with Entity Relationships", () => { |
|
80
|
afterEach(async () => { |
|
81
|
await service.clear({ tags: ["*"] }).catch(() => {}) |
|
82
|
}) |
|
83
|
|
|
84
|
describe("Single Entity Caching", () => { |
|
85
|
it("should cache and retrieve a single product entity using computed keys", async () => { |
|
86
|
const product = { |
|
87
|
id: "prod_1", |
|
88
|
title: "Test Product", |
|
89
|
handle: "test-product", |
|
90
|
status: "published", |
|
91
|
created_at: new Date().toISOString(), |
|
92
|
updated_at: new Date().toISOString(), |
|
93
|
} |
|
94
|
|
|
95
|
const productKey = await service.computeKey(product) |
|
96
|
|
|
97
|
await service.set({ |
|
98
|
key: productKey, |
|
99
|
data: product, |
|
100
|
}) |
|
101
|
|
|
102
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
103
|
|
|
104
|
const cachedProduct = await service.get({ key: productKey }) |
|
105
|
expect(cachedProduct).toEqual(product) |
|
106
|
}) |
|
107
|
|
|
108
|
it("should auto-invalidate single entity when strategy clears computed tags", async () => { |
|
109
|
const product = { |
|
110
|
id: "prod_1", |
|
111
|
title: "Test Product", |
|
112
|
handle: "test-product", |
|
113
|
} |
|
114
|
|
|
115
|
const productKey = await service.computeKey(product) |
|
116
|
|
|
117
|
await service.set({ |
|
118
|
key: productKey, |
|
119
|
data: product, |
|
120
|
}) |
|
121
|
|
|
122
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
123
|
|
|
124
|
await mockEventBus.emit( |
|
125
|
[{ name: "product.updated", data: { id: product.id } }], |
|
126
|
{} |
|
127
|
) |
|
128
|
|
|
129
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
130
|
|
|
131
|
const result = await service.get({ key: productKey }) |
|
132
|
expect(result).toBeNull() |
|
133
|
}) |
|
134
|
|
|
135
|
it("should not auto-invalidate single entity with autoInvalidate=false", async () => { |
|
136
|
const product = { |
|
137
|
id: "prod_1", |
|
138
|
title: "Test Product", |
|
139
|
handle: "test-product", |
|
140
|
} |
|
141
|
|
|
142
|
const productKey = await service.computeKey(product) |
|
143
|
|
|
144
|
await service.set({ |
|
145
|
key: productKey, |
|
146
|
data: product, |
|
147
|
options: { autoInvalidate: false }, |
|
148
|
}) |
|
149
|
|
|
150
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
151
|
|
|
152
|
await mockEventBus.emit( |
|
153
|
[{ name: "product.updated", data: { id: product.id } }], |
|
154
|
{} |
|
155
|
) |
|
156
|
|
|
157
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
158
|
|
|
159
|
const result = await service.get({ key: productKey }) |
|
160
|
expect(result).toEqual(product) |
|
161
|
}) |
|
162
|
}) |
|
163
|
|
|
164
|
describe("Entity List Caching", () => { |
|
165
|
it("should cache and retrieve lists of entities using computed keys", async () => { |
|
166
|
const publishedProductsQuery = { |
|
167
|
entity: "product", |
|
168
|
filters: { status: "published" }, |
|
169
|
fields: ["id", "title", "status"], |
|
170
|
} |
|
171
|
|
|
172
|
const allProductsQuery = { |
|
173
|
entity: "product", |
|
174
|
filters: {}, |
|
175
|
fields: ["id", "title", "status"], |
|
176
|
} |
|
177
|
|
|
178
|
const publishedProducts = [ |
|
179
|
{ id: "prod_1", title: "Product 1", status: "published" }, |
|
180
|
{ id: "prod_2", title: "Product 2", status: "published" }, |
|
181
|
] |
|
182
|
|
|
183
|
const allProducts = [ |
|
184
|
...publishedProducts, |
|
185
|
{ id: "prod_3", title: "Product 3", status: "draft" }, |
|
186
|
] |
|
187
|
|
|
188
|
const publishedProductsKey = await service.computeKey( |
|
189
|
publishedProductsQuery |
|
190
|
) |
|
191
|
const allProductsKey = await service.computeKey(allProductsQuery) |
|
192
|
|
|
193
|
await service.set({ |
|
194
|
key: publishedProductsKey, |
|
195
|
data: publishedProducts, |
|
196
|
}) |
|
197
|
|
|
198
|
await service.set({ |
|
199
|
key: allProductsKey, |
|
200
|
data: allProducts, |
|
201
|
}) |
|
202
|
|
|
203
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
204
|
|
|
205
|
const cachedPublished = await service.get({ |
|
206
|
key: publishedProductsKey, |
|
207
|
}) |
|
208
|
const cachedAll = await service.get({ key: allProductsKey }) |
|
209
|
|
|
210
|
expect(cachedPublished).toEqual(publishedProducts) |
|
211
|
expect(cachedAll).toEqual(allProducts) |
|
212
|
}) |
|
213
|
|
|
214
|
it("should invalidate related lists when individual product is updated", async () => { |
|
215
|
const listQuery = { |
|
216
|
entity: "product", |
|
217
|
filters: { status: "published" }, |
|
218
|
includes: ["id", "title"], |
|
219
|
} |
|
220
|
|
|
221
|
const products = [ |
|
222
|
{ id: "prod_1", title: "Product 1", status: "published" }, |
|
223
|
{ id: "prod_2", title: "Product 2", status: "published" }, |
|
224
|
] |
|
225
|
|
|
226
|
const listKey = await service.computeKey(listQuery) |
|
227
|
|
|
228
|
await service.set({ |
|
229
|
key: listKey, |
|
230
|
data: products, |
|
231
|
}) |
|
232
|
|
|
233
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
234
|
|
|
235
|
await mockEventBus.emit( |
|
236
|
[ |
|
237
|
{ |
|
238
|
name: "product.updated", |
|
239
|
data: { id: "prod_1", title: "Updated Product 1" }, |
|
240
|
}, |
|
241
|
], |
|
242
|
{} |
|
243
|
) |
|
244
|
|
|
245
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
246
|
|
|
247
|
const cachedList = await service.get({ key: listKey }) |
|
248
|
|
|
249
|
expect(cachedList).toBeNull() |
|
250
|
}) |
|
251
|
}) |
|
252
|
|
|
253
|
describe("Nested Entity Caching", () => { |
|
254
|
it("should cache products with nested variants and prices using computed keys", async () => { |
|
255
|
const productWithVariants = { |
|
256
|
id: "prod_1", |
|
257
|
title: "Complex Product", |
|
258
|
variants: [ |
|
259
|
{ |
|
260
|
id: "var_1", |
|
261
|
product_id: "prod_1", |
|
262
|
sku: "SKU-001", |
|
263
|
prices: [ |
|
264
|
{ |
|
265
|
id: "price_1", |
|
266
|
variant_id: "var_1", |
|
267
|
amount: 1000, |
|
268
|
currency_code: "USD", |
|
269
|
}, |
|
270
|
{ |
|
271
|
id: "price_2", |
|
272
|
variant_id: "var_1", |
|
273
|
amount: 900, |
|
274
|
currency_code: "EUR", |
|
275
|
}, |
|
276
|
], |
|
277
|
}, |
|
278
|
{ |
|
279
|
id: "var_2", |
|
280
|
product_id: "prod_1", |
|
281
|
sku: "SKU-002", |
|
282
|
prices: [ |
|
283
|
{ |
|
284
|
id: "price_3", |
|
285
|
variant_id: "var_2", |
|
286
|
amount: 1500, |
|
287
|
currency_code: "USD", |
|
288
|
}, |
|
289
|
], |
|
290
|
}, |
|
291
|
], |
|
292
|
} |
|
293
|
|
|
294
|
const productKey = await service.computeKey(productWithVariants) |
|
295
|
|
|
296
|
await service.set({ |
|
297
|
key: productKey, |
|
298
|
data: productWithVariants, |
|
299
|
}) |
|
300
|
|
|
301
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
302
|
|
|
303
|
const cached = await service.get<typeof productWithVariants>({ |
|
304
|
key: productKey, |
|
305
|
}) |
|
306
|
expect(cached).toEqual(productWithVariants) |
|
307
|
expect(cached!.variants).toHaveLength(2) |
|
308
|
expect(cached!.variants[0].prices).toHaveLength(2) |
|
309
|
}) |
|
310
|
|
|
311
|
it("should invalidate nested product when related variant is updated", async () => { |
|
312
|
const productWithVariants = { |
|
313
|
id: "prod_1", |
|
314
|
title: "Complex Product", |
|
315
|
variants: [{ id: "var_1", product_id: "prod_1", sku: "SKU-001" }], |
|
316
|
} |
|
317
|
|
|
318
|
const productKey = await service.computeKey(productWithVariants) |
|
319
|
|
|
320
|
await service.set({ |
|
321
|
key: productKey, |
|
322
|
data: productWithVariants, |
|
323
|
}) |
|
324
|
|
|
325
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
326
|
|
|
327
|
await mockEventBus.emit( |
|
328
|
[ |
|
329
|
{ |
|
330
|
name: "product_variant.updated", |
|
331
|
data: { |
|
332
|
id: "var_1", |
|
333
|
product_id: "prod_1", |
|
334
|
sku: "SKU-001-UPDATED", |
|
335
|
}, |
|
336
|
}, |
|
337
|
], |
|
338
|
{} |
|
339
|
) |
|
340
|
|
|
341
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
342
|
|
|
343
|
const result = await service.get({ key: productKey }) |
|
344
|
expect(result).toBeNull() |
|
345
|
}) |
|
346
|
|
|
347
|
it("should handle price updates affecting variant and product caches", async () => { |
|
348
|
const price = { |
|
349
|
id: "price_1", |
|
350
|
variant_id: "var_1", |
|
351
|
amount: 1000, |
|
352
|
currency_code: "USD", |
|
353
|
} |
|
354
|
|
|
355
|
const variant = { |
|
356
|
id: "var_1", |
|
357
|
product_id: "prod_1", |
|
358
|
sku: "SKU-001", |
|
359
|
prices: [price], |
|
360
|
} |
|
361
|
|
|
362
|
const product = { |
|
363
|
id: "prod_1", |
|
364
|
title: "Product", |
|
365
|
variants: [variant], |
|
366
|
} |
|
367
|
|
|
368
|
const priceKey = await service.computeKey(price) |
|
369
|
const variantKey = await service.computeKey(variant) |
|
370
|
const productKey = await service.computeKey(product) |
|
371
|
|
|
372
|
await service.set({ |
|
373
|
key: priceKey, |
|
374
|
data: price, |
|
375
|
}) |
|
376
|
await service.set({ |
|
377
|
key: variantKey, |
|
378
|
data: variant, |
|
379
|
}) |
|
380
|
await service.set({ |
|
381
|
key: productKey, |
|
382
|
data: product, |
|
383
|
}) |
|
384
|
|
|
385
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
386
|
|
|
387
|
await mockEventBus.emit( |
|
388
|
[ |
|
389
|
{ |
|
390
|
name: "price.updated", |
|
391
|
data: { id: "price_1", variant_id: "var_1", amount: 1100 }, |
|
392
|
}, |
|
393
|
], |
|
394
|
{} |
|
395
|
) |
|
396
|
|
|
397
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
398
|
|
|
399
|
const cachedPrice = await service.get({ key: priceKey }) |
|
400
|
const cachedVariant = await service.get({ key: variantKey }) |
|
401
|
const cachedProduct = await service.get({ key: productKey }) |
|
402
|
|
|
403
|
expect(cachedPrice).toBeNull() |
|
404
|
expect(cachedVariant).toBeNull() |
|
405
|
expect(cachedProduct).toBeNull() |
|
406
|
}) |
|
407
|
}) |
|
408
|
|
|
409
|
describe("Complex Query Caching", () => { |
|
410
|
it("should cache complex queries and invalidate based on entity relationships", async () => { |
|
411
|
const complexQuery = { |
|
412
|
entity: "product", |
|
413
|
filters: { status: "published", collection_id: "col_1" }, |
|
414
|
includes: { |
|
415
|
variants: { |
|
416
|
include: { |
|
417
|
prices: true, |
|
418
|
}, |
|
419
|
}, |
|
420
|
collection: true, |
|
421
|
}, |
|
422
|
pagination: { limit: 10, offset: 0 }, |
|
423
|
} |
|
424
|
|
|
425
|
const queryResult = { |
|
426
|
data: [ |
|
427
|
{ |
|
428
|
id: "prod_1", |
|
429
|
title: "Product 1", |
|
430
|
collection_id: "col_1", |
|
431
|
variants: [ |
|
432
|
{ |
|
433
|
id: "var_1", |
|
434
|
product_id: "prod_1", |
|
435
|
prices: [{ id: "price_1", amount: 1000 }], |
|
436
|
}, |
|
437
|
], |
|
438
|
collection: { id: "col_1", title: "Collection 1" }, |
|
439
|
}, |
|
440
|
], |
|
441
|
pagination: { total: 1, limit: 10, offset: 0 }, |
|
442
|
} |
|
443
|
|
|
444
|
const queryKey = await service.computeKey(complexQuery) |
|
445
|
|
|
446
|
await service.set({ |
|
447
|
key: queryKey, |
|
448
|
data: queryResult, |
|
449
|
}) |
|
450
|
|
|
451
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
452
|
|
|
453
|
const cached = await service.get({ key: queryKey }) |
|
454
|
expect(cached).toEqual(queryResult) |
|
455
|
|
|
456
|
await mockEventBus.emit( |
|
457
|
[ |
|
458
|
{ |
|
459
|
name: "price.updated", |
|
460
|
data: { id: "price_1", amount: 1100 }, |
|
461
|
}, |
|
462
|
], |
|
463
|
{} |
|
464
|
) |
|
465
|
|
|
466
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
467
|
|
|
468
|
const cachedAfterUpdate = await service.get({ key: queryKey }) |
|
469
|
expect(cachedAfterUpdate).toBeNull() |
|
470
|
}) |
|
471
|
}) |
|
472
|
|
|
473
|
it("should cache complex queries and return the correct cached data", async () => { |
|
474
|
const complexQuery = { |
|
475
|
entity: "product", |
|
476
|
filters: { status: "published", collection_id: "col_1" }, |
|
477
|
includes: { |
|
478
|
variants: { |
|
479
|
include: { |
|
480
|
prices: true, |
|
481
|
}, |
|
482
|
}, |
|
483
|
collection: true, |
|
484
|
}, |
|
485
|
pagination: { limit: 10, offset: 0 }, |
|
486
|
} |
|
487
|
|
|
488
|
const queryResult = { |
|
489
|
data: [ |
|
490
|
{ |
|
491
|
id: "prod_1", |
|
492
|
title: "Product 1", |
|
493
|
collection_id: "col_1", |
|
494
|
variants: [ |
|
495
|
{ |
|
496
|
id: "var_1", |
|
497
|
product_id: "prod_1", |
|
498
|
prices: [{ id: "price_1", amount: 1000 }], |
|
499
|
}, |
|
500
|
], |
|
501
|
collection: { id: "col_1", title: "Collection 1" }, |
|
502
|
}, |
|
503
|
], |
|
504
|
pagination: { total: 1, limit: 10, offset: 0 }, |
|
505
|
} |
|
506
|
|
|
507
|
const queryKey = await service.computeKey(complexQuery) |
|
508
|
|
|
509
|
await service.set({ |
|
510
|
key: queryKey, |
|
511
|
data: queryResult, |
|
512
|
}) |
|
513
|
|
|
514
|
await setTimeout(DEFAULT_WAIT_INTERVAL) |
|
515
|
|
|
516
|
const cached = await service.get({ key: queryKey }) |
|
517
|
expect(cached).toEqual(queryResult) |
|
518
|
|
|
519
|
const complexQueryOffset = { |
|
520
|
entity: "product", |
|
521
|
filters: { status: "published", collection_id: "col_1" }, |
|
522
|
includes: { |
|
523
|
variants: { |
|
524
|
include: { |
|
525
|
prices: true, |
|
526
|
}, |
|
527
|
}, |
|
528
|
collection: true, |
|
529
|
}, |
|
530
|
pagination: { limit: 10, offset: 10 }, |
|
531
|
} |
|
532
|
|
|
533
|
const queryKeyOffset = await service.computeKey(complexQueryOffset) |
|
534
|
|
|
535
|
const cachedOffset = await service.get({ key: queryKeyOffset }) |
|
536
|
expect(cachedOffset).toEqual(null) |
|
537
|
}) |
|
538
|
}) |
|
539
|
}, |
|
540
|
}) |