@graphile/lru

中文文档
5.0.1发布于 2026/10/30MITTypeScriptCommonJS构建来源已验证近 30 天下载 58.6万

Extremely simple zero-dependencies ES6 LRU (you probably want lru-cache instead)

原始使用文档

@graphile/lru

You probably want lru-cache instead.

This is an obsessively optimized LRU cache for Node.js, it forgoes features in favour of performance, and is very marginally faster than node-lru-cache v6 in certain circumstances (namely those that we care about inside the Graphile internals). A performance comparison versus node-lru-cache v7 has not yet been performed.

Usage

import LRU from "@graphile/lru";

// Construct an LRU cache
const lru = new LRU({
  // Maximum number of items to store (required)
  maxLength: 500, // Must be an integer at least 2

  // Optional callback to handle when a value is removed (both when LRU becomes
  // full and when LRU is reset)
  dispose(key, value) {
    console.log(`Disposing of key '${key}' with value '${inspect(value)}'`);
  },
});

const ANY_KEY_HERE = { foo: "bar" };
const ANY_VALUE_HERE = { randomNumber: () => 4 };
// Store a value
lru.set(ANY_KEY_HERE, ANY_VALUE_HERE);

// Retrieve a value
const value = lru.get(ANY_KEY_HERE);

// Clear the cache
lru.reset();

Considering a pull request?

Pull requests that add features that aren't required by the Graphile suite are unlikely to be entertained - use node-lru-cache instead!

Pull requests that improve performance should come with benchmarks, benchmark scripts and explanation of technique. And if you pull it off without breaking anything - YES PLEASE.

Pull requests that add documentation are welcome.

版本与发布记录

@graphile/lru 共发布 42 个版本,首次发布于 2019/05/11;过去 12 个月发布了 25 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.0.1latest2026/10/3017 天前
5.0.1-aardvark.next-20260928202932snapshot-next2026/09/291 个月前
5.0.1-aardvark.next-202609251642492026/09/261 个月前
5.0.1-aardvark.next-202609222244162026/09/231 个月前
5.0.1-aardvark.next-202609191851302026/09/201 个月前
5.0.1-aardvark.next-202609180853272026/09/181 个月前

其他发布通道:next → 4.11.0,prealpha → 5.0.0-alpha.1,alpha → 5.0.0-alpha.2,beta → 5.0.0-beta.4,rc → 5.0.0-rc.5,snapshot-next → 5.0.1-aardvark.next-20260928202932。

关于 @graphile/lru 的常见问题

@graphile/lru 是什么?

Extremely simple zero-dependencies ES6 LRU (you probably want lru-cache instead)

如何安装 @graphile/lru?

在项目目录运行 npm install @graphile/lru,或使用 pnpm add / yarn add / bun add。

@graphile/lru 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@graphile/lru 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@graphile/lru 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@graphile/lru 可以商用吗?

@graphile/lru 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@graphile/lru 的下载量有多少?

根据 npm 官方下载统计,@graphile/lru 在 2026/10/17 – 2026/11/15 这 30 天内被下载 586,049 次,日均约 19,535 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@graphile/lru 还在维护吗?

最新版本 5.0.1 发布于 2026/10/30(17 天前);至今共发布 42 个版本,其中过去 12 个月发布了 25 个;首次发布于 2019/05/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。