feat(medusa): Cache modules (#3187)

This commit is contained in:
Frane Polić authored and GitHub committed 2023-03-10 15:09:26 +01:00
1 parent f43f03badb
commit f97b3d7cce
42 files changed
+783 -186

No files matched your search

@@ -1,4 +1,4 @@
export const ShopifyRedisServiceMock = {
export const ShopifyCacheServiceMock = {
addIgnore: jest.fn().mockImplementation((_id, _event) => {
return Promise.resolve()
}),
@@ -4,7 +4,7 @@ import { ProductServiceMock } from "../__mocks__/product-service"
import { ProductVariantServiceMock } from "../__mocks__/product-variant"
import { ShippingProfileServiceMock } from "../__mocks__/shipping-profile"
import { ShopifyClientServiceMock } from "../__mocks__/shopify-client"
import { ShopifyRedisServiceMock } from "../__mocks__/shopify-redis"
import { ShopifyCacheServiceMock } from "../__mocks__/shopify-cache"
import { medusaProducts, shopifyProducts } from "../__mocks__/test-products"
describe("ShopifyProductService", () => {
@@ -32,7 +32,7 @@ describe("ShopifyProductService", () => {
manager: MockManager,
shopifyClientService: ShopifyClientServiceMock,
productService: ProductServiceMock,
shopifyRedisService: ShopifyRedisServiceMock,
shopifyCacheService: ShopifyCacheServiceMock,
shippingProfileService: ShippingProfileServiceMock,
productVariantService: ProductVariantServiceMock,
})
@@ -46,8 +46,8 @@ describe("ShopifyProductService", () => {
const product = await shopifyProductService.create(data)
expect(ShopifyRedisServiceMock.shouldIgnore).toHaveBeenCalledTimes(1)
expect(ShopifyRedisServiceMock.addIgnore).toHaveBeenCalledTimes(1)
expect(ShopifyCacheServiceMock.shouldIgnore).toHaveBeenCalledTimes(1)
expect(ShopifyCacheServiceMock.addIgnore).toHaveBeenCalledTimes(1)
expect(ShippingProfileServiceMock.retrieveDefault).toHaveBeenCalledTimes(
1
)
@@ -66,7 +66,7 @@ describe("ShopifyProductService", () => {
manager: MockManager,
shopifyClientService: ShopifyClientServiceMock,
productService: ProductServiceMock,
shopifyRedisService: ShopifyRedisServiceMock,
shopifyCacheService: ShopifyCacheServiceMock,
shippingProfileService: ShippingProfileServiceMock,
productVariantService: ProductVariantServiceMock,
})
@@ -1,41 +1,40 @@
// shopify-redis
import { BaseService } from "medusa-interfaces"
import { IGNORE_THRESHOLD } from "../utils/const"
class shopifyRedisService extends BaseService {
constructor({ redisClient }, options) {
class ShopifyCacheService extends BaseService {
constructor({ cacheService }, options) {
super()
this.options_ = options
/** @private @const {RedisClient} */
this.redis_ = redisClient
/** @private @const {ICacheService} */
this.cacheService_ = cacheService
}
async addIgnore(id, side) {
const key = `sh_${id}_ignore_${side}`
return await this.redis_.set(
return await this.cacheService_.set(
key,
1,
"EX",
this.options_.ignore_threshold || IGNORE_THRESHOLD
)
}
async shouldIgnore(id, action) {
const key = `sh_${id}_ignore_${action}`
return await this.redis_.get(key)
return await this.cacheService_.get(key)
}
async addUniqueValue(uniqueVal, type) {
const key = `sh_${uniqueVal}_${type}`
return await this.redis_.set(key, 1, "EX", 60 * 5)
return await this.cacheService_.set(key, 1, 60 * 5)
}
async getUniqueValue(uniqueVal, type) {
const key = `sh_${uniqueVal}_${type}`
return await this.redis_.get(key)
return await this.cacheService_.get(key)
}
}
export default shopifyRedisService
export default ShopifyCacheService
@@ -1,5 +1,6 @@
import { DataType } from "@shopify/shopify-api"
import { BaseService } from "medusa-interfaces"
import { createClient } from "../utils/create-client"
import { pager } from "../utils/pager"
@@ -14,7 +14,7 @@ class ShopifyProductService extends BaseService {
productVariantService,
shippingProfileService,
shopifyClientService,
shopifyRedisService,
shopifyCacheService,
},
options
) {
@@ -32,8 +32,8 @@ class ShopifyProductService extends BaseService {
this.shippingProfileService_ = shippingProfileService
/** @private @const {ShopifyRestClient} */
this.shopify_ = shopifyClientService
this.redis_ = shopifyRedisService
/** @private @const {ICacheService} */
this.cacheService_ = shopifyCacheService
}
withTransaction(transactionManager) {
@@ -41,15 +41,17 @@ class ShopifyProductService extends BaseService {
return this
}
const cloned = new ShopifyProductService({
manager: transactionManager,
options: this.options,
shippingProfileService: this.shippingProfileService_,
productVariantService: this.productVariantService_,
productService: this.productService_,
shopifyClientService: this.shopify_,
shopifyRedisService: this.redis_,
})
const cloned = new ShopifyProductService(
{
manager: transactionManager,
shippingProfileService: this.shippingProfileService_,
productVariantService: this.productVariantService_,
productService: this.productService_,
shopifyClientService: this.shopify_,
shopifyCacheService: this.cacheService_,
},
this.options
)
cloned.transactionManager_ = transactionManager
@@ -65,7 +67,10 @@ class ShopifyProductService extends BaseService {
*/
async create(data) {
return this.atomicPhase_(async (manager) => {
const ignore = await this.redis_.shouldIgnore(data.id, "product.created")
const ignore = await this.cacheService_.shouldIgnore(
data.id,
"product.created"
)
if (ignore) {
return
}
@@ -106,7 +111,7 @@ class ShopifyProductService extends BaseService {
}
}
await this.redis_.addIgnore(data.id, "product.created")
await this.cacheService_.addIgnore(data.id, "product.created")
return product
})
@@ -114,7 +119,7 @@ class ShopifyProductService extends BaseService {
async update(existing, shopifyUpdate) {
return this.atomicPhase_(async (manager) => {
const ignore = await this.redis_.shouldIgnore(
const ignore = await this.cacheService_.shouldIgnore(
shopifyUpdate.id,
"product.updated"
)
@@ -140,7 +145,7 @@ class ShopifyProductService extends BaseService {
}
if (!isEmpty(update)) {
await this.redis_.addIgnore(shopifyUpdate.id, "product.updated")
await this.cacheService_.addIgnore(shopifyUpdate.id, "product.updated")
return await this.productService_
.withTransaction(manager)
.update(existing.id, update)
@@ -219,7 +224,7 @@ class ShopifyProductService extends BaseService {
)
})
await this.redis_.addIgnore(product.external_id, "product.updated")
await this.cacheService_.addIgnore(product.external_id, "product.updated")
}
async shopifyVariantUpdate(id, fields) {
@@ -273,7 +278,7 @@ class ShopifyProductService extends BaseService {
)
})
await this.redis_.addIgnore(
await this.cacheService_.addIgnore(
variant.metadata.sh_id,
"product-variant.updated"
)
@@ -298,7 +303,10 @@ class ShopifyProductService extends BaseService {
)
})
await this.redis_.addIgnore(metadata.sh_id, "product-variant.deleted")
await this.cacheService_.addIgnore(
metadata.sh_id,
"product-variant.deleted"
)
}
async updateCollectionId(productId, collectionId) {
@@ -314,11 +322,11 @@ class ShopifyProductService extends BaseService {
const { id, variants, options } = product
for (let variant of updateVariants) {
const ignore =
(await this.redis_.shouldIgnore(
(await this.cacheService_.shouldIgnore(
variant.metadata.sh_id,
"product-variant.updated"
)) ||
(await this.redis_.shouldIgnore(
(await this.cacheService_.shouldIgnore(
variant.metadata.sh_id,
"product-variant.created"
))
@@ -349,7 +357,7 @@ class ShopifyProductService extends BaseService {
return this.atomicPhase_(async (manager) => {
const { variants } = product
for (const variant of variants) {
const ignore = await this.redis_.shouldIgnore(
const ignore = await this.cacheService_.shouldIgnore(
variant.metadata.sh_id,
"product-variant.deleted"
)
@@ -551,15 +559,15 @@ class ShopifyProductService extends BaseService {
async testUnique_(uniqueVal, type) {
// Test if the unique value has already been added, if it was then pass the value onto the duplicate handler and return the new value
const exists = await this.redis_.getUniqueValue(uniqueVal, type)
const exists = await this.cacheService_.getUniqueValue(uniqueVal, type)
if (exists) {
const dupValue = this.handleDuplicateConstraint_(uniqueVal)
await this.redis_.addUniqueValue(dupValue, type)
await this.cacheService_.addUniqueValue(dupValue, type)
return dupValue
}
// If it doesn't exist, we return the value
await this.redis_.addUniqueValue(uniqueVal, type)
await this.cacheService_.addUniqueValue(uniqueVal, type)
return uniqueVal
}