
Node-lru-cache终极配置指南20个关键参数详解【免费下载链接】node-lru-cacheA fast cache that automatically deletes the least recently used items项目地址: https://gitcode.com/gh_mirrors/no/node-lru-cacheNode-lru-cache是一个高性能的LRU最近最少使用缓存库它能自动删除最久未使用的项目是Node.js应用中管理内存缓存的终极解决方案。这款工具特别适合需要高效缓存管理的Web应用、API服务和数据处理系统。 快速安装与基础配置首先通过npm安装lru-cachenpm install lru-cache --save然后导入并使用import { LRUCache } from lru-cache // 或 const { LRUCache } require(lru-cache)基础配置只需要一个参数但为了获得最佳性能建议预先分配内存const cache new LRUCache({ max: 500, // 最大缓存项数 }) 核心配置参数详解1. 容量控制参数max- 缓存最大项数 这是最重要的参数决定了缓存能存储多少项。设置合适的max值可以避免内存溢出。maxSize- 基于大小的缓存限制 当需要根据项目大小而非数量限制缓存时使用const cache new LRUCache({ maxSize: 1024 * 1024 * 100, // 100MB sizeCalculation: (value, key) { return JSON.stringify(value).length } })2. TTL生存时间参数ttl- 全局TTL设置 所有缓存项的默认生存时间毫秒const cache new LRUCache({ max: 1000, ttl: 1000 * 60 * 5, // 5分钟 })ttlAutopurge- 自动清理过期项 启用后缓存会自动清理过期项const cache new LRUCache({ max: 1000, ttl: 1000 * 60 * 5, ttlAutopurge: true, // 自动清理过期项 })3. 性能优化参数allowStale- 允许返回过期项 当需要即使数据过期也返回时使用const cache new LRUCache({ max: 1000, ttl: 1000 * 60 * 5, allowStale: true, // 允许返回过期数据 })updateAgeOnGet- 获取时更新年龄 获取项目时重置其TTLconst cache new LRUCache({ max: 1000, ttl: 1000 * 60 * 5, updateAgeOnGet: true, // 获取时重置TTL })4. 内存管理参数dispose- 项目被删除时的回调 当项目从缓存中移除时执行清理操作const cache new LRUCache({ max: 100, dispose: (value, key, reason) { console.log(键 ${key} 被移除原因: ${reason}) // 执行清理操作如关闭数据库连接 } })disposeAfter- dispose的异步版本 在dispose之后异步执行const cache new LRUCache({ max: 100, disposeAfter: (value, key, reason) { // 异步清理操作 return cleanupAsync(value) } })5. 大小计算参数sizeCalculation- 自定义大小计算 计算每个项目占用的空间const cache new LRUCache({ maxSize: 1024 * 1024 * 50, // 50MB sizeCalculation: (value, key) { if (Buffer.isBuffer(value)) { return value.length } if (typeof value string) { return value.length } return 1 // 默认大小 } })noDeleteOnStaleGet- 不删除过期项 获取过期项时不自动删除const cache new LRUCache({ max: 1000, ttl: 1000 * 60 * 5, noDeleteOnStaleGet: true, })6. 并发控制参数noDisposeOnSet- 设置时不触发dispose 当新值替换旧值时不触发旧值的disposeconst cache new LRUCache({ max: 100, noDisposeOnSet: true, })noUpdateTTL- 不更新TTL 访问项目时不更新其TTLconst cache new LRUCache({ max: 1000, ttl: 1000 * 60 * 5, noUpdateTTL: true, })7. Fetch相关参数fetchMethod- 异步获取方法 当缓存未命中时自动获取数据const cache new LRUCache({ max: 1000, fetchMethod: async (key, staleValue, { options, signal }) { // 从数据库或API获取数据 const data await fetchFromDatabase(key) return data } })allowStaleOnFetchAbort- 中止时允许过期值 当fetch被中止时返回过期值const cache new LRUCache({ max: 1000, fetchMethod: async (key) { /* ... */ }, allowStaleOnFetchAbort: true, })allowStaleOnFetchRejection- 拒绝时允许过期值 当fetch被拒绝时返回过期值const cache new LRUCache({ max: 1000, fetchMethod: async (key) { /* ... */ }, allowStaleOnFetchRejection: true, })8. 其他重要参数ignoreFetchAbort- 忽略fetch中止 即使fetch被中止也继续const cache new LRUCache({ max: 1000, fetchMethod: async (key) { /* ... */ }, ignoreFetchAbort: true, })noDeleteOnFetchRejection- 拒绝时不删除 当fetch被拒绝时不删除缓存项const cache new LRUCache({ max: 1000, fetchMethod: async (key) { /* ... */ }, noDeleteOnFetchRejection: true, }) 高级使用技巧组合参数实现智能缓存const smartCache new LRUCache({ max: 1000, maxSize: 1024 * 1024 * 100, // 100MB限制 ttl: 1000 * 60 * 10, // 10分钟TTL ttlAutopurge: true, allowStale: true, updateAgeOnGet: true, sizeCalculation: (value) { // 智能大小计算 return estimateMemoryUsage(value) }, fetchMethod: async (key) { // 智能数据获取 return await fetchDataWithFallback(key) } })监控缓存状态使用内置方法监控缓存性能const cache new LRUCache({ max: 1000 }) // 添加一些数据 cache.set(key1, value1) cache.set(key2, value2) // 获取缓存统计 console.log(cache.size) // 当前缓存项数 console.log(cache.calculatedSize) // 计算的总大小 console.log(cache.has(key1)) // 检查键是否存在 // 遍历缓存 for (const [key, value] of cache.entries()) { console.log(key, value) } 性能优化建议预先分配内存设置合理的max值避免动态扩容选择合适的TTL根据数据更新频率设置TTL使用sizeCalculation精确控制内存使用启用ttlAutopurge定期清理过期数据利用fetchMethod减少缓存未命中的延迟 实战应用场景Web API缓存const apiCache new LRUCache({ max: 1000, ttl: 1000 * 60, // 1分钟 allowStale: true, fetchMethod: async (url) { const response await fetch(url) return await response.json() } }) async function getCachedData(url) { return await apiCache.fetch(url) }数据库查询缓存const queryCache new LRUCache({ max: 500, ttl: 1000 * 60 * 5, // 5分钟 dispose: (value, key) { // 清理数据库连接 value.connection?.close() } }) 总结Node-lru-cache提供了20多个配置参数让你可以精确控制缓存行为。通过合理组合这些参数你可以创建出适合各种场景的高性能缓存系统。记住关键点总是设置max或maxSize来限制缓存大小根据数据特性选择合适的TTL并利用fetchMethod优化缓存未命中时的用户体验。开始使用Node-lru-cache让你的应用性能飞起来【免费下载链接】node-lru-cacheA fast cache that automatically deletes the least recently used items项目地址: https://gitcode.com/gh_mirrors/no/node-lru-cache创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考