{"_id":"@cacheable/memoize","_rev":"4179180","name":"@cacheable/memoize","description":"Memoization utilities for cacheable","dist-tags":{"latest":"2.0.3"},"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"time":{"modified":"2026-03-01T21:43:45.000Z","created":"2025-07-26T01:06:46.609Z","2.0.3":"2025-10-01T17:33:10.421Z","2.0.2":"2025-09-22T15:46:53.468Z","2.0.1":"2025-09-16T19:03:16.024Z","2.0.0":"2025-09-09T17:57:52.827Z","1.1.1":"2025-08-26T22:20:25.930Z","1.1.0":"2025-07-31T03:15:34.977Z","1.0.0":"2025-07-26T01:06:46.609Z"},"users":{},"author":{"name":"Jared Wray","email":"me@jaredwray.com"},"repository":{"type":"git","url":"git+https://github.com/jaredwray/cacheable.git","directory":"packages/memoize"},"versions":{"2.0.3":{"name":"@cacheable/memoize","version":"2.0.3","description":"Memoization utilities for cacheable","type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.js"}},"repository":{"type":"git","url":"git+https://github.com/jaredwray/cacheable.git","directory":"packages/memoize"},"author":{"name":"Jared Wray","email":"me@jaredwray.com"},"license":"MIT","private":false,"devDependencies":{"@biomejs/biome":"^2.2.4","@faker-js/faker":"^10.0.0","@types/node":"^24.5.2","@vitest/coverage-v8":"^3.2.4","rimraf":"^6.0.1","tsup":"^8.5.0","typescript":"^5.9.2","vitest":"^3.2.4"},"keywords":["cacheable","caching","keyv","memoization","memoize","performance","wrapper","wrap","getOrSet"],"dependencies":{"@cacheable/utils":"^2.0.3"},"scripts":{"build":"rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean --minify","prepublish":"pnpm build","lint":"biome check --write --error-on-warnings","test":"pnpm lint && vitest run --coverage","test:ci":"biome check --error-on-warnings && vitest run --coverage","clean":"rimraf ./dist ./coverage ./node_modules"},"_id":"@cacheable/memoize@2.0.3","bugs":{"url":"https://github.com/jaredwray/cacheable/issues"},"homepage":"https://github.com/jaredwray/cacheable#readme","_integrity":"sha512-hl9wfQgpiydhQEIv7fkjEzTGE+tcosCXLKFDO707wYJ/78FVOlowb36djex5GdbSyeHnG62pomYLMuV/OT8Pbw==","_resolved":"/private/var/folders/h1/n3vxgc0n1sn5_9pxftc4p6l80000gn/T/fb6e22e585fcef27394598355f361f70/cacheable-memoize-2.0.3.tgz","_from":"file:cacheable-memoize-2.0.3.tgz","_nodeVersion":"20.17.0","_npmVersion":"11.4.1","dist":{"shasum":"64b18a6b42f987fe8a9e9e2e4391b14cbf85680f","size":5062,"noattachment":false,"key":"/@cacheable/memoize/-/@cacheable/memoize-2.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/@cacheable/memoize/download/@cacheable/memoize-2.0.3.tgz"},"_npmUser":{"name":"jaredwray","email":"me@jaredwray.com"},"directories":{},"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_2.0.3_1759339990265_0.8506939281823498"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-10-01T17:33:10.421Z","publish_time":1759339990421,"_source_registry_name":"default","_cnpm_publish_time":1759339990421},"2.0.2":{"name":"@cacheable/memoize","version":"2.0.2","description":"Memoization utilities for cacheable","type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.js"}},"repository":{"type":"git","url":"git+https://github.com/jaredwray/cacheable.git","directory":"packages/memoize"},"author":{"name":"Jared Wray","email":"me@jaredwray.com"},"license":"MIT","private":false,"devDependencies":{"@biomejs/biome":"^2.2.2","@faker-js/faker":"^10.0.0","@types/node":"^24.3.0","@vitest/coverage-v8":"^3.2.4","lru-cache":"^11.1.0","rimraf":"^6.0.1","tsup":"^8.5.0","typescript":"^5.9.2","vitest":"^3.2.4"},"keywords":["cacheable","caching","keyv","memoization","memoize","performance","wrapper","wrap","getOrSet"],"dependencies":{"@cacheable/utils":"^2.0.2"},"scripts":{"build":"rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean --minify","prepublish":"pnpm build","lint":"biome check --write --error-on-warnings","test":"pnpm lint && vitest run --coverage","test:ci":"biome check --error-on-warnings && vitest run --coverage","clean":"rimraf ./dist ./coverage ./node_modules"},"_id":"@cacheable/memoize@2.0.2","bugs":{"url":"https://github.com/jaredwray/cacheable/issues"},"homepage":"https://github.com/jaredwray/cacheable#readme","_integrity":"sha512-wPrr7FUiq3Qt4yQyda2/NcOLTJCFcQSU3Am2adP+WLy+sz93/fKTokVTHmtz+rjp4PD7ee0AEOeRVNN6IvIfsg==","_resolved":"/private/var/folders/q4/x95kq1ln6cd7rrnct9cby32r0000gn/T/f23f4ba20bb4194a2767fdbfc5213fa0/cacheable-memoize-2.0.2.tgz","_from":"file:cacheable-memoize-2.0.2.tgz","_nodeVersion":"22.12.0","_npmVersion":"11.6.0","dist":{"shasum":"5b4796906454a1f4b41ccd789a9d9e6678ee9901","size":5045,"noattachment":false,"key":"/@cacheable/memoize/-/@cacheable/memoize-2.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/@cacheable/memoize/download/@cacheable/memoize-2.0.2.tgz"},"_npmUser":{"name":"jaredwray","email":"me@jaredwray.com"},"directories":{},"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_2.0.2_1758556013274_0.7891791282586891"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-09-22T15:46:53.468Z","publish_time":1758556013468,"_source_registry_name":"default","_cnpm_publish_time":1758556013468},"2.0.1":{"name":"@cacheable/memoize","version":"2.0.1","description":"Memoization utilities for cacheable","type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.js"}},"repository":{"type":"git","url":"git+https://github.com/jaredwray/cacheable.git","directory":"packages/memoize"},"author":{"name":"Jared Wray","email":"me@jaredwray.com"},"license":"MIT","private":false,"devDependencies":{"@biomejs/biome":"^2.2.2","@faker-js/faker":"^10.0.0","@types/node":"^24.3.0","@vitest/coverage-v8":"^3.2.4","lru-cache":"^11.1.0","rimraf":"^6.0.1","tsup":"^8.5.0","typescript":"^5.9.2","vitest":"^3.2.4"},"keywords":["cacheable","caching","keyv","memoization","memoize","performance","wrapper","wrap","getOrSet"],"dependencies":{"@cacheable/utils":"^2.0.1"},"scripts":{"build":"rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean --minify","prepublish":"pnpm build","lint":"biome check --write --error-on-warnings","test":"pnpm lint && vitest run --coverage","test:ci":"biome check --error-on-warnings && vitest run --coverage","clean":"rimraf ./dist ./coverage ./node_modules"},"_id":"@cacheable/memoize@2.0.1","bugs":{"url":"https://github.com/jaredwray/cacheable/issues"},"homepage":"https://github.com/jaredwray/cacheable#readme","_integrity":"sha512-WBLH37SynkCa39S6IrTSMQF3Wdv4/51WxuU5TuCNEqZcLgLGHme8NUxRTcDIO8ZZFXlslWbh9BD3DllixgPg6Q==","_resolved":"/private/var/folders/q4/x95kq1ln6cd7rrnct9cby32r0000gn/T/6574bf6da99c5dae82591a465efe4c05/cacheable-memoize-2.0.1.tgz","_from":"file:cacheable-memoize-2.0.1.tgz","_nodeVersion":"22.12.0","_npmVersion":"11.6.0","dist":{"shasum":"fe8b353db579de267cca6365a9d44f07947aa920","size":5044,"noattachment":false,"key":"/@cacheable/memoize/-/@cacheable/memoize-2.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/@cacheable/memoize/download/@cacheable/memoize-2.0.1.tgz"},"_npmUser":{"name":"jaredwray","email":"me@jaredwray.com"},"directories":{},"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_2.0.1_1758049395848_0.890560308960862"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-09-16T19:03:16.024Z","publish_time":1758049396024,"_source_registry_name":"default","_cnpm_publish_time":1758049396024},"2.0.0":{"name":"@cacheable/memoize","version":"2.0.0","description":"Memoization utilities for cacheable","type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.js"}},"repository":{"type":"git","url":"git+https://github.com/jaredwray/cacheable.git","directory":"packages/memoize"},"author":{"name":"Jared Wray","email":"me@jaredwray.com"},"license":"MIT","private":false,"devDependencies":{"@biomejs/biome":"^2.2.2","@faker-js/faker":"^10.0.0","@types/node":"^24.3.0","@vitest/coverage-v8":"^3.2.4","lru-cache":"^11.1.0","rimraf":"^6.0.1","tsup":"^8.5.0","typescript":"^5.9.2","vitest":"^3.2.4"},"keywords":["cacheable","caching","keyv","memoization","memoize","performance","wrapper","wrap","getOrSet"],"dependencies":{"@cacheable/utils":"^2.0.0"},"scripts":{"build":"rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean --minify","prepublish":"pnpm build","lint":"biome check --write --error-on-warnings","test":"pnpm lint && vitest run --coverage","test:ci":"biome check --error-on-warnings && vitest run --coverage","clean":"rimraf ./dist ./coverage ./node_modules"},"_id":"@cacheable/memoize@2.0.0","bugs":{"url":"https://github.com/jaredwray/cacheable/issues"},"homepage":"https://github.com/jaredwray/cacheable#readme","_integrity":"sha512-Q6PjhmFuLOKLFSLrs/2NaGvryuiTsUJON+rJlUxkzXQowja7hmqD9Y8iFU9UgocMoUbW31bMcpyGZWj5A8Q+1Q==","_resolved":"/private/var/folders/h1/n3vxgc0n1sn5_9pxftc4p6l80000gn/T/335a375a5d66430a8a08114c4e7d097b/cacheable-memoize-2.0.0.tgz","_from":"file:cacheable-memoize-2.0.0.tgz","_nodeVersion":"20.17.0","_npmVersion":"11.4.1","dist":{"shasum":"1cbd40f8ce1cc864c2b8aa7521174094e124d2f0","size":5074,"noattachment":false,"key":"/@cacheable/memoize/-/@cacheable/memoize-2.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@cacheable/memoize/download/@cacheable/memoize-2.0.0.tgz"},"_npmUser":{"name":"jaredwray","email":"me@jaredwray.com"},"directories":{},"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_2.0.0_1757440672631_0.08288536746023523"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-09-09T17:57:52.827Z","publish_time":1757440672827,"_source_registry_name":"default","_cnpm_publish_time":1757440672827},"1.1.1":{"name":"@cacheable/memoize","version":"1.1.1","description":"Memoization utilities for cacheable","type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.js"}},"repository":{"type":"git","url":"git+https://github.com/jaredwray/cacheable.git","directory":"packages/memoize"},"author":{"name":"Jared Wray","email":"me@jaredwray.com"},"license":"MIT","private":false,"devDependencies":{"@biomejs/biome":"^2.2.2","@faker-js/faker":"^10.0.0","@types/node":"^24.3.0","@vitest/coverage-v8":"^3.2.4","lru-cache":"^11.1.0","rimraf":"^6.0.1","tsup":"^8.5.0","typescript":"^5.9.2","vitest":"^3.2.4"},"keywords":["cacheable","caching","keyv","memoization","memoize","performance","wrapper","wrap","getOrSet"],"dependencies":{"@cacheable/utils":"^1.1.1"},"scripts":{"build":"rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean","prepublish":"pnpm build","lint":"biome check --write --error-on-warnings","test":"pnpm lint && vitest run --coverage","test:ci":"biome check --error-on-warnings && vitest run --coverage","clean":"rimraf ./dist ./coverage ./node_modules"},"_id":"@cacheable/memoize@1.1.1","bugs":{"url":"https://github.com/jaredwray/cacheable/issues"},"homepage":"https://github.com/jaredwray/cacheable#readme","_integrity":"sha512-/O7iqUCOrT7tUV5Ic1Pp5YRuyXbcznFpq/nqA+Ep5v8Ops4qy+kOEnigsTdTRnNEX1+WkOXTRvkMrqR1+rL+0A==","_resolved":"/private/var/folders/q4/x95kq1ln6cd7rrnct9cby32r0000gn/T/87202e3b00ad29a28388e288e615611d/cacheable-memoize-1.1.1.tgz","_from":"file:cacheable-memoize-1.1.1.tgz","_nodeVersion":"22.12.0","_npmVersion":"11.4.2","dist":{"shasum":"94afe5cf748b909b67132ecd016e492c51306cc4","size":5023,"noattachment":false,"key":"/@cacheable/memoize/-/@cacheable/memoize-1.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/@cacheable/memoize/download/@cacheable/memoize-1.1.1.tgz"},"_npmUser":{"name":"jaredwray","email":"me@jaredwray.com"},"directories":{},"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_1.1.1_1756246825714_0.257594632068483"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-08-26T22:20:25.930Z","publish_time":1756246825930,"_source_registry_name":"default","_cnpm_publish_time":1756246825930},"1.1.0":{"name":"@cacheable/memoize","version":"1.1.0","description":"Memoization utilities for cacheable","type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.js"}},"repository":{"type":"git","url":"git+https://github.com/jaredwray/cacheable.git","directory":"packages/memoize"},"author":{"name":"Jared Wray","email":"me@jaredwray.com"},"license":"MIT","private":false,"devDependencies":{"@faker-js/faker":"^9.9.0","@keyv/redis":"^5.0.0","@keyv/valkey":"^1.0.7","@types/eslint":"^9.6.1","@types/node":"^24.1.0","@vitest/coverage-v8":"^3.2.4","lru-cache":"^11.1.0","rimraf":"^6.0.1","tsup":"^8.5.0","typescript":"^5.8.3","vitest":"^3.2.4","xo":"^1.2.1"},"keywords":["cacheable","caching","keyv","memoization","memoize","performance","wrapper","wrap","getOrSet"],"dependencies":{"@cacheable/utils":"^1.1.0"},"scripts":{"build":"rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean","prepublish":"pnpm build","test":"xo --fix && vitest run --coverage","test:ci":"xo && vitest run --coverage","clean":"rimraf ./dist ./coverage ./node_modules"},"_id":"@cacheable/memoize@1.1.0","bugs":{"url":"https://github.com/jaredwray/cacheable/issues"},"homepage":"https://github.com/jaredwray/cacheable#readme","_integrity":"sha512-j3np1W5pRP1ob599hE9YA0jxa4VoQWA64LWEc1kSJ4n6r1+djb4CN5qZkAfciYHxa4jvh5+va9jkDPNX2IJBrA==","_resolved":"/private/var/folders/q4/x95kq1ln6cd7rrnct9cby32r0000gn/T/a741230164db011ea8177cfb8692466d/cacheable-memoize-1.1.0.tgz","_from":"file:cacheable-memoize-1.1.0.tgz","_nodeVersion":"22.12.0","_npmVersion":"11.4.2","dist":{"shasum":"3ba76bd9fb2cd642274bf576cf1e82c80a718a7b","size":5021,"noattachment":false,"key":"/@cacheable/memoize/-/@cacheable/memoize-1.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@cacheable/memoize/download/@cacheable/memoize-1.1.0.tgz"},"_npmUser":{"name":"jaredwray","email":"me@jaredwray.com"},"directories":{},"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_1.1.0_1753931734761_0.42123187825475084"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-07-31T03:15:34.977Z","publish_time":1753931734977,"_source_registry_name":"default","_cnpm_publish_time":1753931734977},"1.0.0":{"name":"@cacheable/memoize","version":"1.0.0","description":"Memoization utilities for cacheable","type":"module","main":"./dist/index.cjs","module":"./dist/index.js","types":"./dist/index.d.ts","exports":{".":{"require":"./dist/index.cjs","import":"./dist/index.js"}},"repository":{"type":"git","url":"git+https://github.com/jaredwray/cacheable.git","directory":"packages/memoize"},"author":{"name":"Jared Wray","email":"me@jaredwray.com"},"license":"MIT","private":false,"devDependencies":{"@faker-js/faker":"^9.9.0","@keyv/redis":"^5.0.0","@keyv/valkey":"^1.0.7","@types/eslint":"^9.6.1","@types/node":"^24.1.0","@vitest/coverage-v8":"^3.2.4","lru-cache":"^11.1.0","rimraf":"^6.0.1","tsup":"^8.5.0","typescript":"^5.8.3","vitest":"^3.2.4","xo":"^1.2.1","cacheable":"^1.10.3"},"keywords":["cacheable","caching","keyv","memoization","memoize","performance","wrapper","wrap","getOrSet"],"dependencies":{"@cacheable/utils":"^1.0.0"},"scripts":{"build":"rimraf ./dist && tsup src/index.ts --format cjs,esm --dts --clean","prepublish":"pnpm build","test":"xo --fix && vitest run --coverage","test:ci":"xo && vitest run --coverage","clean":"rimraf ./dist ./coverage ./node_modules"},"_id":"@cacheable/memoize@1.0.0","bugs":{"url":"https://github.com/jaredwray/cacheable/issues"},"homepage":"https://github.com/jaredwray/cacheable#readme","_integrity":"sha512-OoBMhSiCaaJpyl39la6iAjLuTTZJHPeMvOPDAcqZbixLtiPm5G9/XHX3QPAd6ArWmiTfQRJ8b3sZh3aPTgPe9g==","_resolved":"/private/var/folders/q4/x95kq1ln6cd7rrnct9cby32r0000gn/T/f318e945b6b640caf765fb37d7d9207a/cacheable-memoize-1.0.0.tgz","_from":"file:cacheable-memoize-1.0.0.tgz","_nodeVersion":"22.12.0","_npmVersion":"11.4.2","dist":{"shasum":"947433156872329bd275775b80f9d700f308ec79","size":4913,"noattachment":false,"key":"/@cacheable/memoize/-/@cacheable/memoize-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/@cacheable/memoize/download/@cacheable/memoize-1.0.0.tgz"},"_npmUser":{"name":"jaredwray","email":"me@jaredwray.com"},"directories":{},"maintainers":[{"name":"jaredwray","email":"me@jaredwray.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/memoize_1.0.0_1753492006437_0.6227610027078867"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2025-07-26T01:06:46.609Z","publish_time":1753492006609,"_source_registry_name":"default","_cnpm_publish_time":1753492006609}},"readme":"[<img align=\"center\" src=\"https://cacheable.org/logo.svg\" alt=\"Cacheable\" />](https://github.com/jaredwray/cacheable)\n\n> Memoization utilities for Cacheable\n\n[![codecov](https://codecov.io/gh/jaredwray/cacheable/graph/badge.svg?token=lWZ9OBQ7GM)](https://codecov.io/gh/jaredwray/cacheable)\n[![tests](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml/badge.svg)](https://github.com/jaredwray/cacheable/actions/workflows/tests.yml)\n[![npm](https://img.shields.io/npm/dm/@cacheable/memoize.svg)](https://www.npmjs.com/package/@cacheable/memoize)\n[![npm](https://img.shields.io/npm/v/@cacheable/memoize)](https://www.npmjs.com/package/@cacheable/memoize)\n[![license](https://img.shields.io/github/license/jaredwray/cacheable)](https://github.com/jaredwray/cacheable/blob/main/LICENSE)\n\n# Table of Contents\n- [Getting Started](#getting-started)\n- [Wrap / Memoization for Sync and Async Functions](#wrap--memoization-for-sync-and-async-functions)\n- [Get Or Set Memoization Function](#get-or-set-memoization-function)\n- [How to Contribute](#how-to-contribute)\n- [License and Copyright](#license-and-copyright)\n\n# Getting Started\n\nTo get started with `@cacheable/memoize`, install the package:\n\n```bash\nnpm install @cacheable/memoize\n```\n\n# Wrap / Memoization for Sync and Async Functions\n\nThe `@cacheable/memoize` package provides two main functions: `wrap` and `wrapSync`. These functions are used to memoize asynchronous and synchronous functions, respectively.\n\n```javascript\nimport { Cacheable } from 'cacheable';\nconst asyncFunction = async (value: number) => {\n  return Math.random() * value;\n};\n\nconst cache = new Cacheable();\nconst options = {\n  ttl: '1h', // 1 hour\n  keyPrefix: 'p1', // key prefix. This is used if you have multiple functions and need to set a unique prefix.\n  cache,\n}\nconst wrappedFunction = wrap(asyncFunction, options);\nconsole.log(await wrappedFunction(2)); // 4\nconsole.log(await wrappedFunction(2)); // 4 from cache\n```\nWith `wrap` we have also included stampede protection so that a `Promise` based call will only be called once if multiple requests of the same are executed at the same time. Here is an example of how to test for stampede protection:\n  \n```javascript\nimport { Cacheable } from 'cacheable';\nconst asyncFunction = async (value: number) => {\n  return value;\n};\n\nconst cache = new Cacheable();\nconst options = {\n  ttl: '1h', // 1 hour\n  keyPrefix: 'p1', // key prefix. This is used if you have multiple functions and need to set a unique prefix.\n  cache,\n}\n\nconst wrappedFunction = wrap(asyncFunction, options);\nconst promises = [];\nfor (let i = 0; i < 10; i++) {\n  promises.push(wrappedFunction(i));\n}\n\nconst results = await Promise.all(promises); // all results should be the same\n\nconsole.log(results); // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n```\n\nIn this example we are wrapping an `async` function in a cache with a `ttl` of `1 hour`. This will cache the result of the function for `1 hour` and then expire the value. You can also wrap a `sync` function in a cache:\n\n```javascript\nimport { CacheableMemory } from 'cacheable';\nconst syncFunction = (value: number) => {\n  return value * 2;\n};\n\nconst cache = new CacheableMemory();\nconst wrappedFunction = wrap(syncFunction, { ttl: '1h', key: 'syncFunction', cache });\nconsole.log(wrappedFunction(2)); // 4\nconsole.log(wrappedFunction(2)); // 4 from cache\n```\n\nIn this example we are wrapping a `sync` function in a cache with a `ttl` of `1 hour`. This will cache the result of the function for `1 hour` and then expire the value. You can also set the `key` property in the `wrap()` options to set a custom key for the cache.\n\nWhen an error occurs in the function it will not cache the value and will return the error. This is useful if you want to cache the results of a function but not cache the error. If you want it to cache the error you can set the `cacheError` property to `true` in the `wrap()` options. This is disabled by default.\n\n```javascript\nimport { CacheableMemory } from 'cacheable';\nconst syncFunction = (value: number) => {\n  throw new Error('error');\n};\n\nconst cache = new CacheableMemory();\nconst wrappedFunction = wrap(syncFunction, { ttl: '1h', key: 'syncFunction', cacheError: true, cache });\nconsole.log(wrappedFunction()); // error\nconsole.log(wrappedFunction()); // error from cache\n```\n\nIf you would like to generate your own key for the wrapped function you can set the `createKey` property in the `wrap()` options. This is useful if you want to generate a key based on the arguments of the function or any other criteria.\n\n```javascript\n  const cache = new Cacheable();\n  const options: WrapOptions = {\n    cache,\n    keyPrefix: 'test',\n    createKey: (function_, arguments_, options: WrapOptions) => `customKey:${options?.keyPrefix}:${arguments_[0]}`,\n  };\n\n  const wrapped = wrap((argument: string) => `Result for ${argument}`, options);\n\n  const result1 = await wrapped('arg1');\n  const result2 = await wrapped('arg1'); // Should hit the cache\n\n  console.log(result1); // Result for arg1\n  console.log(result2); // Result for arg1 (from cache)\n```\n\nWe will pass in the `function` that is being wrapped, the `arguments` passed to the function, and the `options` used to wrap the function. You can then use these to generate a custom key for the cache.\n\n# Get Or Set Memoization Function\n\nThe `getOrSet` method provides a convenient way to implement the cache-aside pattern. It attempts to retrieve a value from cache, and if not found, calls the provided function to compute the value and store it in cache before returning it. Here are the options:\n\n```typescript\nexport type GetOrSetFunctionOptions = {\n\tttl?: number | string;\n\tcacheErrors?: boolean;\n\tthrowErrors?: boolean;\n};\n```\n\nHere is an example of how to use the `getOrSet` method:\n\n```javascript\nimport { Cacheable } from 'cacheable';\nconst cache = new Cacheable();\n// Use getOrSet to fetch user data\nconst function_ = async () => Math.random() * 100;\nconst value = await getOrSet('randomValue', function_, { ttl: '1h', cache });\nconsole.log(value); // e.g. 42.123456789\n```\n\nYou can also use a function to compute the key for the function:\n\n```javascript\nimport { Cacheable, GetOrSetOptions } from 'cacheable';\nconst cache = new Cacheable();\n\n// Function to generate a key based on options\nconst generateKey = (options?: GetOrSetOptions) => {\n  return `custom_key_:${options?.cacheId || 'default'}`;\n};\n\nconst function_ = async () => Math.random() * 100;\nconst value = await getOrSet(generateKey(), function_, { ttl: '1h', cache });\n```\n\n# How to Contribute\n\nYou can contribute by forking the repo and submitting a pull request. Please make sure to add tests and update the documentation. To learn more about how to contribute go to our main README [https://github.com/jaredwray/cacheable](https://github.com/jaredwray/cacheable). This will talk about how to `Open a Pull Request`, `Ask a Question`, or `Post an Issue`.\n\n# License and Copyright\n[MIT © Jared Wray](./LICENSE)\n","_attachments":{},"homepage":"https://github.com/jaredwray/cacheable#readme","bugs":{"url":"https://github.com/jaredwray/cacheable/issues"},"license":"MIT"}