功能说明
模拟给定容量 LFU 缓存。每 get 递增频次并更新最近性;满缓存 put 淘汰最少频次使用键,并列以最近性 LRU 破。淘汰打印使策略可观察。运行 printf “put a 1\nput b 2\nget a\nput c 3” | node lfu.mjs 2 输出 get a = 1、evicted b。频次与最近性映射每次淘汰扫描 O(n);真实 LFU 用频次表结构 O(1) 淘汰,此 CLI 重清晰。
运行方式
printf "put a 1\nput b 2\nget a\nput c 3" | node lfu.mjs 2
限制与后续
当前最少使用频次缓存命令行工具实现覆盖最常见的使用路径。本译文依据英文原文完整重写,代码与命令与英文版本严格一致,可在本地按运行方式复现并核对结果。The frequency and recency maps are scanned on every eviction, which is O(n). A real LFU uses a frequency-list structure for O(1) eviction, but this CLI prioritizes clarity.若用于生产,请先补充输入校验、错误处理与日志,再结合自身环境调整node版本。