1 {"slug":"/references/caching-service","title":"How to Use Caching Module","frontmatter":{"slug":"/references/caching-service","tags":["caching","server","how to"],"sidebar_label":"Use Caching Module"},"toc":[{"title":"Resolve Caching Module's Service","id":"resolve-caching-module-s-service","level":2},{"title":"clear","id":"clear","level":2},{"title":"Example","id":"example","level":3},{"title":"Parameters","id":"clear-parameters","level":3},{"title":"Returns","id":"clear-returns","level":3},{"title":"computeKey","id":"computekey","level":2},{"title":"Example","id":"example-1","level":3},{"title":"Parameters","id":"computekey-parameters","level":3},{"title":"Returns","id":"computekey-returns","level":3},{"title":"computeTags","id":"computetags","level":2},{"title":"Example","id":"example-2","level":3},{"title":"Parameters","id":"computetags-parameters","level":3},{"title":"Returns","id":"computetags-returns","level":3},{"title":"get","id":"get","level":2},{"title":"Example","id":"example-3","level":3},{"title":"Parameters","id":"get-parameters","level":3},{"title":"Returns","id":"get-returns","level":3},{"title":"set","id":"set","level":2},{"title":"Example","id":"example-4","level":3},{"title":"Parameters","id":"set-parameters","level":3},{"title":"Returns","id":"set-returns","level":3}],"blocks":[{"kind":"markdown","html":"In this guide, you’ll learn about the different methods in the Caching Module's service and how to use them."},{"kind":"note","variant":"note","html":"The Caching Module and its providers are available starting [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0)."},{"kind":"note","variant":"tip","html":"You should use the Caching Module's service when you're caching computed data or data from external APIs. To cache database query results, enable caching in [Query](!docs!/learn/fundamentals/query#cache) or [Index Module](!docs!/learn/fundamentals/query/index-module#cache) instead."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"Resolve Caching Module's Service","id":"resolve-caching-module-s-service"},{"kind":"markdown","html":"In your workflow's step, you can resolve the Caching Module's service from the Medusa container:\n\n```ts\nimport { Modules } from \"@medusajs/framework/utils\"\nimport { createStep } from \"@medusajs/framework/workflows-sdk\"\n\nconst step1 = createStep(\n \"step-1\",\n async ({}, { container }) => {\n const cachingModuleService = container.resolve(\n Modules.CACHING\n )\n \n // TODO use cachingModuleService\n } \n)\n```\n\nYou can then use the Caching Module's service's methods in the step. The rest of this guide details these methods.\n\n---"},{"kind":"heading","level":2,"text":"clear","id":"clear"},{"kind":"markdown","html":"This method clears data from the cache. If neither `key` nor `tags` are provided, nothing is cleared.\n\nBy default, all items matching the key or tags are cleared regardless of their options. If you provide `options.autoInvalidate: true`,\nonly items that were set with `options.autoInvalidate: true` are cleared.\n\nFor example, if you set `options.autoInvalidate: true`, only items that were set with `options.autoInvalidate: true` are cleared.\nItems that were set with `options.autoInvalidate: false` are only cleared when you don't provide any options."},{"kind":"heading","level":3,"text":"Example","id":"example"},{"kind":"markdown","html":"To invalidate cache by key:\n\n```ts\nawait cachingModuleService.clear({\n key: \"products\" // this key would typically be a hash\n})\n```\n\nThis example will clear the item with the key `products` regardless of its `options.autoInvalidate` value.\n\nTo invalidate cache by tags:\n\n```ts\nawait cachingModuleService.clear({\n tags: [\"Product:list:*\"]\n})\n```\n\nThis example will clear all items with the tag `Product:list:*` regardless of their `options.autoInvalidate` value.\n\nTo invalidate only the cache data that were set to automatically invalidate:\n\n```ts\nawait cachingModuleService.clear({\n tags: [\"Product:list:*\"],\n options: { autoInvalidate: true }\n})\n```\n\nThis example will only clear items with the tag `Product:list:*` that were set with `options.autoInvalidate: true`.\nItems that were set with `options.autoInvalidate: false` will not be cleared."},{"kind":"note","variant":"note","html":"Setting `options.autoInvalidate: false` when calling the `clear` method will not clear any items.\nTo clear items that were set with `options.autoInvalidate: false`, you must call the `clear` method without any options."},{"kind":"markdown","html":"To invalidate cache from specific providers:\n\n```ts\nawait cachingModuleService.clear({\n key: \"products\",\n providers: [\"caching-redis\", \"caching-memcached\"]\n})\n```\n\nThis example will try to clear the data from both the `caching-redis` and `caching-memcached` providers."},{"kind":"heading","level":3,"text":"Parameters","id":"clear-parameters"},{"kind":"typeList","sectionTitle":"clear","types":[{"name":"param0","type":"`object`","description":"The options for clearing the item(s).","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"key","type":"`string`","description":"The key of the item to clear.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"tags","type":"`string`[]","description":"The tags of the items to clear. Tags\nare useful to clear multiple related items at once.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"options","type":"`object`","description":"Options for clearing the item(s). The options are matched against the stored options when the item was set.\nFor example, if the item was set with `autoInvalidate: true`, it will only be cleared if the `autoInvalidate` option is also set to `true`.\nIf not provided, all items matching the key or tags are cleared regardless of their options.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"autoInvalidate","type":"`boolean`","description":"Whether to clear item(s) that were set to automatically invalidate.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"name":"providers","type":"`string`[]","description":"The providers from which to clear the item(s). You can specify an array of provider IDs.\nIf not provided, the [default provider](https://docs.medusajs.com/infrastructure-modules/caching/providers#default-caching-module-provider) is used.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"Returns","id":"clear-returns"},{"kind":"typeList","sectionTitle":"clear","types":[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"A promise that resolves when the item(s) have been cleared.","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"computeKey","id":"computekey"},{"kind":"markdown","html":"This method computes a cache key based on the input object. It's useful to generate consistent and unique keys for caching."},{"kind":"heading","level":3,"text":"Example","id":"example-1"},{"kind":"markdown","html":"```ts\nconst key = await cachingModuleService.computeKey({\n id: \"prod_123\",\n title: \"Product 123\"\n})\n// key will be a hash string like \"a1b2c3d4e5f6g7h8i9j0\"\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"computekey-parameters"},{"kind":"typeList","sectionTitle":"computeKey","types":[{"name":"input","type":"`object`","description":"The input object to compute the key from.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Returns","id":"computekey-returns"},{"kind":"typeList","sectionTitle":"computeKey","types":[{"name":"Promise","type":"Promise&#60;string&#62;","optional":false,"defaultValue":"","description":"The computed cache key.","expandable":false,"children":[{"name":"string","type":"`string`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":2,"text":"computeTags","id":"computetags"},{"kind":"markdown","html":"This method computes cache tags based on the input object. It's useful to generate consistent and relevant tags for caching."},{"kind":"heading","level":3,"text":"Example","id":"example-2"},{"kind":"markdown","html":"```ts\nconst tags = await cachingModuleService.computeTags({\n products: [{ id: \"prod_123\" }, { id: \"prod_456\" }],\n}, {\n operation: \"updated\"\n})\n// tags might be [\"Product:prod_123\", \"Product:prod_456\", \"Product:list:*\"]\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"computetags-parameters"},{"kind":"typeList","sectionTitle":"computeTags","types":[{"name":"input","type":"`object`","description":"The input object to compute the tags from.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"options","type":"`Record<string, any>`","description":"Additional options to influence tag computation.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Returns","id":"computetags-returns"},{"kind":"typeList","sectionTitle":"computeTags","types":[{"name":"Promise","type":"Promise&#60;string[]&#62;","optional":false,"defaultValue":"","description":"An array of computed cache tags.","expandable":false,"children":[{"name":"string[]","type":"`string`[]","optional":false,"defaultValue":"","description":"","expandable":false,"children":[{"name":"string","type":"`string`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":2,"text":"get","id":"get"},{"kind":"markdown","html":"This method retrieves data from the cache. If neither `key` nor `tags` are provided, or the item is not found, `null` is returned."},{"kind":"heading","level":3,"text":"Example","id":"example-3"},{"kind":"markdown","html":"To retrieve by key:\n\n```ts\nconst data = await cachingModuleService.get({\n key: \"products\", // this key would typically be a hash\n}) as { id: string; title: string; }\n```\n\nTo retrieve by tags:\n\n```ts\nconst data = await cachingModuleService.get({\n tags: [\"Product:list:*\"],\n}) as { id: string; title: string; }[]\n```\n\nTo retrieve by key from specific providers:\n\n```ts\nconst data = await cachingModuleService.get({\n key: \"products\", // this key would typically be a hash\n providers: [\"caching-redis\", \"caching-memcached\"]\n}) as { id: string; title: string; }\n```\n\nThis example will try to get the data from the `caching-redis` provider first, and if not found, it will try to get it from the `caching-memcached` provider."},{"kind":"heading","level":3,"text":"Parameters","id":"get-parameters"},{"kind":"typeList","sectionTitle":"get","types":[{"name":"param0","type":"`object`","description":"The options for retrieving the item.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"key","type":"`string`","description":"The key of the item to retrieve.\nIf both `key` and `tags` are provided, `key` takes precedence over `tags`.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"tags","type":"`string`[]","description":"The tags of the items to retrieve. Tags\nare useful to retrieve multiple related items at once.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"providers","type":"`string`[]","description":"The providers to retrieve the item(s) from. You can specify an array of provider IDs.\nThey're checked in the order they're provided in, so make sure to order them based on your priority.\nIf not provided, the [default provider](https://docs.medusajs.com/infrastructure-modules/caching/providers#default-caching-module-provider) is used.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":3,"text":"Returns","id":"get-returns"},{"kind":"typeList","sectionTitle":"get","types":[{"name":"Promise","type":"Promise&#60;any&#62;","optional":false,"defaultValue":"","description":"The item(s) that was stored in the cache. If no item was found, or neither `key` nor `tags` were provided, `null` is returned.","expandable":false,"children":[{"name":"any","type":"`any`","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":2,"text":"set","id":"set"},{"kind":"markdown","html":"This method stores data in the cache using the \n[default Caching Module Provider](https://docs.medusajs.com/infrastructure-modules/caching/providers#default-caching-module-provider)."},{"kind":"heading","level":3,"text":"Example","id":"example-4"},{"kind":"markdown","html":"To store with key:\n\n```ts\nconst data = { id: \"prod_123\", title: \"Product 123\" }\nconst key = await cachingModuleService.computeKey(data)\nawait cachingModuleService.set({\n key,\n data\n})\n```\n\nTo store with tags:"},{"kind":"note","variant":"note","html":"Tags should follow [conventions](https://docs.medusajs.com/infrastructure-modules/caching/concepts#caching-tags-convention) to ensure they're automatically invalidated."},{"kind":"markdown","html":"```ts\nconst data = [{ id: \"prod_123\", title: \"Product 123\" }]\nconst key = await cachingModuleService.computeKey(data)\nawait cachingModuleService.set({\n key,\n data,\n tags: [`Product:${data[0].id}`, \"Product:list:*\"]\n})\n```\n\nTo disable auto-invalidation for the item:\n\n```ts\nconst data = [{ id: \"prod_123\", title: \"Product 123\" }]\nconst key = await cachingModuleService.computeKey(data)\nawait cachingModuleService.set({\n key,\n data,\n options: { autoInvalidate: false }\n})\n```\n\nThe item is now only invalidated when calling the `clear` method directly with the same key or tags.\n\nTo store with specific providers:\n\n```ts\nconst data = { id: \"prod_123\", title: \"Product 123\" }\nconst key = await cachingModuleService.computeKey(data)\nawait cachingModuleService.set({\n key,\n data,\n providers: [\n \"caching-redis\",\n { id: \"caching-memcached\", ttl: 120 } // custom TTL for this provider\n ]\n})\n```\n\nThis example will store the item in both the `caching-redis` and `caching-memcached` providers, with a custom TTL of `120` seconds for the `caching-memcached` provider."},{"kind":"heading","level":3,"text":"Parameters","id":"set-parameters"},{"kind":"typeList","sectionTitle":"set","types":[{"name":"param0","type":"`object`","description":"The options for storing the item.","optional":false,"defaultValue":"","expandable":false,"children":[{"name":"key","type":"`string`","description":"The key of the item to store.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"data","type":"`object`","description":"The data to store in the cache.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"ttl","type":"`number`","description":"The time-to-live (TTL in seconds) value in seconds.\nIf not provided, the default TTL value configured in the provider should be used.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"tags","type":"`string`[]","description":"The tags of the items to store. Tags are useful to group related items\ntogether for retrieval or invalidation.\n\nIf not provided, the tags are computed from `data`. If provided, they're used on\ntheir own unless `computeAutomaticTags` is also set.","optional":true,"defaultValue":"","expandable":false,"children":[]},{"name":"computeAutomaticTags","type":"`boolean`","description":"Whether the tags computed from `data` should be applied along with the ones given\nin `tags`, rather than `tags` being used on its own. Useful for tagging what the\ncomputation can't see, such as link tables, relations selected without their\n`id`, or entities that affect the data without appearing in it, without\nrestating everything it can.\n\nHas no effect when `tags` is omitted.","optional":true,"defaultValue":"false","example":"await cachingModuleService.set({ key, data, tags: [\"LinkProductVariantInventoryItem:list:*\"], computeAutomaticTags: true })","expandable":false,"children":[]},{"name":"options","type":"`object`","description":"Options for storing the item. The options are stored with the item, allowing you to later match against them when clearing the item.\nFor example, if you set `autoInvalidate: false`, the item will only be invalidated when calling the `clear` method directly with the same key or tags.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"autoInvalidate","type":"`boolean`","description":"Whether to automatically invalidate the item when related data changes.","optional":true,"defaultValue":"true","expandable":false,"children":[]}]},{"name":"providers","type":"[Providers](/references/types/types/types.Providers)","description":"The providers to store the item(s) in. You can specify an array of provider IDs or an array of objects with provider ID and TTL.\nIf not provided, the [default provider](https://docs.medusajs.com/infrastructure-modules/caching/providers#default-caching-module-provider) is used.","optional":true,"defaultValue":"","expandable":false,"children":[{"name":"id","type":"`string`","description":"The ID of the provider to use, as set in `medusa-config.ts`.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"ttl","type":"`number`","description":"Optional custom time-to-live (TTL) (in seconds) for this provider. If not provided, the default TTL configured\nin the provider is used, or the default TTL of the Caching Module if not configured in the provider.","optional":true,"defaultValue":"","expandable":false,"children":[]}]}]}]},{"kind":"heading","level":3,"text":"Returns","id":"set-returns"},{"kind":"typeList","sectionTitle":"set","types":[{"name":"Promise","type":"Promise&#60;void&#62;","optional":false,"defaultValue":"","description":"A promise that resolves when the item has been stored.","expandable":false,"children":[]}]}]}