{"_id":"@a-shui/lite-req-cache","name":"@a-shui/lite-req-cache","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.1":{"name":"@a-shui/lite-req-cache","version":"1.0.1","description":"A zero-dependency, LRU-based Promise caching library for Browser and Node.js","keywords":["cache","promise","lru","request","memoize","async","typescript"],"author":"","license":"ISC","type":"module","main":"./dist/index.cjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs"}},"scripts":{"build":"rollup -c","test":"vitest run","test:watch":"vitest","prepublishOnly":"npm run build"},"devDependencies":{"@rollup/plugin-terser":"^0.4.4","@rollup/plugin-typescript":"^12.1.2","rollup":"^4.34.8","rollup-plugin-dts":"^6.1.1","tslib":"^2.8.1","typescript":"^5.7.3","vitest":"^3.0.7"},"engines":{"node":">=14.0.0"},"repository":{"type":"git","url":""},"_id":"@a-shui/lite-req-cache@1.0.1","gitHead":"f8b57c6503ef933cef7a896a288ac73fa00ae2c7","_nodeVersion":"20.19.6","_npmVersion":"10.8.2","dist":{"integrity":"sha512-xoeLYWjdRVhYbqf7YkC7pX+47gOYdnNB/7Moh6/IH7mjauJc99BRJcnubQchCRgWLenDPYzvdt7UE8hXXkbCIw==","shasum":"ceb38fd5189686e70a02b1d0a9fdbfd2452e592e","tarball":"https://registry.npmjs.org/@a-shui/lite-req-cache/-/lite-req-cache-1.0.1.tgz","fileCount":5,"unpackedSize":8868,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQD1pTp/d2jGCPyOhudseyjs77cEcqn31dKYQenNWp44mwIhAI1wcWWYapqnyNll8EPsYkqaHhs7opRVlxybJkdSEQG9"}]},"_npmUser":{"name":"ashuiweb","email":"1032414245@qq.com"},"directories":{},"maintainers":[{"name":"ashuiweb","email":"1032414245@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/lite-req-cache_1.0.1_1772608626341_0.8913453730668937"},"_hasShrinkwrap":false}},"time":{"created":"2026-03-04T07:17:06.285Z","1.0.1":"2026-03-04T07:17:06.489Z","modified":"2026-03-04T07:17:06.675Z"},"maintainers":[{"name":"ashuiweb","email":"1032414245@qq.com"}],"description":"A zero-dependency, LRU-based Promise caching library for Browser and Node.js","keywords":["cache","promise","lru","request","memoize","async","typescript"],"repository":{"type":"git","url":""},"license":"ISC","readme":"# lite-req-cache 快速启动说明文档\n\n## 一、环境准备\n\n### 1.1 系统要求\n\n- Node.js >= 14.0.0\n- pnpm >= 8.x（推荐）或 npm / yarn\n\n### 1.2 安装 Node.js\n\n如果尚未安装 Node.js，请前往 [Node.js 官网](https://nodejs.org/) 下载并安装 LTS 版本。\n\n验证安装：\n```bash\nnode -v\nnpm -v\n```\n\n### 1.3 安装 pnpm（推荐）\n\n```bash\nnpm install -g pnpm\n\n# 验证安装\npnpm -v\n```\n\n## 二、项目启动步骤\n\n### 2.1 克隆/下载项目\n\n```bash\n# 进入项目目录\ncd lite-req-cache\n```\n\n### 2.2 安装依赖\n\n```bash\n# 使用 pnpm（推荐）\npnpm install\n\n# 或使用 npm\nnpm install\n\n# 或使用 yarn\nyarn install\n```\n\n### 2.3 构建项目\n\n```bash\npnpm build\n```\n\n构建成功后会生成 `dist/` 目录，包含：\n- `index.mjs` - ESM 格式\n- `index.cjs` - CommonJS 格式\n- `index.d.ts` - TypeScript 类型声明\n\n### 2.4 运行测试\n\n```bash\n# 单次运行测试\npnpm test\n\n# 监听模式（开发时使用）\npnpm test:watch\n```\n\n## 三、使用示例\n\n### 3.1 基础用法\n\n```typescript\nimport reqCache from 'lite-req-cache';\n\n// 原始异步函数\nconst fetchData = async (name: string) => {\n  const response = await fetch(`/api/data?name=${name}`);\n  return response.json();\n};\n\n// 包装为缓存版本（缓存 3000ms）\nconst cachedFetchData = reqCache(fetchData, 3000);\n\n// 使用\nawait cachedFetchData('test');  // 真实发起请求\nawait cachedFetchData('test');  // 命中缓存，不发起请求\n```\n\n### 3.2 进阶用法\n\n```typescript\nimport reqCache from 'lite-req-cache';\n\nconst fetchUserList = reqCache(\n  async (params: { page: number; keyword: string; timestamp: number }) => {\n    // ...请求逻辑\n  },\n  {\n    ttl: 5000,              // 缓存 5 秒\n    max: 20,                // LRU 最大缓存 20 条\n    keyResolver: (params) => `${params.page}_${params.keyword}`  // 自定义缓存键\n  }\n);\n```\n\n### 3.3 在不同环境中使用\n\n#### ES Modules (ESM)\n\n```javascript\nimport reqCache from 'lite-req-cache';\n```\n\n#### CommonJS (CJS)\n\n```javascript\nconst reqCache = require('lite-req-cache').default;\n// 或\nconst { default: reqCache } = require('lite-req-cache');\n```\n\n## 四、核心功能验证\n\n### 4.1 验证缓存命中\n\n```javascript\nlet callCount = 0;\nconst fn = reqCache(async (x) => {\n  callCount++;\n  return x * 2;\n}, 1000);\n\nawait fn(1);  // callCount = 1\nawait fn(1);  // callCount = 1 (命中缓存)\nawait fn(2);  // callCount = 2 (不同参数)\n```\n\n### 4.2 验证 TTL 过期\n\n```javascript\nconst fn = reqCache(async (x) => Date.now(), 100);\n\nconst t1 = await fn(1);\nawait new Promise(r => setTimeout(r, 200));\nconst t2 = await fn(1);\n\nconsole.log(t1 !== t2);  // true，缓存已过期重新请求\n```\n\n### 4.3 验证失败不缓存\n\n```javascript\nlet failCount = 0;\nconst fn = reqCache(async () => {\n  failCount++;\n  if (failCount === 1) throw new Error('fail');\n  return 'success';\n}, 5000);\n\ntry { await fn(); } catch (e) {}  // 第一次失败\nconst result = await fn();         // 第二次重新请求，成功\nconsole.log(result);  // 'success'\n```\n\n## 五、常见问题\n\n### Q1: 构建时提示找不到 tslib\n\n**解决方案**：安装 tslib 开发依赖\n```bash\npnpm add -D tslib\n```\n\n### Q2: 测试运行失败\n\n**解决方案**：\n1. 确保依赖已完整安装：`pnpm install`\n2. 检查 Node.js 版本：`node -v`（需要 >= 14）\n\n### Q3: 类型推断不正确\n\n**解决方案**：\n确保使用 TypeScript 4.x+ 版本，并在 tsconfig.json 中启用 strict 模式。\n\n## 六、发布到 NPM（开发者）\n\n```bash\n# 1. 登录 npm\nnpm login\n\n# 2. 发布\nnpm publish\n\n# 3. 验证发布成功\nnpm info lite-req-cache\n```\n\n## 七、技术支持\n\n- 问题反馈：请提交 GitHub Issue\n- 文档位置：`md/` 目录下的需求文档和架构文档","readmeFilename":"readme.md","_rev":"1-e82dc50cdc8cb15abd1b816bccb3b131"}