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.
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
.
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:
Redis Caching Module Options#
Option | Description | Default |
---|---|---|
| The connection URL for the Redis server. | Required. An error is thrown if not provided. |
| 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. |
|
| 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 |
| 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. |
|
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.
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:
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:
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:
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.