Cache Module
In this document, you'll learn what a Cache Module is and how to use it in your Medusa application.
What is a Cache Module?#
A Cache Module is used to cache the results of computations such as price selection or various tax calculations.
The underlying database, third-party service, or caching logic is flexible since it's implemented in a module. You can choose from Medusa’s cache modules or create your own to support something more suitable for your architecture.
Default Cache Module#
By default, Medusa uses the In-Memory Cache Module. This module uses a plain JavaScript Map object to store the cache data. While this is suitable for development, it's recommended to use other Cache Modules, such as the Redis Cache Module, for production. You can also Create a Cache Module.
How to Use the Cache Module?#
You can use the registered Cache Module as part of the workflows you build for your custom features. A workflow is a special function composed of a series of steps that guarantees data consistency and reliable roll-back mechanism.
In a step of your workflow, you can resolve the Cache Module's service and use its methods to cache data, retrieve cached data, or clear the cache.
For example:
1import { Modules } from "@medusajs/framework/utils"2import { 3 createStep,4 createWorkflow,5} from "@medusajs/framework/workflows-sdk"6 7const step1 = createStep(8 "step-1",9 async ({}, { container }) => {10 const cacheModuleService = container.resolve(11 Modules.CACHE12 )13 14 await cacheModuleService.set("key", "value")15 } 16)17 18export const workflow = createWorkflow(19 "workflow-1",20 () => {21 step1()22 }23)
In the example above, you create a workflow that has a step. In the step, you resolve the service of the Cache Module from the Medusa container.
Then, you use the set
method of the Cache Module to cache the value "value"
with the key "key"
.
List of Cache Modules#
Medusa provides the following Cache Modules. You can use one of them, or Create a Cache Module.