{"id":20098584,"url":"https://github.com/dishait/file-computed","last_synced_at":"2025-10-03T23:48:01.670Z","repository":{"id":57701196,"uuid":"494724662","full_name":"dishait/file-computed","owner":"dishait","description":"文件型计算属性，当且仅当文件变化时才重新做计算","archived":false,"fork":false,"pushed_at":"2024-02-28T05:34:53.000Z","size":2608,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-14T16:14:20.847Z","etag":null,"topics":["cache","computed","easy","fs"],"latest_commit_sha":null,"homepage":"","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/dishait.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}},"created_at":"2022-05-21T08:27:47.000Z","updated_at":"2023-02-02T20:50:05.000Z","dependencies_parsed_at":"2023-02-16T16:46:18.129Z","dependency_job_id":"5df07c23-7dce-43af-8527-3484eefd6acd","html_url":"https://github.com/dishait/file-computed","commit_stats":{"total_commits":88,"total_committers":1,"mean_commits":88.0,"dds":0.0,"last_synced_commit":"9fcbe23108302bdba5b27544efecab57e14495df"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":"markthree/node-lib-starter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dishait%2Ffile-computed","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dishait%2Ffile-computed/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dishait%2Ffile-computed/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dishait%2Ffile-computed/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dishait","download_url":"https://codeload.github.com/dishait/file-computed/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248914115,"owners_count":21182359,"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":["cache","computed","easy","fs"],"created_at":"2024-11-13T17:05:58.226Z","updated_at":"2025-10-03T23:47:56.625Z","avatar_url":"https://github.com/dishait.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n    \u003cimg width=\"100%\" height=\"100%\" src=\"./file-computed.gif\" /\u003e\n    \u003ch1\u003efile-computed\u003c/h1\u003e\n    \u003cp\u003e文件型计算属性，当且仅当文件内容发生变化时才重新做计算\u003c/p\u003e\n\u003c/div\u003e\n\n\u003cbr /\u003e\n\n## 动机\n\n有时候我们的计算在特定目标文件不变时结果是一样的，当计算特别耗时复杂时可以通过本地文件缓存的方式快速得到原先的结果，而不需要每一次都进行该计算，以此提高运算效率。\n\n\u003cbr /\u003e\n\u003cbr /\u003e\n\n## 使用\n\n### 安装\n\n```shell\npnpm i file-computed\n```\n\n\u003cbr /\u003e\n\n### 基础\n\n```ts\nimport { createFsComputed } from 'file-computed'\n\nconst fsComputed = createFsComputed()\n\nconst result = await fsComputed('package.json', () =\u003e {\n\t/** 模拟复杂计算，只会跑一次，后边会直接获取缓存中的结果 */\n\tlet n = 0\n\tlet t = 10000\n\twhile (t--) {\n\t\tn++\n\t}\n\treturn n\n})\n\nresult // 10000\n```\n\n\u003cbr /\u003e\n\n### 缓存路径\n\n```ts\nimport { createFsComputed } from 'file-computed'\n\nconst fsComputed = createFsComputed({\n\tcachePath: 'temp' // 默认为最近 node_modules 的 .cache/.file-computed\n})\n```\n\n\u003cbr /\u003e\n\n### 同步\n\n```ts\nimport { createFsComputedSync } from 'file-computed'\n\nconst fsComputed = createFsComputedSync()\n\nconst result = fsComputed('package.json', () =\u003e {\n\t/** 模拟复杂计算，只会跑一次，后边会直接获取缓存中的结果 */\n\tlet n = 0\n\tlet t = 10000\n\twhile (t--) {\n\t\tn++\n\t}\n\treturn n\n})\n\nresult // 10000\n```\n\n\u003cbr /\u003e\n\n### 流\n\n适合大型缓存\n\n```ts\nimport { createFsComputedWithStream } from 'file-computed'\n\nconst fsComputed = createFsComputedWithStream()\n\nconst result = await fsComputed('package.json', () =\u003e {\n\t/** 模拟复杂计算，只会跑一次，后边会直接获取缓存中的结果 */\n\tlet n = 0\n\tlet t = 10000\n\twhile (t--) {\n\t\tn++\n\t}\n\treturn n\n})\n\nresult // 10000\n```\n\n#### 后置写\n\n开启 `post`，结果将优先返回，速度更快\n\n```ts\nimport { createFsComputedWithStream } from 'file-computed'\n\nconst fsComputed = createFsComputedWithStream({\n\tpost: true\n})\n\nconst result = await fsComputed('package.json', () =\u003e {\n\tlet n = 0\n\tlet t = 10000\n\twhile (t--) {\n\t\tn++\n\t}\n\treturn n\n})\n\nresult // 写被后置了，结果 10000 优先返回\n```\n\n因为写被后置了，所以下次计算前，缓存与否是不确定的\n\n\u003cbr /\u003e\n\u003cbr /\u003e\n\n## License\n\nMade with [markthree](https://github.com/markthree)\n\nPublished under [MIT License](./LICENSE).\n\n\u003cbr /\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdishait%2Ffile-computed","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdishait%2Ffile-computed","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdishait%2Ffile-computed/lists"}