1 {"slug":"/references/cache-service","title":"How to Use Cache Module","frontmatter":{"slug":"/references/cache-service","tags":["cache","server","how to"],"sidebar_label":"Use Cache Module","keywords":["cache","provider","integration"]},"toc":[{"title":"Resolve Cache Module's Service","id":"resolve-cache-module-s-service","level":2},{"title":"get","id":"get","level":2},{"title":"Example","id":"example","level":3},{"title":"Type Parameters","id":"get-type-parameters","level":3},{"title":"Parameters","id":"get-parameters","level":3},{"title":"Returns","id":"get-returns","level":3},{"title":"invalidate","id":"invalidate","level":2},{"title":"Example","id":"example-1","level":3},{"title":"Parameters","id":"invalidate-parameters","level":3},{"title":"Returns","id":"invalidate-returns","level":3},{"title":"set","id":"set","level":2},{"title":"Example","id":"example-2","level":3},{"title":"Parameters","id":"set-parameters","level":3},{"title":"Returns","id":"set-returns","level":3}],"blocks":[{"kind":"markdown","html":"In this document, you’ll learn about the different methods in the Cache Module's service and how to use them."},{"kind":"note","variant":"note","title":"Deprecation Notice","html":"The Cache Module is deprecated starting from [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0). [Use the Caching Module](https://docs.medusajs.com/resources/infrastructure-modules/caching) instead."},{"kind":"markdown","html":"---"},{"kind":"heading","level":2,"text":"Resolve Cache Module's Service","id":"resolve-cache-module-s-service"},{"kind":"markdown","html":"In your workflow's step, you can resolve the Cache 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 cacheModuleService = container.resolve(\n Modules.CACHE\n )\n \n // TODO use cacheModuleService\n } \n)\n```\n\nThis will resolve the service of the configured Cache Module, which is the [In-Memory Cache Module](https://docs.medusajs.com/resources/infrastructure-modules/cache/in-memory) by default.\n\nYou can then use the Cache Module's service's methods in the step. The rest of this guide details these methods.\n\n---"},{"kind":"heading","level":2,"text":"get","id":"get"},{"kind":"markdown","html":"This method retrieves data from the cache."},{"kind":"heading","level":3,"text":"Example","id":"example"},{"kind":"markdown","html":"```ts\nconst data = await cacheModuleService.get(\"my-key\")\n```"},{"kind":"heading","level":3,"text":"Type Parameters","id":"get-type-parameters"},{"kind":"typeList","sectionTitle":"get","types":[{"name":"T","type":"`object`","description":"","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Parameters","id":"get-parameters"},{"kind":"typeList","sectionTitle":"get","types":[{"name":"key","type":"`string`","description":"The key of the item to retrieve.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Returns","id":"get-returns"},{"kind":"typeList","sectionTitle":"get","types":[{"name":"Promise","type":"Promise<null \\| T>","optional":false,"defaultValue":"","description":"The item that was stored in the cache. If the item was not found, null is returned.","expandable":false,"children":[{"name":"null \\| T","type":"`null` \\| T","optional":false,"defaultValue":"","description":"","expandable":false,"children":[]}]}]},{"kind":"heading","level":2,"text":"invalidate","id":"invalidate"},{"kind":"markdown","html":"This method removes an item from the cache."},{"kind":"heading","level":3,"text":"Example","id":"example-1"},{"kind":"markdown","html":"```ts\nawait cacheModuleService.invalidate(\"my-key\")\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"invalidate-parameters"},{"kind":"typeList","sectionTitle":"invalidate","types":[{"name":"key","type":"`string`","description":"The key of the item to remove.","optional":false,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Returns","id":"invalidate-returns"},{"kind":"typeList","sectionTitle":"invalidate","types":[{"name":"Promise","type":"Promise<void>","optional":false,"defaultValue":"","description":"This method removes an item from the cache.","expandable":false,"children":[]}]},{"kind":"heading","level":2,"text":"set","id":"set"},{"kind":"markdown","html":"This method stores data in the cache."},{"kind":"heading","level":3,"text":"Example","id":"example-2"},{"kind":"markdown","html":"```ts\nawait cacheModuleService.set(\"my-key\", { product_id: \"prod_123\" }, 60)\n```"},{"kind":"heading","level":3,"text":"Parameters","id":"set-parameters"},{"kind":"typeList","sectionTitle":"set","types":[{"name":"key","type":"`string`","description":"The key of the item to store.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"data","type":"`unknown`","description":"The data to store in the cache.","optional":false,"defaultValue":"","expandable":false,"children":[]},{"name":"ttl","type":"`number`","description":"The time-to-live (TTL) value in seconds. If not provided, the default TTL value is used. The default value is based on the used Cache Module.","optional":true,"defaultValue":"","expandable":false,"children":[]}]},{"kind":"heading","level":3,"text":"Returns","id":"set-returns"},{"kind":"typeList","sectionTitle":"set","types":[{"name":"Promise","type":"Promise<void>","optional":false,"defaultValue":"","description":"This method stores data in the cache.","expandable":false,"children":[]}]}]}