Caching Module Providers
In this guide, you'll learn about Caching Module Providers in Medusa, including how to configure and use them in your Medusa application.
What is a Caching Module Provider?#
A Caching Module Provider implements the logic for caching data, such as integrating third-party caching services. The Caching Module then uses the registered Caching Module Providers to handle caching data.
Medusa provides the Redis Caching Module Provider that you can use in development and production. You can also Create a Caching Provider.
Default Caching Module Provider#
You can register multiple Caching Module Providers and specify which one to use as the default. The Caching Module uses the default provider for all caching operations unless you specify a specific provider.
How is the Default Provider Selected?#
The Caching Module determines the default Caching Module Provider based on the following scenarios:
Scenario | Default Provider |
---|---|
One provider is registered. | The registered provider. |
Multiple providers and one of them has an | The provider with the |
If none of the above scenarios apply, the Caching Module throws an error during startup indicating that no default provider is configured.
Setting the Default Caching Module Provider#
To specify a provider as the default, you can set its is_default
option to true
when registering it in the provider
array of the Caching Module.
For example:
1module.exports = defineConfig({2 // ...3 modules: [4 {5 resolve: "@medusajs/medusa/caching",6 options: {7 providers: [8 {9 id: "caching-redis",10 resolve: "@medusajs/caching-redis",11 is_default: true, // Set as the default provider12 options: {13 // Redis options...14 },15 },16 {17 id: "caching-memcached",18 resolve: "./path/to/your/memcached-provider",19 options: {20 // Memcached options...21 },22 },23 ],24 },25 },26 ],27})
In this example, the Redis Caching Module Provider is set as the default provider by setting is_default: true
. The Memcached Caching Module Provider is also registered but not set as the default.
Caching with Specific Providers#
Whether you're caching data with Query, the Index Module, or directly using the Caching Module's service, you can specify an array of provider IDs to use for that specific caching operation.
For example, considering you have the above configuration with both Redis and Memcached providers registered, you can specify which provider to use when caching data:
In this example, both Query and the Caching Module's service use the Memcached provider, overriding the default Redis provider.
The ID you pass is the same ID you specified in medusa-config.ts
when registering the provider.