@medusajs/caching
Documentation | Website
Building blocks for digital commerce
Caching Module for Medusa: caches data such as Query and Index Module results in a pluggable caching service like Redis. It replaces the deprecated Cache Modules (@medusajs/cache-inmemory, @medusajs/cache-redis) and uses a provider pattern, so the underlying store is a Caching Module Provider. It ships with @medusajs/medusa and is registered from @medusajs/medusa/caching.
At a glance
| Package | @medusajs/caching |
| Type | Infrastructure Module |
| Module key | Modules.CACHING ("caching") |
| Registered by default | No |
| Import from | @medusajs/medusa/caching |
Features
- Cache Query and Index Module results, or cache any data directly through the module's service.
- Pluggable providers, with a default provider selected by
is_default(or the only registered provider). Official provider:@medusajs/caching-redis: stores cached data in Redis.
Installation
No installation needed. The module ships with @medusajs/medusa. To use it:
- Enable the caching feature flag by setting
MEDUSA_FF_CACHING=truein your environment. - Register the module with at least one provider in
medusa-config.ts. The module throws on startup if no provider is configured.
Usage
import { defineConfig } from "@medusajs/framework/utils"
module.exports = defineConfig({
// ...
modules: [
{
resolve: "@medusajs/medusa/caching",
options: {
providers: [
{
resolve: "@medusajs/caching-redis",
id: "caching-redis",
is_default: true,
options: {
redisUrl: process.env.CACHE_REDIS_URL,
},
},
],
},
},
],
})
Then resolve the service with container.resolve(Modules.CACHING).
Configuration
| Option | Type | Description | Default |
|---|---|---|---|
providers |
{ resolve: string, id: string, is_default?: boolean, options?: Record<string, unknown> }[] |
Caching Module Providers to register. | - |
ttl |
number |
Time to keep data in the cache, in seconds. | 3600 |