|
| 1 | +export const metadata = { |
| 2 | + title: `How to Clear Cached Data`, |
| 3 | +} |
| 4 | + |
| 5 | +# {metadata.title} |
| 6 | + |
| 7 | +In this guide, you'll learn how to clear cached data in Medusa. |
| 8 | + |
| 9 | +## Why Clear Cache? |
| 10 | + |
| 11 | +You should mainly cache data that isn't frequently changing, such as product details or categories. However, there are scenarios where you might need to clear the cache of that data manually. |
| 12 | + |
| 13 | +For example, if you've integrated a third-party system that updates product information outside of Medusa, or if you've cached data from the third-party system, Medusa won't be aware of changes made externally. |
| 14 | + |
| 15 | +In such cases, you should clear the cache in Medusa to ensure it fetches the most up-to-date information from the source rather than relying on outdated cached data. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## How to Clear Cache |
| 20 | + |
| 21 | +This section explains how to clear data cached by the Caching Module in Medusa. |
| 22 | + |
| 23 | +### Identify Cache Tags |
| 24 | + |
| 25 | +Before clearing the cache, identify the specific cache tags associated with the data you want to clear. |
| 26 | + |
| 27 | +When you cache entities with the [Query](!docs!/learn/fundamentals/module-links/query) or [Index Module](!docs!/learn/fundamentals/module-links/index-module), the Caching Module automatically generates tags based on the entity type and its ID: |
| 28 | + |
| 29 | +- `Entity:id`: Cache tag for a single record of an entity. For example, `Product:prod_123` caches a single product with the ID `prod_123`. |
| 30 | +- `Entity:list:*`: Cache tag for a list of records of an entity. For example, `Product:list:*` caches a list of products. |
| 31 | + |
| 32 | +To clear the cache for a specific product, use the `Product:{id}` tag. To clear the cache for all products, use the `Product:list:*` tag. |
| 33 | + |
| 34 | +Refer to the [Caching Module Concepts guide](../../concepts/page.mdx#caching-tags-convention) to learn more about cache tags. |
| 35 | + |
| 36 | +### Clear Cache with Caching Module Service |
| 37 | + |
| 38 | +To clear cached data, use the [clear](/references/caching-service#clear) method of the Caching Module's service. You can resolve the service in a workflow step, API route, subscriber, or scheduled job, then call the `clear` method with the identified cache tags. |
| 39 | + |
| 40 | +For example, to clear the cache for specific products in a workflow step: |
| 41 | + |
| 42 | +```ts |
| 43 | +import { createStep, StepResponse } from "@medusajs/framework/workflows-sdk" |
| 44 | +import { Modules } from "@medusajs/framework/utils" |
| 45 | + |
| 46 | +type ClearProductCacheInput = { |
| 47 | + productId: string | string[] |
| 48 | +} |
| 49 | + |
| 50 | +export const clearProductCacheStep = createStep( |
| 51 | + "clear-product-cache", |
| 52 | + async ({ productId }: ClearProductCacheInput, { container }) => { |
| 53 | + const cachingModuleService = container.resolve(Modules.CACHING) |
| 54 | + |
| 55 | + const productIds = Array.isArray(productId) ? productId : [productId] |
| 56 | + |
| 57 | + // Clear cache for all specified products |
| 58 | + for (const id of productIds) { |
| 59 | + if (id) { |
| 60 | + await cachingModuleService.clear({ |
| 61 | + tags: [`Product:${id}`], |
| 62 | + }) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return new StepResponse({}) |
| 67 | + } |
| 68 | +) |
| 69 | +``` |
| 70 | + |
| 71 | +In this example, the `clearProductCacheStep` step takes a `productId` (or an array of IDs) as input and clears the cache for each specified product using its cache tag. |
| 72 | + |
| 73 | +You can then use this step in a workflow to clear the cache whenever necessary, such as after receiving a webhook from a third-party system indicating that product data has changed. |
0 commit comments