{"id":27873837,"url":"https://github.com/vvscode/file-memoize","last_synced_at":"2025-10-13T20:04:00.651Z","repository":{"id":288294516,"uuid":"967502985","full_name":"vvscode/file-memoize","owner":"vvscode","description":"A higher-order function in Node.js that caches results to JSON files in the OS temporary directory","archived":false,"fork":false,"pushed_at":"2025-04-16T16:48:25.000Z","size":104,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-05T01:15:49.606Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/vvscode.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-16T14:53:23.000Z","updated_at":"2025-04-16T16:47:33.000Z","dependencies_parsed_at":"2025-04-20T06:31:24.862Z","dependency_job_id":null,"html_url":"https://github.com/vvscode/file-memoize","commit_stats":null,"previous_names":["vvscode/file-memoize"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vvscode%2Ffile-memoize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vvscode%2Ffile-memoize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vvscode%2Ffile-memoize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vvscode%2Ffile-memoize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vvscode","download_url":"https://codeload.github.com/vvscode/file-memoize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252420921,"owners_count":21745154,"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-05-05T01:15:53.633Z","updated_at":"2025-10-13T20:03:55.609Z","avatar_url":"https://github.com/vvscode.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# file-memoize\n\n\n[![npm version](https://badge.fury.io/js/file-memoize.svg)](https://www.npmjs.com/package/file-memoize)\n[![Deploy](https://github.com/vvscode/file-memoize/workflows/build/badge.svg)](https://github.com/vvscode/file-memoize/actions)\n\n\nA simple and efficient TypeScript higher-order function to cache asynchronous function results to JSON files in the OS temporary directory. Designed for Node.js, it persists cached results across runs using parameter-based keys and customizable cache file naming.\n\n---\n\n## Features\n\n- Cache async function results to JSON files on disk\n- Uses OS temporary directory for cache storage\n- Supports single string or multiple parameters as cache keys\n- Cache file named using `CI_COMMIT_SHA` environment variable and function name\n- Automatically loads and saves cache on function calls\n- Written in TypeScript with full type safety\n\n---\n\n## Installation\n\n```bash\nnpm install file-memoize\n# or\nyarn add file-memoize\n```\n\n---\n\n## Usage\n\n```typescript\nimport { fileMemoize } from 'file-memoize';\n\n// Example async function\nasync function fetchData(param: string): Promise {\n  // Simulate an async operation\n  return `Data for ${param}`;\n}\n\n// Create a memoized version of the function\nconst memoizedFetchData = fileMemoize(fetchData, 'fetchData');\n\nasync function run() {\n  // First call: runs the original function and caches result\n  console.log(await memoizedFetchData('books')); // Output: Data for books\n\n  // Second call with same param: returns cached result instantly\n  console.log(await memoizedFetchData('books')); // Output: Data for books\n}\n\nrun();\n```\n\n---\n\n## API\n\n### `fileMemoize(fn, functionName, getFileName?)`\n\nCreates a memoized version of an async function `fn` that caches results to a JSON file.\n\n| Parameter     | Type                               | Description                                                                                          |\n|---------------|----------------------------------|--------------------------------------------------------------------------------------------------|\n| `fn`          | `(...args: any[]) =\u003e Promise` | The async function to memoize.                                                                     |\n| `functionName`| `string`                         | Name of the function, used in cache file naming.                                                  |\n| `getFileName` | `() =\u003e string` (optional)        | Optional function to customize cache file path. Defaults to `${tmpdir}/${CI_COMMIT_SHA}_${functionName}.json`. |\n\n**Returns:** A function with the same signature as `fn` that caches results on disk.\n\n---\n\n## How it works\n\n- On first invocation, the cache file is loaded if it exists.\n- Function call arguments are serialized to form cache keys:\n  - If a single string argument is passed, it is used directly as the key.\n  - Otherwise, the arguments array is JSON-stringified.\n- If a cached result exists for the key, it is returned immediately.\n- Otherwise, the original function is called, the result cached, and saved to file.\n\n---\n\n## Environment Variables\n\n- `CI_COMMIT_SHA`: Used as part of the cache filename to isolate cache per commit or environment. Defaults to `'default'` if unset.\n\n---\n\n## Testing\n\nThe package includes Jest tests covering:\n\n- Cache hit and miss behavior\n- Cache persistence to disk\n- Handling of multiple parameters\n- Graceful error handling on file read/write failures\n\nRun tests with:\n\n```bash\nnpm test\n```\n\n---\n\n## Contributing\n\nContributions, issues, and feature requests are welcome! Feel free to open issues or pull requests.\n\n---\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvvscode%2Ffile-memoize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvvscode%2Ffile-memoize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvvscode%2Ffile-memoize/lists"}