{"id":24956960,"url":"https://github.com/prodo-dev/js-caching","last_synced_at":"2025-03-28T20:38:31.218Z","repository":{"id":98996831,"uuid":"131438602","full_name":"prodo-dev/js-caching","owner":"prodo-dev","description":"JS Caching Utility","archived":false,"fork":false,"pushed_at":"2018-04-30T10:29:15.000Z","size":41,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-03T06:42:00.093Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prodo-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-28T19:36:14.000Z","updated_at":"2018-04-30T10:29:17.000Z","dependencies_parsed_at":"2023-05-01T05:32:24.024Z","dependency_job_id":null,"html_url":"https://github.com/prodo-dev/js-caching","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/prodo-dev%2Fjs-caching","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prodo-dev%2Fjs-caching/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prodo-dev%2Fjs-caching/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prodo-dev%2Fjs-caching/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prodo-dev","download_url":"https://codeload.github.com/prodo-dev/js-caching/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246100419,"owners_count":20723466,"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-02-03T06:41:47.118Z","updated_at":"2025-03-28T20:38:31.212Z","avatar_url":"https://github.com/prodo-dev.png","language":"JavaScript","readme":"# JS ASync\n\nUtility library for caching.\n\n## Usage\n\nA `Cache` wraps a function (both synchronous or asynchronous) to ensure that results are cached for later use.\n\nTo create a `Cache` object, use the `createCache` method:\n\n```javascript\nimport {createCache} from \"@prodo-ai/js-caching\";\n\nconst cache = createCache(\n  key =\u003e key,\n  async key =\u003e ({\n    value: await service.getValue(key),\n  }),\n);\n\nconst firstResult = cache.get(\"first\");       // Will use `service`.\nconst secondResult = cache.get(\"second\");     // Will use `service`.\nconst firstResultAgain = cache.get(\"first\");  // Will use the cache.\n```\n\nThe first parameter `hasher` is used to create a unique string for each key. This is the hash that uniquely identifies the result in the cache.\n\nThe second parameter `create` is the factory method to fetch results that don't exist in the hash. The return value of this function should be an object containing the following properties:\n\n* `value` - the value to store in the cache\n* `destroy` - an optional callback that will destroy and clean-up the cached value.\n\nFor example, if your cache stores references to information stored on disk, your `destroy` function would delete the information from the disk. This function is only called when the value is removed from the cache.\n\nYou can also pass additional options as a third parameter to gain greater control over the cache:\n\n* `size` - the number of results to store in the cache.\n* `expiry` - a `Duration` (see [@prodo-ai/js-timing](https://github.com/prodo-ai/js-timing)) that determines how long to keep a result for.\n* `cleanupInterval` - a `Duration` that specifies how frequently to run garbage collection.\n\n### memoize\n\nThe most simple use of a cache is to memoize a function. This can be done using the `memoize` helper:\n\n```javascript\nimport {memoize} from \"@prodo-ai/js-caching\";\n\nconst func = (args) =\u003e {/*...*/};\nconst hasher = (args) =\u003e {/* create a hash of the args */};\nconst memoizedFunc = memoize(hasher, func);\n\nconst firstResult = memoizedFunc(\"first\");\nconst secondResult = memoizedFunc(\"second\");\nconst firstResultAgain = memoizedFunc(\"first\");  // Will use the cache.\n```\n\n## Information\n\nOwner: Prodo Tech Ltd\n\nMaintainer: [tdawes](https://github.com/tdawes)\n\nLicense: UNLICENSED (for now)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprodo-dev%2Fjs-caching","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprodo-dev%2Fjs-caching","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprodo-dev%2Fjs-caching/lists"}