{"id":24711934,"url":"https://github.com/borgar/localcache","last_synced_at":"2026-05-19T09:32:35.692Z","repository":{"id":42848473,"uuid":"262154422","full_name":"borgar/localcache","owner":"borgar","description":"Simple caching/memo utility for JavaScript functions","archived":false,"fork":false,"pushed_at":"2023-01-06T05:16:22.000Z","size":1452,"stargazers_count":0,"open_issues_count":15,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-26T18:05:51.209Z","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/borgar.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2020-05-07T20:42:35.000Z","updated_at":"2020-09-11T09:07:08.000Z","dependencies_parsed_at":"2023-02-05T09:50:10.573Z","dependency_job_id":null,"html_url":"https://github.com/borgar/localcache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borgar%2Flocalcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borgar%2Flocalcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borgar%2Flocalcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/borgar%2Flocalcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/borgar","download_url":"https://codeload.github.com/borgar/localcache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244918582,"owners_count":20531686,"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":"2025-01-27T07:16:37.053Z","updated_at":"2026-05-19T09:32:35.650Z","avatar_url":"https://github.com/borgar.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# localcache\n\nPromise compatible function memoizer. Requires Promise and Map to be available.\n\n\n## Install\n\n```\n$ npm install @borgar/localcache\n```\n\n\n## Usage\n\n```js\nimport cache from '@borgar/localcache';\n\nconst fetchUserInfo = cache(function (username) {\n  return fetch(\"https://api.example.com/userinfo/\" + username)\n    .then(response =\u003e {\n      if (!response.ok) { throw new Error(response.status); }\n      return response.json();\n    });\n}, 60);\n```\n\nA useful feature of localcache is that if the wrapped function returns a promise, all cached responses will be the same pending promise instance. That means that a web application may request the same URL multiple times and when the data arrives, all awaiting will get the result concurrently.\n\n\n## API\n\n### localcache(function, options?)\n\n#### input\n\nType: `function`\n\nThe function to wrapped.\n\n#### options\n\nType: `object`\nDefault: `{ lifetime: 900, idresolve: \u003cdefaultIdresolver\u003e, bootstrap: null }`\n\nThe time in seconds the result should be cached. By default localcache hangs on to old data for 15 minutes.\n\n\n##### options.lifetime\n\nType: `number`\nDefault: `900`\n\nThe time in seconds the result should be cached. By default localcache hangs on to old data for 15 minutes.\n\n\n##### options.bootstrap\n\nType: `array | null`\nDefault: `null`\n\nAn array of objects containing keys: id, result.  id is the cache key which should match the argument of the cached function \u0026 result is the cached value. e.g.\n\n```js\nimport cache from '@borgar/localcache';\n\nconst bootstrap = [{ id: '', result: 5, { id: 'some-other-arg', result: 2} ]\nconst functionToReturnCachedValue = localCache(function functionToCache (arg) { return 5 }, { bootstrap });\n\nfunctionToReturnCachedValue()\n// returns 5\nturnCachedValue('some-other-arg')\n// returns 2\nturnCachedValue('not-bootsrapped')\n// returns 5\n```\n\n##### options.idresolver\n\nType: `function`\n\nA function which identifies the input. By default localcache will simply string join the arguments which means that wrapping functions that accept input requires you to create your own identities to key them on.\n\nIt may be as simple as this:\n\n```js\nimport cache from '@borgar/localcache';\n\nfunction loadFile (fileinfo) {\n  // load a file named fileinfo.filename from fileinfo.path\n}\n\nconst maybeLoadFile = localCache(loadFile, { lifetime: 60, idresolver: fileinfo =\u003e {\n  return fileinfo.path + '/' + fileinfo.filename;\n}});\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fborgar%2Flocalcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fborgar%2Flocalcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fborgar%2Flocalcache/lists"}