Migrate from Cache Module to Caching Module

In this guide, you'll learn how to migrate from the deprecated Cache Module to the new Caching Module.

Note: The Caching Module is available starting Medusa v2.11.0.
This guide is for developers who have: 
  1. Set up a Cache Module in medusa-config.ts.
  2. Used the Cache Module's service in their code.

If you haven't done either of these, you don't need to migrate to the Caching Module. You can refer to the Caching Module guide to learn how to set it up.

Why Migrate to the Caching Module?#

The Caching Module provides improved performance, flexibility, and scalability compared to the deprecated Cache Module.

It also offers a better developer experience, making it easier to cache Query results and other data.

Additionally, the Caching Module supports registering multiple caching providers, such as Redis and Memcached, allowing you to use different caching backends in your application.


Architectural Module Changes#

The Cache Module implements caching logic, including integration with third-party caching services like Redis. For example, the Redis Cache Module handled connecting to the Redis server and performing caching operations.

The Caching Module, on the other hand, follows a provider-based architecture. The Caching Module provides the interface you use to manage cached data, while Caching Module Providers implement the actual caching logic. You can choose which provider to use, such as the Redis Caching Module Provider or a custom provider you create.

This separation of concerns allows for greater flexibility and extensibility. You can easily switch between different caching providers or create your own custom provider without modifying the core Caching Module.

Diagram illustrating the change in architecture from Cache Module to Caching Module


How to Migrate to the Caching Module#

1. Remove the Cache Module#

The first step is to remove the Cache Module from your medusa-config.ts file.

For example, if you set up the Redis Cache Module, remove the following code from medusa-config.ts:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  modules: [4    // REMOVE THE FOLLOWING LINES5    {6      resolve: "@medusajs/medusa/cache-redis",7      options: { 8        redisUrl: process.env.CACHE_REDIS_URL,9      },10    },11  ],12})

2. Install and Register the Caching Module#

The Caching Module is installed by default in your application. To use it, enable the caching feature flag and register the module in your medusa-config.ts file.

Refer to the Caching Module guide for setup instructions.

3. (Optional) Update Your Code to Use the Caching Module's Service#

If you're using the Cache Module's service in your code, update it to use the Caching Module's service instead.

Container Key Change

Previously, you resolved the Cache Module's service using the Modules.CACHE or cache key.

Now, use the Modules.CACHING or caching key to resolve the Caching Module's service. For example:

Code
1const cachingModuleService = container.resolve(Modules.CACHING)2// or3const cachingModuleService = container.resolve("caching")

Method Changes

The Caching Module's service has similar methods to the Cache Module's service:

  1. get -> Use the Caching Module's get method.
  2. set -> Use the Caching Module's set method.
  3. invalidate -> Use the Caching Module's clear method.

4. (Optional) Create Custom Caching Module Provider#

If you have a custom Cache Module, recreate it as a custom Caching Module Provider. For example, recreate a custom Memcached Cache Module as a Caching Module Provider.

The Caching Module Provider's service has similar methods to the Cache Module's service. Refer to the Create Caching Module Provider guide for instructions on creating a custom Caching Module Provider.


Understand Caching Changes#

With the Cache Module, you handled caching and invalidation manually.

The Caching Module can now handle caching and invalidation automatically for certain operations, such as caching Query results. You can still use the Caching Module's service to cache custom data.

Learn more about the Caching Module in the Caching Module guide.

Was this page helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break