www/apps/resources/app/infrastructure-modules/caching/providers/redis/page.mdx
import { Table, Prerequisites } from "docs-ui"
export const metadata = {
title: 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.
</Note><Prerequisites items={[ { text: "Redis installed and Redis server running", link: "https://redis.io/docs/getting-started/installation/" } ]} />
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.
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/caching",
options: {
providers: [
{
resolve: "@medusajs/caching-redis",
id: "caching-redis",
// Optional, makes this the default caching provider
is_default: true,
options: {
redisUrl: process.env.CACHE_REDIS_URL,
// more options...
},
},
// other caching providers...
],
},
},
],
})
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.
Make sure to add the following environment variable:
CACHE_REDIS_URL=redis://localhost:6379
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.
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:
import {
createWorkflow,
WorkflowResponse,
} from "@medusajs/framework/workflows-sdk"
import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
export const cacheProductsWorkflow = createWorkflow(
"cache-products",
() => {
const { data: products } = useQueryGraphStep({
entity: "product",
fields: ["id", "title"],
options: {
cache: {
enable: true,
providers: ["caching-redis"],
},
},
})
return new WorkflowResponse(products)
}
)
Next, execute that workflow in an API route. For example, create a route at src/api/cache-product/route.ts with the following content:
import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
import { cacheProductsWorkflow } from "../../workflows/cache-products"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const { result } = await cacheProductsWorkflow(req.scope)
.run({})
res.status(200).json(result)
}
Finally, start your Medusa server with the following command:
npm run dev
Then, make a GET request to the /cache-product endpoint:
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.