{"_id":"@isaacs/cached","_rev":"1760173","name":"@isaacs/cached","description":"Cache a function that takes a path as an argument, expiring whenever the file mtime changes","dist-tags":{"latest":"1.0.1"},"maintainers":[{"name":"isaacs","email":"i@izs.me"}],"time":{"modified":"2026-11-23T18:59:28.000Z","created":"2023-10-30T22:34:39.699Z","1.0.1":"2023-11-04T03:11:50.799Z","1.0.0":"2023-10-30T22:34:39.699Z"},"users":{},"versions":{"1.0.1":{"name":"@isaacs/cached","description":"Cache a function that takes a path as an argument, expiring whenever the file mtime changes","license":"BlueOak-1.0.0","type":"module","scripts":{"preversion":"npm test","postversion":"npm publish","prepublishOnly":"git push origin --follow-tags","prepare":"tshy","pretest":"npm run prepare","presnap":"npm run prepare","test":"tap","snap":"tap","format":"prettier --write . --loglevel warn --cache","typedoc":"typedoc"},"prettier":{"semi":false,"printWidth":70,"tabWidth":2,"useTabs":false,"singleQuote":true,"jsxSingleQuote":false,"bracketSameLine":true,"arrowParens":"avoid","endOfLine":"lf"},"devDependencies":{"@types/node":"^20.8.9","prettier":"^2.8.8","tap":"^18.5.3","tshy":"^1.7.0","typedoc":"^0.25.3"},"tshy":{"exports":{"./package.json":"./package.json",".":"./src/index.ts"}},"exports":{"./package.json":"./package.json",".":{"import":{"types":"./dist/esm/index.d.ts","default":"./dist/esm/index.js"},"require":{"types":"./dist/commonjs/index.d.ts","default":"./dist/commonjs/index.js"}}},"main":"./dist/commonjs/index.js","types":"./dist/commonjs/index.d.ts","dependencies":{"@isaacs/catcher":"^1.0.0"},"version":"1.0.1","_id":"@isaacs/cached@1.0.1","gitHead":"b9621c794d8deecd1559d946eaa36ce8cfa8a9a9","_nodeVersion":"20.8.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-7kGcJ9Hc1f4qpTApWz3swxbF9Qv1NF/GxuPtXeTptbsgvJIoufSd0h854Nq/2bw80F5C1onsFgEI05l+q0e4vw==","shasum":"b6ad07c346f843fb3f117a0f3401ea8b7f7d4eea","tarball":"http://registry-npm.ywwl.com/@isaacs/cached/download/@isaacs/cached-1.0.1.tgz","fileCount":13,"unpackedSize":26437,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBG63BW40UCBO04dvyUfmSDcCAf6FlVbd8kP9yg+MJi+AiA5pbtKJ+k8FASofCKbfpjJlN0oAePZMV8X59dbBGL+6g=="}],"size":5501,"noattachment":false,"key":"/@isaacs/cached/-/@isaacs/cached-1.0.1.tgz"},"_npmUser":{"name":"isaacs","email":"i@izs.me"},"directories":{},"maintainers":[{"name":"isaacs","email":"i@izs.me"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/cached_1.0.1_1699067510569_0.5867286482324654"},"_hasShrinkwrap":false,"publish_time":1699067510799,"_cnpm_publish_time":1699067510799},"1.0.0":{"name":"@isaacs/cached","description":"Cache a function that takes a path as an argument, expiring whenever the file mtime changes","license":"BlueOak-1.0.0","type":"module","scripts":{"preversion":"npm test","postversion":"npm publish","prepublishOnly":"git push origin --follow-tags","prepare":"tshy","pretest":"npm run prepare","presnap":"npm run prepare","test":"tap","snap":"tap","format":"prettier --write . --loglevel warn --cache","typedoc":"typedoc"},"prettier":{"semi":false,"printWidth":70,"tabWidth":2,"useTabs":false,"singleQuote":true,"jsxSingleQuote":false,"bracketSameLine":true,"arrowParens":"avoid","endOfLine":"lf"},"devDependencies":{"@types/node":"^20.8.9","prettier":"^2.8.8","tap":"^18.5.3","tshy":"^1.7.0","typedoc":"^0.25.3"},"tshy":{"exports":{"./package.json":"./package.json",".":"./src/index.ts"}},"exports":{"./package.json":"./package.json",".":{"import":{"types":"./dist/esm/index.d.ts","default":"./dist/esm/index.js"},"require":{"types":"./dist/commonjs/index.d.ts","default":"./dist/commonjs/index.js"}}},"main":"./dist/commonjs/index.js","types":"./dist/commonjs/index.d.ts","dependencies":{"@isaacs/catcher":"^1.0.0"},"version":"1.0.0","_id":"@isaacs/cached@1.0.0","gitHead":"e564e98c7704e0b71d5dd29fefd211a508a3e103","_nodeVersion":"20.8.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-sOcqfy70fGSNb3lSDvTG7Q/hEwyezucqz+7ZFhK8MphkcT0HL4S1pMNfml4GhBv4KdyFd4WkGAUaCxTY/ziouA==","shasum":"8a8d6a10ebbad602ac9fc38507913f44301cc920","tarball":"http://registry-npm.ywwl.com/@isaacs/cached/download/@isaacs/cached-1.0.0.tgz","fileCount":13,"unpackedSize":20805,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIACLnHSps7fUCTs++ZCvjM8FG15Xa8iBmPF2QdS/zVoIAiAMAnFKx5rM5Hj0HDNyrxVHS9cBQCXI4kNGP2rX5Zq9Sg=="}],"size":4802,"noattachment":false,"key":"/@isaacs/cached/-/@isaacs/cached-1.0.0.tgz"},"_npmUser":{"name":"isaacs","email":"i@izs.me"},"directories":{},"maintainers":[{"name":"isaacs","email":"i@izs.me"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/cached_1.0.0_1698705279483_0.9946091808900932"},"_hasShrinkwrap":false,"publish_time":1698705279699,"_cnpm_publish_time":1698705279699}},"readme":"# @isaacs/cached\n\nCache the results of a function based on a `path` argument, and\nclear the cache whenever the mtime of the path changes.\n\n## USAGE\n\n```js\nimport { cachedMtime } from '@isaacs/cached'\nimport { writeFile, readFile } from 'node:fs/promises'\n\nconst readFileCached = cachedMtime(readFile)\n\n// first time, actually reads the file\nconst results = await readFileCached('file.txt')\n// second time, just serve from cache\nconst cachedResults = await readFileCached('file.txt')\n\n// file changes!\nawait writeFile('file.txt', 'new contents')\nconst cacheBusted = await readFileCached('file.txt')\n```\n\nThe `cached` method is the same thing, but without the mtime\nstuff, so you'd have to expire the cache yourself if you want to\ndo that.\n\n```js\nimport { cached } from '@isaacs/cached'\nimport { writeFile, readFile } from 'node:fs/promises'\n\nconst readFileCached = cached(readFile)\n\nawait writeFile('file.txt', 'some contents')\n\n// first time, actually reads the file\nconst results = await readFileCached('file.txt')\n// second time, just serve from cache\nconst cachedResults = await readFileCached('file.txt')\n\n// file changes! but this is ignored, because we're using\n// cached(), not cachedMtime()\nawait writeFile('file.txt', 'new contents')\nconst cacheNotBusted = await readFileCached('file.txt')\nassert.equal(cacheNotBusted, 'some contents')\n```\n\nYou can supply your own cache to the method if you'd like to use\nan [LRU](https://github.com/isaacs/node-lru-cache) or something.\nIt must implement get, set, and has, at minimum.\n\n```js\nimport { cached } from '@isaacs/cached'\nconst map1 = new Map([\n  [1, 11],\n  [2, 22],\n])\nconst map2 = new Map([\n  [3, 13],\n  [4, 14],\n])\nconst myCache = {\n  get: key => map1.get(key) || map2.get(key),\n  set: (key, value) => map1.set(key),\n  has: key => map1.has(key) || map2.has(key),\n  delete: key => map1.delete(key) || map2.delete(key),\n}\nconst cachedFunction = cached(key => key * 100)\nassert.equal(cachedFunction(1), 11)\nassert.equal(cachedFunction(4), 14)\nassert.equal(cachedFunction(5), 500)\nassert.equal(map1.get(5), 500) // cached now\n```\n\nIf you want to interact with the cache (and in the case of\n`cachedMtime`, the mtime cache) then that's hanging on the\nreturned function. You can use this to pre-seed cached values,\nclear the cache in a targetted way, etc.\n\n```js\nimport { cachedMtime } from '@isaacs/cached'\nimport { writeFile, readFile } from 'node:fs/promises'\n\nconst readFileCached = cachedMtime(readFile)\nawait readFileCached('file.txt')\nconsole.log(readFileCached.cache) // Map { 'file.txt' => 'some contents' }\nconsole.log(readFileCached.mtimeCache) // Map { 'file.txt' => 853283471948 }\n```\n\nYou can set the minimum time between stat calls, and optionally\nprovide your own cache map objects for the return values and for\nthe mtime values.\n\n```js\nimport { cachedMtime } from '@isaacs/cached'\nimport { readFileSync } from 'node:fs'\nconst mtimes = new Map<string, number>\nconst contents = new Map<string, string>\nconst cached = cachedMtime(\n  (path: string) => readFileSync(path, 'utf8'),\n  1000, // check for mtime changes at most 1ce per second\n  contents,\n  mtimes\n)\n```\n\nSee [the typedocs](https://isaacs.github.io/cached) for more\ninformation.\n","_attachments":{},"license":"BlueOak-1.0.0"}