Feat(): distributed caching (#13435)

RESOLVES CORE-1153

**What**
- This pr mainly lay the foundation the caching layer. It comes with a modules (built in memory cache) and a redis provider.
- Apply caching to few touch point to test

Co-authored-by: Carlos R. L. Rodrigues <37986729+carlos-r-l-rodrigues@users.noreply.github.com>
This commit is contained in:
Adrien de Peretti
2025-09-30 16:19:06 +00:00
committed by GitHub
co-authored by Carlos R. L. Rodrigues
parent 5b135a41fe
commit b9d6f73320
117 changed files with 5741 additions and 530 deletions
+12
View File
@@ -0,0 +1,12 @@
import { Module, Modules } from "@medusajs/framework/utils"
import { default as loadHash } from "./loaders/hash"
import { default as loadProviders } from "./loaders/providers"
import CachingModuleService from "./services/cache-module"
export default Module(Modules.CACHING, {
service: CachingModuleService,
loaders: [loadHash, loadProviders],
})
// Module options types
export { CachingModuleOptions } from "./types"
@@ -0,0 +1,8 @@
import { asValue } from "awilix"
export default async ({ container }) => {
const xxhashhWasm = await import("xxhash-wasm")
const { h32ToString } = await xxhashhWasm.default()
container.register("hasher", asValue(h32ToString))
}
@@ -0,0 +1,94 @@
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk"
import { LoaderOptions, ModulesSdkTypes } from "@medusajs/framework/types"
import {
ContainerRegistrationKeys,
getProviderRegistrationKey,
} from "@medusajs/framework/utils"
import { CachingProviderService } from "@services"
import {
CachingDefaultProvider,
CachingIdentifiersRegistrationName,
CachingModuleOptions,
CachingProviderRegistrationPrefix,
} from "@types"
import { aliasTo, asFunction, asValue, Lifetime } from "awilix"
import { MemoryCachingProvider } from "../providers/memory-cache"
import { DefaultCacheStrategy } from "../utils/strategy"
const registrationFn = async (klass, container, { id }) => {
const key = CachingProviderService.getRegistrationIdentifier(klass)
if (!id) {
throw new Error(`No "id" provided for provider ${key}`)
}
const regKey = getProviderRegistrationKey({
providerId: id,
providerIdentifier: key,
})
container.register({
[CachingProviderRegistrationPrefix + id]: aliasTo(regKey),
})
container.registerAdd(CachingIdentifiersRegistrationName, asValue(key))
}
export default async ({
container,
options,
}: LoaderOptions<
(
| ModulesSdkTypes.ModuleServiceInitializeOptions
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
) &
CachingModuleOptions
>): Promise<void> => {
container.registerAdd(CachingIdentifiersRegistrationName, asValue(undefined))
const strategy = DefaultCacheStrategy // Re enable custom strategy another time
container.register("strategy", asValue(strategy))
// MemoryCachingProvider - default provider
container.register({
[CachingProviderRegistrationPrefix + MemoryCachingProvider.identifier]:
asFunction(() => new MemoryCachingProvider(), {
lifetime: Lifetime.SINGLETON,
}),
})
container.registerAdd(
CachingIdentifiersRegistrationName,
asValue(MemoryCachingProvider.identifier)
)
container.register(
CachingDefaultProvider,
asValue(MemoryCachingProvider.identifier)
)
// Load other providers
await moduleProviderLoader({
container,
providers: options?.providers || [],
registerServiceFn: registrationFn,
})
const isSingleProvider = options?.providers?.length === 1
let hasDefaultProvider = false
for (const provider of options?.providers || []) {
if (provider.is_default || isSingleProvider) {
if (provider.is_default) {
hasDefaultProvider = true
}
container.register(CachingDefaultProvider, asValue(provider.id))
}
}
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
if (!hasDefaultProvider) {
logger.warn(
`[caching-module]: No default caching provider defined. Using "${container.resolve(
CachingDefaultProvider
)}" as default.`
)
}
}
@@ -0,0 +1,228 @@
import NodeCache from "node-cache"
import type { ICachingProviderService } from "@medusajs/framework/types"
export interface MemoryCacheModuleOptions {
/**
* TTL in seconds
*/
ttl?: number
/**
* Maximum number of keys to store (see node-cache documentation)
*/
maxKeys?: number
/**
* Check period for expired keys in seconds (see node-cache documentation)
*/
checkPeriod?: number
/**
* Use clones for cached data (see node-cache documentation)
*/
useClones?: boolean
}
export class MemoryCachingProvider implements ICachingProviderService {
static identifier = "cache-memory"
protected cacheClient: NodeCache
protected tagIndex: Map<string, Set<string>> = new Map() // tag -> keys
protected keyTags: Map<string, Set<string>> = new Map() // key -> tags
protected entryOptions: Map<string, { autoInvalidate?: boolean }> = new Map() // key -> options
protected options: MemoryCacheModuleOptions
constructor() {
this.options = {
ttl: 3600,
maxKeys: 25000,
checkPeriod: 60, // 10 minutes
useClones: false, // Default to false for speed, true would be slower but safer. we can discuss
}
const cacheClient = new NodeCache({
stdTTL: this.options.ttl,
maxKeys: this.options.maxKeys,
checkperiod: this.options.checkPeriod,
useClones: this.options.useClones,
})
this.cacheClient = cacheClient
// Clean up tag indices when keys expire
this.cacheClient.on("expired", (key: string, value: any) => {
this.cleanupTagReferences(key)
})
this.cacheClient.on("del", (key: string, value: any) => {
this.cleanupTagReferences(key)
})
}
private cleanupTagReferences(key: string): void {
const tags = this.keyTags.get(key)
if (tags) {
tags.forEach((tag) => {
const keysForTag = this.tagIndex.get(tag)
if (keysForTag) {
keysForTag.delete(key)
if (keysForTag.size === 0) {
this.tagIndex.delete(tag)
}
}
})
this.keyTags.delete(key)
}
// Also clean up entry options
this.entryOptions.delete(key)
}
async get({ key, tags }: { key?: string; tags?: string[] }): Promise<any> {
if (key) {
return this.cacheClient.get(key) ?? null
}
if (tags && tags.length) {
const allKeys = new Set<string>()
tags.forEach((tag) => {
const keysForTag = this.tagIndex.get(tag)
if (keysForTag) {
keysForTag.forEach((key) => allKeys.add(key))
}
})
if (allKeys.size === 0) {
return []
}
const results: any[] = []
allKeys.forEach((key) => {
const value = this.cacheClient.get(key)
if (value !== undefined) {
results.push(value)
}
})
return results
}
return null
}
async set({
key,
data,
ttl,
tags,
options,
}: {
key: string
data: object
ttl?: number
tags?: string[]
options?: {
autoInvalidate?: boolean
}
}): Promise<void> {
// Set the cache entry
const effectiveTTL = ttl ?? this.options.ttl ?? 3600
this.cacheClient.set(key, data, effectiveTTL)
// Handle tags if provided
if (tags && tags.length) {
// Clean up any existing tag references for this key
this.cleanupTagReferences(key)
const tagSet = new Set(tags)
this.keyTags.set(key, tagSet)
// Add this key to each tag's index
tags.forEach((tag) => {
if (!this.tagIndex.has(tag)) {
this.tagIndex.set(tag, new Set())
}
this.tagIndex.get(tag)!.add(key)
})
}
// Store entry options if provided
if (
Object.keys(options ?? {}).length &&
!Object.values(options ?? {}).every((value) => value === undefined)
) {
this.entryOptions.set(key, options!)
}
}
async clear({
key,
tags,
options,
}: {
key?: string
tags?: string[]
options?: {
autoInvalidate?: boolean
}
}): Promise<void> {
if (key) {
this.cacheClient.del(key)
return
}
if (tags && tags.length) {
// Handle wildcard tag to clear all cache data
if (tags.includes("*")) {
this.cacheClient.flushAll()
this.tagIndex.clear()
this.keyTags.clear()
this.entryOptions.clear()
return
}
const allKeys = new Set<string>()
tags.forEach((tag) => {
const keysForTag = this.tagIndex.get(tag)
if (keysForTag) {
keysForTag.forEach((key) => allKeys.add(key))
}
})
if (allKeys.size) {
// If no options provided (user explicit call), clear everything
if (!options) {
const keysToDelete = Array.from(allKeys)
this.cacheClient.del(keysToDelete)
// Clean up ALL tag references for deleted keys
keysToDelete.forEach((key) => {
this.cleanupTagReferences(key)
})
return
}
// If autoInvalidate is true (strategy call), only clear entries with autoInvalidate=true (default)
if (options.autoInvalidate === true) {
const keysToDelete: string[] = []
allKeys.forEach((key) => {
const entryOptions = this.entryOptions.get(key)
// Delete if entry has autoInvalidate=true or no setting (default true)
const shouldAutoInvalidate = entryOptions?.autoInvalidate ?? true
if (shouldAutoInvalidate) {
keysToDelete.push(key)
}
})
if (keysToDelete.length) {
this.cacheClient.del(keysToDelete)
// Clean up ALL tag references for deleted keys
keysToDelete.forEach((key) => {
this.cleanupTagReferences(key)
})
}
}
}
}
}
}
@@ -0,0 +1,406 @@
import { MedusaModule } from "@medusajs/framework/modules-sdk"
import type {
ICachingModuleService,
ICachingStrategy,
Logger,
} from "@medusajs/framework/types"
import { GraphQLUtils, MedusaError } from "@medusajs/framework/utils"
import { CachingDefaultProvider, InjectedDependencies } from "@types"
import CacheProviderService from "./cache-provider"
const ONE_HOUR_IN_SECOND = 60 * 60
export default class CachingModuleService implements ICachingModuleService {
protected container: InjectedDependencies
protected providerService: CacheProviderService
protected strategyCtr: new (...args: any[]) => ICachingStrategy
protected strategy: ICachingStrategy
protected defaultProviderId: string
protected logger: Logger
protected ongoingRequests: Map<string, Promise<any>> = new Map()
protected ttl: number
static traceGet?: (
cacheGetFn: () => Promise<any>,
key: string,
tags: string[]
) => Promise<any>
static traceSet?: (
cacheSetFn: () => Promise<any>,
key: string,
tags: string[],
options: { autoInvalidate?: boolean }
) => Promise<any>
static traceClear?: (
cacheClearFn: () => Promise<any>,
key: string,
tags: string[],
options: { autoInvalidate?: boolean }
) => Promise<any>
constructor(
container: InjectedDependencies,
protected readonly moduleDeclaration:
| { options: { ttl?: number } }
| { ttl?: number }
) {
this.container = container
this.providerService = container.cacheProviderService
this.defaultProviderId = container[CachingDefaultProvider]
this.strategyCtr = container.strategy as new (
...args: any[]
) => ICachingStrategy
this.strategy = new this.strategyCtr(this.container, this)
const moduleOptions =
"options" in moduleDeclaration
? moduleDeclaration.options
: moduleDeclaration
this.ttl = moduleOptions.ttl ?? ONE_HOUR_IN_SECOND
this.logger = container.logger ?? (console as unknown as Logger)
}
__hooks = {
onApplicationStart: async () => {
this.onApplicationStart()
},
onApplicationShutdown: async () => {
this.onApplicationShutdown()
},
onApplicationPrepareShutdown: async () => {
this.onApplicationPrepareShutdown()
},
}
protected onApplicationStart() {
const loadedSchema = MedusaModule.getAllJoinerConfigs()
.map((joinerConfig) => joinerConfig?.schema ?? "")
.join("\n")
const defaultMedusaSchema = `
scalar DateTime
scalar JSON
directive @enumValue(value: String) on ENUM_VALUE
`
const { schema: cleanedSchema } = GraphQLUtils.cleanGraphQLSchema(
defaultMedusaSchema + loadedSchema
)
const mergedSchema = GraphQLUtils.mergeTypeDefs(cleanedSchema)
const schema = GraphQLUtils.makeExecutableSchema({
typeDefs: mergedSchema,
})
this.strategy.onApplicationStart?.(
schema,
MedusaModule.getAllJoinerConfigs()
)
}
protected onApplicationShutdown() {
this.strategy.onApplicationShutdown?.()
}
protected onApplicationPrepareShutdown() {
this.strategy.onApplicationPrepareShutdown?.()
}
protected static normalizeProviders(
providers: string[] | { id: string; ttl?: number }[]
): { id: string; ttl?: number }[] {
const providers_ = Array.isArray(providers) ? providers : [providers]
return providers_.map((provider) => {
return typeof provider === "string" ? { id: provider } : provider
})
}
protected getRequestKey(
key?: string,
tags?: string[],
providers?: string[]
): string {
const keyPart = key || ""
const tagsPart = tags?.sort().join(",") || ""
const providersPart = providers?.join(",") || this.defaultProviderId
return `${keyPart}|${tagsPart}|${providersPart}`
}
protected getClearRequestKey(
key?: string,
tags?: string[],
providers?: string[]
): string {
const keyPart = key || ""
const tagsPart = tags?.sort().join(",") || ""
const providersPart = providers?.join(",") || this.defaultProviderId
return `clear:${keyPart}|${tagsPart}|${providersPart}`
}
async get(options: { key?: string; tags?: string[]; providers?: string[] }) {
if (CachingModuleService.traceGet) {
return await CachingModuleService.traceGet(
() => this.get_(options),
options.key ?? "",
options.tags ?? []
)
}
return await this.get_(options)
}
private async get_({
key,
tags,
providers,
}: {
key?: string
tags?: string[]
providers?: string[]
}) {
if (!key && !tags) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Either key or tags must be provided"
)
}
const requestKey = this.getRequestKey(key, tags, providers)
const existingRequest = this.ongoingRequests.get(requestKey)
if (existingRequest) {
return await existingRequest
}
const requestPromise = this.performCacheGet(key, tags, providers)
this.ongoingRequests.set(requestKey, requestPromise)
try {
const result = await requestPromise
return result
} finally {
// Clean up the completed request
this.ongoingRequests.delete(requestKey)
}
}
protected async performCacheGet(
key?: string,
tags?: string[],
providers?: string[]
): Promise<any> {
const providersToCheck = providers ?? [this.defaultProviderId]
for (const providerId of providersToCheck) {
try {
const provider_ = this.providerService.retrieveProvider(providerId)
const result = await provider_.get({ key, tags })
if (result != null) {
return result
}
} catch (error) {
this.logger.warn(
`Cache provider ${providerId} failed: ${error.message}\n${error.stack}`
)
continue
}
}
return null
}
async set(options: {
key: string
data: object
ttl?: number
tags?: string[]
providers?: string[]
options?: { autoInvalidate?: boolean }
}) {
if (CachingModuleService.traceSet) {
return await CachingModuleService.traceSet(
() => this.set_(options),
options.key,
options.tags ?? [],
options.options ?? {}
)
}
return await this.set_(options)
}
private async set_({
key,
data,
ttl,
tags,
providers,
options,
}: {
key: string
data: object
tags?: string[]
ttl?: number
providers?: string[] | { id: string; ttl?: number }[]
options?: {
autoInvalidate?: boolean
}
}) {
if (!key) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"[CachingModuleService] Key must be provided"
)
}
const key_ = key
const tags_ = tags ?? (await this.strategy.computeTags(data))
let providers_: string[] | { id: string; ttl?: number }[] = [
this.defaultProviderId,
]
providers_ = CachingModuleService.normalizeProviders(
providers ?? providers_
)
const providerIds = providers_.map((p) => p.id)
const requestKey = this.getRequestKey(key_, tags_, providerIds)
const existingRequest = this.ongoingRequests.get(requestKey)
if (existingRequest) {
return await existingRequest
}
const requestPromise = this.performCacheSet(
key_,
tags_,
data,
ttl,
providers_,
options
)
this.ongoingRequests.set(requestKey, requestPromise)
try {
await requestPromise
} finally {
// Clean up the completed request
this.ongoingRequests.delete(requestKey)
}
}
protected async performCacheSet(
key: string,
tags: string[],
data: object,
ttl?: number,
providers?: { id: string; ttl?: number }[],
options?: {
autoInvalidate?: boolean
}
): Promise<void> {
for (const providerOptions of providers || []) {
const ttl_ = providerOptions.ttl ?? ttl ?? this.ttl
const provider = this.providerService.retrieveProvider(providerOptions.id)
void provider.set({
key,
tags,
data,
ttl: ttl_,
options,
})
}
}
async clear(options: {
key?: string
tags?: string[]
options?: { autoInvalidate?: boolean }
providers?: string[]
}) {
if (CachingModuleService.traceClear) {
return await CachingModuleService.traceClear(
() => this.clear_(options),
options.key ?? "",
options.tags ?? [],
options.options ?? {}
)
}
return await this.clear_(options)
}
private async clear_({
key,
tags,
options,
providers,
}: {
key?: string
tags?: string[]
options?: {
autoInvalidate?: boolean
}
providers?: string[]
}) {
if (!key && !tags) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"Either key or tags must be provided"
)
}
const requestKey = this.getClearRequestKey(key, tags, providers)
const existingRequest = this.ongoingRequests.get(requestKey)
if (existingRequest) {
return await existingRequest
}
const requestPromise = this.performCacheClear(key, tags, options, providers)
this.ongoingRequests.set(requestKey, requestPromise)
try {
await requestPromise
} finally {
// Clean up the completed request
this.ongoingRequests.delete(requestKey)
}
}
protected async performCacheClear(
key?: string,
tags?: string[],
options?: {
autoInvalidate?: boolean
},
providers?: string[]
): Promise<void> {
let providerIds_: string[] = [this.defaultProviderId]
if (providers) {
providerIds_ = Array.isArray(providers) ? providers : [providers]
}
for (const providerId of providerIds_) {
const provider = this.providerService.retrieveProvider(providerId)
void provider.clear({ key, tags, options })
}
}
async computeKey(input: object): Promise<string> {
return await this.strategy.computeKey(input)
}
async computeTags(
input: object,
options?: Record<string, any>
): Promise<string[]> {
return await this.strategy.computeTags(input, options)
}
}
@@ -0,0 +1,60 @@
import {
Constructor,
ICachingProviderService,
Logger,
} from "@medusajs/framework/types"
import { MedusaError } from "@medusajs/framework/utils"
import { CachingProviderRegistrationPrefix } from "../types"
type InjectedDependencies = {
[key: `cp_${string}`]: ICachingProviderService
logger?: Logger
}
export default class CacheProviderService {
#container: InjectedDependencies
#logger: Logger
constructor(container: InjectedDependencies) {
this.#container = container
this.#logger = container["logger"]
? container.logger
: (console as unknown as Logger)
}
static getRegistrationIdentifier(
providerClass: Constructor<ICachingProviderService>
) {
if (!(providerClass as any).identifier) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
`Trying to register a caching provider without an identifier.`
)
}
return `${(providerClass as any).identifier}`
}
public retrieveProvider(providerId: string): ICachingProviderService {
try {
return this.#container[
`${CachingProviderRegistrationPrefix}${providerId}`
]
} catch (err) {
if (err.name === "AwilixResolutionError") {
const errMessage = `
Unable to retrieve the caching provider with id: ${providerId}
Please make sure that the provider is registered in the container and it is configured correctly in your project configuration file.`
// Log full error for debugging
this.#logger.error(`AwilixResolutionError: ${err.message}`, err)
throw new Error(errMessage)
}
const errMessage = `Unable to retrieve the caching provider with id: ${providerId}, the following error occurred: ${err.message}`
this.#logger.error(errMessage)
throw new Error(errMessage)
}
}
}
@@ -0,0 +1,2 @@
export { default as CachingModuleService } from "./cache-module"
export { default as CachingProviderService } from "./cache-provider"
@@ -0,0 +1,56 @@
import type {
Constructor,
ICachingStrategy,
IEventBusModuleService,
Logger,
ModuleProviderExports,
ModuleServiceInitializeOptions,
} from "@medusajs/framework/types"
import { Modules } from "@medusajs/framework/utils"
import { default as CacheProviderService } from "../services/cache-provider"
export const CachingDefaultProvider = "default_provider"
export const CachingIdentifiersRegistrationName = "caching_providers_identifier"
export const CachingProviderRegistrationPrefix = "lp_"
export type InjectedDependencies = {
cacheProviderService: CacheProviderService
hasher: (data: string) => string
logger?: Logger
strategy: Constructor<ICachingStrategy>
[CachingDefaultProvider]: string
[Modules.EVENT_BUS]: IEventBusModuleService
}
export type CachingModuleOptions = Partial<ModuleServiceInitializeOptions> & {
/**
* The strategy to be used. Default to the inbuilt default strategy.
*/
// strategy?: ICachingStrategy
/**
* Time to keep data in cache (in seconds)
*/
ttl?: number
/**
* Providers to be registered
*/
providers?: {
/**
* The module provider to be registered
*/
resolve: string | ModuleProviderExports
/**
* If the provider is the default
*/
is_default?: boolean
/**
* The id of the provider
*/
id: string
/**
* key value pair of the configuration to be passed to the provider constructor
*/
options?: Record<string, unknown>
}[]
}
@@ -0,0 +1,487 @@
import { GraphQLSchema, buildSchema } from "graphql"
import { CacheInvalidationParser, EntityReference } from "../parser"
describe("CacheInvalidationParser", () => {
let parser: CacheInvalidationParser
let schema: GraphQLSchema
beforeEach(() => {
const schemaDefinition = `
type Product {
id: ID!
title: String
description: String
collection: ProductCollection
categories: [ProductCategory!]
variants: [ProductVariant!]
created_at: String
updated_at: String
}
type ProductCollection {
id: ID!
title: String
products: [Product!]
created_at: String
updated_at: String
}
type ProductCategory {
id: ID!
name: String
products: [Product!]
parent: ProductCategory
children: [ProductCategory!]
created_at: String
updated_at: String
}
type ProductVariant {
id: ID!
title: String
sku: String
product: Product!
prices: [Price!]
created_at: String
updated_at: String
}
type Price {
id: ID!
amount: Int
currency_code: String
variant: ProductVariant!
created_at: String
updated_at: String
}
type Order {
id: ID!
status: String
items: [OrderItem!]
customer: Customer
created_at: String
updated_at: String
}
type OrderItem {
id: ID!
quantity: Int
order: Order!
variant: ProductVariant!
created_at: String
updated_at: String
}
type Customer {
id: ID!
first_name: String
last_name: String
email: String
orders: [Order!]
created_at: String
updated_at: String
}
`
schema = buildSchema(schemaDefinition)
parser = new CacheInvalidationParser(schema, [
// Partially populate this record ro force the test to match from both id prefix or type
// detection
{
idPrefixToEntityName: {
prod: "Product",
col: "ProductCollection",
cat: "ProductCategory",
},
},
])
})
describe("parseObjectForEntities", () => {
it("should identify a simple product entity", () => {
const product = {
id: "prod_123",
title: "Test Product",
description: "A test product",
}
const entities = parser.parseObjectForEntities(product)
expect(entities).toHaveLength(1)
expect(entities[0]).toEqual({
type: "Product",
id: "prod_123",
isInArray: false,
})
})
it("should identify nested entities in a product with collection", () => {
const product = {
id: "prod_123",
title: "Test Product",
collection: {
id: "col_456",
title: "Test Collection",
},
}
const entities = parser.parseObjectForEntities(product)
expect(entities).toHaveLength(2)
expect(entities).toContainEqual({
type: "Product",
id: "prod_123",
isInArray: false,
})
expect(entities).toContainEqual({
type: "ProductCollection",
id: "col_456",
isInArray: false,
})
})
it("should identify entities in arrays", () => {
const product = {
id: "prod_123",
title: "Test Product",
variants: [
{
id: "var_789",
title: "Variant 1",
sku: "SKU-001",
},
{
id: "var_790",
title: "Variant 2",
sku: "SKU-002",
},
],
}
const entities = parser.parseObjectForEntities(product)
expect(entities).toHaveLength(3)
expect(entities).toContainEqual({
type: "Product",
id: "prod_123",
isInArray: false,
})
expect(entities).toContainEqual({
type: "ProductVariant",
id: "var_789",
isInArray: true,
})
expect(entities).toContainEqual({
type: "ProductVariant",
id: "var_790",
isInArray: true,
})
})
it("should handle deeply nested entities", () => {
const order = {
id: "order_123",
status: "completed",
items: [
{
id: "item_456",
quantity: 2,
variant: {
id: "var_789",
title: "Variant 1",
product: {
id: "prod_123",
title: "Test Product",
collection: {
id: "col_456",
title: "Test Collection",
},
},
},
},
],
customer: {
id: "cus_789",
email: "test@example.com",
first_name: "John",
},
}
const entities = parser.parseObjectForEntities(order)
expect(entities).toHaveLength(6)
expect(entities).toContainEqual({
type: "Order",
id: "order_123",
isInArray: false,
})
expect(entities).toContainEqual({
type: "OrderItem",
id: "item_456",
isInArray: true,
})
expect(entities).toContainEqual({
type: "ProductVariant",
id: "var_789",
isInArray: false,
})
expect(entities).toContainEqual({
type: "Product",
id: "prod_123",
isInArray: false,
})
expect(entities).toContainEqual({
type: "ProductCollection",
id: "col_456",
isInArray: false,
})
expect(entities).toContainEqual({
type: "Customer",
id: "cus_789",
isInArray: false,
})
})
it("should return empty array for null or primitive values", () => {
expect(parser.parseObjectForEntities(null)).toEqual([])
expect(parser.parseObjectForEntities(undefined)).toEqual([])
expect(parser.parseObjectForEntities("string")).toEqual([])
expect(parser.parseObjectForEntities(123)).toEqual([])
expect(parser.parseObjectForEntities(true)).toEqual([])
})
it("should ignore objects without id field", () => {
const invalidObject = {
title: "No ID Object",
description: "This object has no ID",
}
const entities = parser.parseObjectForEntities(invalidObject)
expect(entities).toEqual([])
})
it("should handle objects with partial field matches", () => {
const partialProduct = {
id: "prod_123",
title: "Test Product",
unknown_field: "Should still work",
}
const entities = parser.parseObjectForEntities(partialProduct)
expect(entities).toHaveLength(1)
expect(entities[0]).toEqual({
type: "Product",
id: "prod_123",
isInArray: false,
})
})
})
describe("buildInvalidationEvents", () => {
it("should build invalidation events for a single entity", () => {
const entities: EntityReference[] = [{ type: "Product", id: "prod_123" }]
const events = parser.buildInvalidationEvents(entities)
expect(events).toHaveLength(1)
expect(events[0]).toMatchObject({
entityType: "Product",
entityId: "prod_123",
relatedEntities: [],
})
expect(events[0].cacheKeys).toEqual(["Product:prod_123"])
})
it("should build invalidation events with related entities", () => {
const entities: EntityReference[] = [
{ type: "Product", id: "prod_123" },
{ type: "ProductCollection", id: "col_456" },
{ type: "ProductVariant", id: "var_789" },
]
const events = parser.buildInvalidationEvents(entities)
expect(events).toHaveLength(3)
const productEvent = events.find((e) => e.entityType === "Product")
expect(productEvent).toBeDefined()
expect(productEvent!.relatedEntities).toHaveLength(2)
expect(productEvent!.cacheKeys).toEqual(["Product:prod_123"])
})
it("should avoid duplicate entities in events", () => {
const entities: EntityReference[] = [
{ type: "Product", id: "prod_123" },
{ type: "Product", id: "prod_123" }, // Duplicate
{ type: "ProductCollection", id: "col_456" },
]
const events = parser.buildInvalidationEvents(entities)
expect(events).toHaveLength(2) // Should only have Product and ProductCollection events
expect(events.map((e) => e.entityType).sort()).toEqual([
"Product",
"ProductCollection",
])
})
it("should generate comprehensive cache keys", () => {
const entities: EntityReference[] = [
{ type: "Product", id: "prod_123" },
{ type: "ProductCollection", id: "col_456" },
]
const events = parser.buildInvalidationEvents(entities)
const productEvent = events.find((e) => e.entityType === "Product")!
expect(productEvent.cacheKeys).toEqual(["Product:prod_123"])
})
})
describe("integration scenarios", () => {
it("should handle a complete product updated scenario", () => {
const productData = {
id: "prod_123",
title: "Updated Product Title",
collection: {
id: "col_456",
title: "Fashion Collection",
},
categories: [
{ id: "cat_789", name: "Shirts" },
{ id: "cat_790", name: "Casual" },
],
variants: [
{
id: "var_111",
title: "Size S",
prices: [{ id: "price_222", amount: 2999, currency_code: "USD" }],
},
],
}
const entities = parser.parseObjectForEntities(productData)
const events = parser.buildInvalidationEvents(entities)
// Should identify all nested entities
expect(entities).toHaveLength(6) // Product, Collection, 2 Categories, Variant, Price
// Should created events for each entity type
expect(events).toHaveLength(6)
// Validate cache keys for each entity type
const productEvent = events.find((e) => e.entityType === "Product")!
expect(productEvent.cacheKeys).toEqual(["Product:prod_123"])
const collectionEvent = events.find(
(e) => e.entityType === "ProductCollection"
)!
expect(collectionEvent.cacheKeys).toEqual(["ProductCollection:col_456"])
const categoryEvents = events.filter(
(e) => e.entityType === "ProductCategory"
)
expect(categoryEvents).toHaveLength(2)
expect(categoryEvents[0].cacheKeys).toEqual([
"ProductCategory:cat_789",
"ProductCategory:list:*",
])
expect(categoryEvents[1].cacheKeys).toEqual([
"ProductCategory:cat_790",
"ProductCategory:list:*",
])
const variantEvent = events.find(
(e) => e.entityType === "ProductVariant"
)!
expect(variantEvent.cacheKeys).toEqual([
"ProductVariant:var_111",
"ProductVariant:list:*",
])
const priceEvent = events.find((e) => e.entityType === "Price")!
expect(priceEvent.cacheKeys).toEqual(["Price:price_222", "Price:list:*"])
})
it("should handle order with customer and items scenario", () => {
const orderData = {
id: "order_123",
status: "completed",
customer: {
id: "cus_456",
email: "customer@example.com",
},
items: [
{
id: "item_789",
quantity: 2,
variant: {
id: "var_111",
sku: "SHIRT-S-BLUE",
},
},
],
}
const entities = parser.parseObjectForEntities(orderData)
const events = parser.buildInvalidationEvents(entities)
expect(entities).toHaveLength(4) // Order, Customer, OrderItem, ProductVariant
expect(events).toHaveLength(4)
// Validate cache keys for each entity type
const orderEvent = events.find((e) => e.entityType === "Order")!
expect(orderEvent.cacheKeys).toEqual(["Order:order_123"])
const customerEvent = events.find((e) => e.entityType === "Customer")!
expect(customerEvent.cacheKeys).toEqual(["Customer:cus_456"])
const itemEvent = events.find((e) => e.entityType === "OrderItem")!
expect(itemEvent.cacheKeys).toEqual([
"OrderItem:item_789",
"OrderItem:list:*",
])
const variantEvent = events.find(
(e) => e.entityType === "ProductVariant"
)!
expect(variantEvent.cacheKeys).toEqual(["ProductVariant:var_111"])
})
it("should include simplified cache keys for created operation", () => {
const entities: EntityReference[] = [{ type: "Product", id: "prod_123" }]
const events = parser.buildInvalidationEvents(entities, "created")
const productEvent = events[0]
expect(productEvent.cacheKeys).toEqual([
"Product:prod_123",
"Product:list:*",
])
})
it("should include simplified cache keys for deleted operation", () => {
const entities: EntityReference[] = [{ type: "Product", id: "prod_123" }]
const events = parser.buildInvalidationEvents(entities, "deleted")
const productEvent = events[0]
expect(productEvent.cacheKeys).toEqual([
"Product:prod_123",
"Product:list:*",
])
})
it("should include simplified cache keys for updated operation", () => {
const entities: EntityReference[] = [{ type: "Product", id: "prod_123" }]
const events = parser.buildInvalidationEvents(entities, "updated")
const productEvent = events[0]
expect(productEvent.cacheKeys).toEqual(["Product:prod_123"])
})
})
})
@@ -0,0 +1,242 @@
import { ModuleJoinerConfig } from "@medusajs/framework/types"
import { isObject } from "@medusajs/framework/utils"
import {
GraphQLObjectType,
GraphQLSchema,
isListType,
isNonNullType,
isObjectType,
} from "graphql"
export interface EntityReference {
type: string
id: string | number
field?: string
isInArray?: boolean
}
export interface InvalidationEvent {
entityType: string
entityId: string | number
relatedEntities: EntityReference[]
cacheKeys: string[]
}
export class CacheInvalidationParser {
private typeMap: Map<string, GraphQLObjectType>
private idPrefixToEntityName: Record<string, string>
constructor(schema: GraphQLSchema, joinerConfigs: ModuleJoinerConfig[]) {
this.typeMap = new Map()
// Build type map for quick lookups
const schemaTypeMap = schema.getTypeMap()
Object.keys(schemaTypeMap).forEach((typeName) => {
const type = schemaTypeMap[typeName]
if (isObjectType(type) && !typeName.startsWith("__")) {
this.typeMap.set(typeName, type)
}
})
this.idPrefixToEntityName = joinerConfigs.reduce((acc, joinerConfig) => {
if (joinerConfig.idPrefixToEntityName) {
Object.entries(joinerConfig.idPrefixToEntityName).forEach(
([idPrefix, entityName]) => {
acc[idPrefix] = entityName
}
)
}
return acc
}, {} as Record<string, string>)
}
/**
* Parse an object to identify entities and their relationships
*/
parseObjectForEntities(
obj: any,
parentType?: string,
isInArray: boolean = false
): EntityReference[] {
const entities: EntityReference[] = []
if (!obj || typeof obj !== "object") {
return entities
}
// Check if this object matches any known GraphQL types
const detectedType = this.detectEntityType(obj, parentType)
if (detectedType && obj.id) {
entities.push({
type: detectedType,
id: obj.id,
isInArray,
})
}
// Recursively parse nested objects and arrays
Object.keys(obj).forEach((key) => {
const value = obj[key]
if (Array.isArray(value)) {
value.forEach((item) => {
entities.push(
...this.parseObjectForEntities(
item,
this.getRelationshipType(detectedType, key),
true
)
)
})
} else if (isObject(value)) {
entities.push(
...this.parseObjectForEntities(
value,
this.getRelationshipType(detectedType, key),
false
)
)
}
})
return entities
}
/**
* Detect entity type based on object structure and GraphQL type map
*/
private detectEntityType(obj: any, suggestedType?: string): string | null {
if (obj.id) {
const idParts = obj.id.split("_")
if (idParts.length > 1 && this.idPrefixToEntityName[idParts[0]]) {
return this.idPrefixToEntityName[idParts[0]]
}
}
if (suggestedType && this.typeMap.has(suggestedType)) {
const type = this.typeMap.get(suggestedType)!
if (this.objectMatchesType(obj, type)) {
return suggestedType
}
}
// Try to match against all known types
for (const [typeName, type] of this.typeMap) {
if (this.objectMatchesType(obj, type)) {
return typeName
}
}
return null
}
/**
* Check if object structure matches GraphQL type fields
*/
private objectMatchesType(obj: any, type: GraphQLObjectType): boolean {
const fields = type.getFields()
const objKeys = Object.keys(obj)
// Must have id field for entities
if (!obj.id || !fields.id) {
return false
}
// Check if at least 50% of non-null object fields match type fields
const matchingFields = objKeys.filter((key) => fields[key]).length
return matchingFields >= Math.max(1, objKeys.length * 0.5)
}
/**
* Get the expected type for a relationship field
*/
private getRelationshipType(
parentType: string | null,
fieldName: string
): string | undefined {
if (!parentType || !this.typeMap.has(parentType)) {
return undefined
}
const type = this.typeMap.get(parentType)!
const field = type.getFields()[fieldName]
if (!field) {
return undefined
}
let fieldType = field.type
// Unwrap NonNull and List wrappers
if (isNonNullType(fieldType)) {
fieldType = fieldType.ofType
}
if (isListType(fieldType)) {
fieldType = fieldType.ofType
}
if (isNonNullType(fieldType)) {
fieldType = fieldType.ofType
}
if (isObjectType(fieldType)) {
return fieldType.name
}
return undefined
}
/**
* Build invalidation events based on parsed entities
*/
buildInvalidationEvents(
entities: EntityReference[],
operation: "created" | "updated" | "deleted" = "updated"
): InvalidationEvent[] {
const events: InvalidationEvent[] = []
const processedEntities = new Set<string>()
entities.forEach((entity) => {
const entityKey = `${entity.type}:${entity.id}`
if (processedEntities.has(entityKey)) {
return
}
processedEntities.add(entityKey)
const relatedEntities = entities.filter(
(e) => e.type !== entity.type || e.id !== entity.id
)
const affectedKeys = this.buildAffectedCacheKeys(entity, operation)
events.push({
entityType: entity.type,
entityId: entity.id,
relatedEntities,
cacheKeys: affectedKeys,
})
})
return events
}
/**
* Build list of cache keys that should be invalidated
*/
private buildAffectedCacheKeys(
entity: EntityReference,
operation: "created" | "updated" | "deleted" = "updated"
): string[] {
const keys = new Set<string>()
keys.add(`${entity.type}:${entity.id}`)
// Add list key only if entity was found in an array context or if an event of type created or
// deleted is triggered
if (entity.isInArray || ["created", "deleted"].includes(operation)) {
keys.add(`${entity.type}:list:*`)
}
return Array.from(keys)
}
}
@@ -0,0 +1,133 @@
import type {
Event,
ICachingModuleService,
ICachingStrategy,
ModuleJoinerConfig,
} from "@medusajs/framework/types"
import {
type GraphQLSchema,
Modules,
toCamelCase,
upperCaseFirst,
} from "@medusajs/framework/utils"
import { type CachingModuleService } from "@services"
import type { InjectedDependencies } from "@types"
import stringify from "fast-json-stable-stringify"
import { CacheInvalidationParser, EntityReference } from "./parser"
export class DefaultCacheStrategy implements ICachingStrategy {
#cacheInvalidationParser: CacheInvalidationParser
#cacheModule: ICachingModuleService
#container: InjectedDependencies
#hasher: (data: string) => string
constructor(
container: InjectedDependencies,
cacheModule: CachingModuleService
) {
this.#cacheModule = cacheModule
this.#container = container
this.#hasher = container.hasher
}
objectHash(input: any): string {
const str = stringify(input)
return this.#hasher(str)
}
async onApplicationStart(
schema: GraphQLSchema,
joinerConfigs: ModuleJoinerConfig[]
) {
this.#cacheInvalidationParser = new CacheInvalidationParser(
schema,
joinerConfigs
)
const eventBus = this.#container[Modules.EVENT_BUS]
const handleEvent = async (data: Event) => {
try {
// We dont have to await anything here and the rest can be done in the background
return
} finally {
const eventName = data.name
const operation = eventName.split(".").pop() as
| "created"
| "updated"
| "deleted"
const entityType = eventName.split(".").slice(-2).shift()!
const eventData = data.data as
| { id: string | string[] }
| { id: string | string[] }[]
const normalizedEventData = Array.isArray(eventData)
? eventData
: [eventData]
const tags: string[] = []
for (const item of normalizedEventData) {
const ids = Array.isArray(item.id) ? item.id : [item.id]
for (const id of ids) {
const entityReference: EntityReference = {
type: upperCaseFirst(toCamelCase(entityType)),
id,
}
const tags_ = await this.computeTags(item, {
entities: [entityReference],
operation,
})
tags.push(...tags_)
}
}
void this.#cacheModule.clear({
tags,
options: { autoInvalidate: true },
})
}
}
eventBus.subscribe("*", handleEvent)
eventBus.addInterceptor?.(handleEvent)
}
async computeKey(input: object) {
return this.objectHash(input)
}
async computeTags(
input: object,
options?: {
entities?: EntityReference[]
operation?: "created" | "updated" | "deleted"
}
): Promise<string[]> {
// Parse the input object to identify entities
const entities_ =
options?.entities ||
this.#cacheInvalidationParser.parseObjectForEntities(input)
if (entities_.length === 0) {
return []
}
// Build invalidation events to get comprehensive cache keys
const events = this.#cacheInvalidationParser.buildInvalidationEvents(
entities_,
options?.operation
)
// Collect all unique cache keys from all events as tags
const tags = new Set<string>()
events.forEach((event) => {
event.cacheKeys.forEach((key) => tags.add(key))
})
return Array.from(tags)
}
}