Redis Caching Module Provider

The Redis Caching Module Provider is a robust caching solution that leverages Redis to store cached data. Redis offers high performance, scalability, and data persistence, making it an ideal choice for production environments.

Note: The Caching Module and its providers are available starting Medusa v2.11.0.

Register the Redis Caching Module#

To use the Redis Caching Module Provider, you need to register it in the providers array of the Caching Module in your medusa-config.ts.

medusa-config.ts
1module.exports = defineConfig({2  // ...3  modules: [4    {5      resolve: "@medusajs/medusa/caching",6      options: {7        providers: [8          {9            resolve: "@medusajs/caching-redis",10            id: "caching-redis",11            // Optional, makes this the default caching provider12            is_default: true,13            options: {14              redisUrl: process.env.CACHE_REDIS_URL,15              // more options...16            },17          },18          // other caching providers...19        ],20      },21    },22  ],23})

Notice that you pass an id property to the provider. The provider will be registered with that ID, which you can use to explicitly specify the provider when caching data.

Environment Variables#

Make sure to add the following environment variable:

Terminal
CACHE_REDIS_URL=redis://localhost:6379

Redis Caching Module Options#

Option

Description

Default

redisUrl

The connection URL for the Redis server.

Required. An error is thrown if not provided.

ttl

A number indicating the default time-to-live (TTL) in seconds for cached items. After this period, cached items will be removed from the cache.

3600 (1 hour)

prefix

A string to prefix all cache keys with. This is useful for namespacing your cache keys, especially when sharing a Redis instance with other applications or modules.

No prefix by default

compressionThreshold

A number indicating the size threshold in bytes above which cached items will be compressed before being stored in Redis. This helps save memory when caching large items.

1024 (1 KB)


Test the Redis Caching Module#

You can test the Redis Caching Module by caching data using the Query or Index Module, or by directly using the Caching Module's service, as described in the Caching Module guide.

If you don't set the Redis Caching Module Provider as the default, you can explicitly specify its provider ID caching-redis when caching data with Query, the Index Module, or directly with the Caching Module's service.

Tip: caching-redis is the ID you set in the medusa-config.ts file when registering the Redis Caching Module Provider.

For example, you can create a workflow in src/workflows/cache-products.ts that caches products using the Redis Caching Module Provider:

src/workflows/cache-products.ts
1import {2  createWorkflow,3  WorkflowResponse,4} from "@medusajs/framework/workflows-sdk"5import { useQueryGraphStep } from "@medusajs/medusa/core-flows" 6
7export const cacheProductsWorkflow = createWorkflow(8  "cache-products",9  () => {10    const { data: products } = useQueryGraphStep({11      entity: "product",12      fields: ["id", "title"],13      options: {14        cache: {15          enable: true,16          providers: ["caching-redis"],17        },18      },19    })20
21    return new WorkflowResponse(products)22  }23)

Next, execute that workflow in an API route. For example, create a route at src/api/cache-product/route.ts with the following content:

src/api/cache-product/route.ts
1import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"2import { cacheProductsWorkflow } from "../../workflows/cache-products"3
4export const GET = async (req: MedusaRequest, res: MedusaResponse) => {5  const { result } = await cacheProductsWorkflow(req.scope)6    .run({})7
8  res.status(200).json(result)9}

Finally, start your Medusa server with the following command:

Then, make a GET request to the /cache-product endpoint:

Code
curl http://localhost:9000/cache-product

You should receive a response with the list of products. The first time you make this request, the products will be fetched from the database and cached in Redis. Subsequent requests will retrieve the products from the cache, resulting in improved performance.

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