{"id":13454448,"url":"https://github.com/nodeca/promise-memoize","last_synced_at":"2025-05-08T23:44:15.293Z","repository":{"id":57331366,"uuid":"59306435","full_name":"nodeca/promise-memoize","owner":"nodeca","description":"Memoize promise-returning functions. Includes cache expire and prefetch.","archived":false,"fork":false,"pushed_at":"2018-12-05T12:15:57.000Z","size":20,"stargazers_count":62,"open_issues_count":1,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-26T21:03:14.164Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nodeca.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-20T15:41:20.000Z","updated_at":"2024-12-31T09:50:22.000Z","dependencies_parsed_at":"2022-08-29T05:10:47.050Z","dependency_job_id":null,"html_url":"https://github.com/nodeca/promise-memoize","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodeca%2Fpromise-memoize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodeca%2Fpromise-memoize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodeca%2Fpromise-memoize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodeca%2Fpromise-memoize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nodeca","download_url":"https://codeload.github.com/nodeca/promise-memoize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252732020,"owners_count":21795584,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T08:00:54.157Z","updated_at":"2025-05-08T23:44:15.274Z","avatar_url":"https://github.com/nodeca.png","language":"JavaScript","funding_links":[],"categories":["Packages","Repository","包","Control flow"],"sub_categories":["Control flow","流程控制","控制流"],"readme":"# promise-memoize\n\n[![Build Status](https://img.shields.io/travis/nodeca/promise-memoize/master.svg?style=flat)](https://travis-ci.org/nodeca/promise-memoize)\n[![NPM version](https://img.shields.io/npm/v/promise-memoize.svg?style=flat)](https://www.npmjs.org/package/promise-memoize)\n[![Coverage Status](https://coveralls.io/repos/github/nodeca/promise-memoize/badge.svg?branch=master)](https://coveralls.io/github/nodeca/promise-memoize?branch=master)\n\n\u003e Memoize promise-returning functions. Includes cache expire and prefetch.\n\n- When data expire mode enabled, new values are fetched in advance. Cache\n  will be always valid, without \"gaps\".\n  - Prefetch happens only for items in use. Inactive ones will be GC-ed as usual.\n- Errors are not cached\n  - You still can enable cache with separate expire time for errors, to avoid\n    specific peak loads. For example, set 120s for good result and 1s on fail.\n\n\n## Install\n\n```bash\nnpm install promise-memoize --save\n```\n\n(\\*) IE9 and below will require [setTimeout polyfill](https://developer.mozilla.org/docs/Web/API/WindowTimers/setTimeout)\nfor correct work.\n\n\n## Usage example\n\n```js\n// Pseudo code\nlet db = require('mongoose').createConnection('mongodb://localhost/forum');\n\nfunction lastPosts(limit) {\n  return db.model('Post').find().limit(limit).orderBy('-_id').lean(true).exec(); // \u003c- Promise\n}\n\nlet cachedLastPosts = require('promise-memoize')(lastPosts, { maxAge: 60000 });\n\n// Later...\ncachedLastPosts(10).then(posts =\u003e console.log(posts));\n```\n\n## API\n\n### promiseMemoize(fn [, options]) -\u003e memoizedFn\n\nMemoize function `fn`.\n\n - **fn(params...)** — function, returning a promise (or any \"thenable\").\n   It can have any number of arguments, but arguments should be uniquely\n   castable to strings (see below).\n - **options** — options for memoization (optional)\n   - **maxAge** — an amount of milliseconds it should cache resolved\n     values for (default: `Infinity`, i.e. cache forever).\n   - **maxErrorAge** — an amount of milliseconds it should cache\n     rejected values for (default: `0`, i.e. don't cache).\n   - **resolve** — serialiser to build unique key from `fn` arguments.\n     (default: `simple`). Possible values:\n     - `simple` (string) — convert each param to string \u0026 join those.\n     - `json` (string) — JSON.stringify each param \u0026 join results.\n     - function(Array) — custom function, with `fn` params as array on input\n     - `[ String, Boolean, 'json', function ]` — array with custom functions,\n       specific for each `fn` param position (text shortcuts as above are allowed).\n\nReturn value is a function with the same signature as *fn*.\n\n**Note. How prefetch works.**\n\nIf `maxAge` used and request to cached data happens after `0.7 * maxAge` time, then:\n\n- cached data returned\n- `fn` call is executed in parallel\n- cached data will be substituted with new one on success, timeouts will be extended.\n\nSo your application will not have to wait for data fetch after cache expire.\n\n\n### memoizedFn(params...) -\u003e promise\n\nReturns result as cached promise (errors are not cached by default). If `maxAge`\nused, tries to prefetch new value before expire to replace cache transparently.\n\n\n### memoizedFn.clear()\n\nRemove all cached data.\n\n\n## License\n\n[MIT](https://github.com/nodeca/promise-memoize/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodeca%2Fpromise-memoize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnodeca%2Fpromise-memoize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodeca%2Fpromise-memoize/lists"}