@apollo/utils.keyvadapter

中文文档
4.0.1发布于 2025/05/22MITTypeScriptCommonJS近 30 天下载 71.7万

Keyv adapter implementing Apollo's KeyValueCache interface

原始使用文档
文档目录3 节
  1. Usage
  2. Options
  3. disableBatchReads

KeyvAdapter class

The KeyvAdapter class is a simple wrapper for Keyv which implements the KeyValueCache interface from the @apollo/utils.keyvaluecache package. This class is intended for use by (but not limited to) Apollo Server's cache. Keyv supports a number of useful adapters like Redis, Mongo, SQLite, etc.

Usage

Here's an example of using a Redis store for your ApolloServer cache:

Install the necessary packages:

npm install @apollo/utils.keyvadapter keyv @keyv/redis

Note: The latest version of this library (v4 and up) only works with Keyv version 5. In order to work with the older version 4 of Keyv, version 3 of this library can be used instead.

In Apollo Server v3:

import { ApolloServer } from "apollo-server";
import { KeyvAdapter } from "@apollo/utils.keyvadapter";
import Keyv from "keyv";
import KeyvRedis from "@keyv/redis";

new ApolloServer({
  cache: new KeyvAdapter(new Keyv({ store: new KeyvRedis("redis://...") })),
});

In Apollo Server v4:

import { ApolloServer } from "@apollo/server";
import { KeyvAdapter } from "@apollo/utils.keyvadapter";
import Keyv from "keyv";
import KeyvRedis from "@keyv/redis";

new ApolloServer({
  cache: new KeyvAdapter(new Keyv({ store: new KeyvRedis("redis://...") })),
});

Options

disableBatchReads

By default, KeyvAdapter will use DataLoader's batching functionality in order to request a list of keys when possible.

  • If your Keyv.store implements getMany, this will be called with the list of keys aggregated by DataLoader.
  • If your Keyv.store does not implement getMany, get will be called in parallel for each key (awaiting a Promise.all()).

Redis.Cluster from ioredis does not support mget (and thus, store.getMany is broken in this case), so batching should be disabled like so:

new ApolloServer({
  cache: new KeyvAdapter(new Keyv({ store: new KeyvRedis("redis://...") }), {
    disableBatchReads: true,
  }),
});

版本与发布记录

@apollo/utils.keyvadapter 共发布 12 个版本,首次发布于 2022/05/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.1latest2025/05/221.5 年前
4.0.02024/10/232 年前
3.1.02023/09/143 年前
3.0.02023/04/183 年前
2.0.22023/04/043 年前
2.0.12023/03/073 年前

关于 @apollo/utils.keyvadapter 的常见问题

@apollo/utils.keyvadapter 是什么?

Keyv adapter implementing Apollo's KeyValueCache interface

如何安装 @apollo/utils.keyvadapter?

在项目目录运行 npm install @apollo/utils.keyvadapter,或使用 pnpm add / yarn add / bun add。

@apollo/utils.keyvadapter 支持 TypeScript 吗?

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

@apollo/utils.keyvadapter 支持 ESM 还是 CommonJS?

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

@apollo/utils.keyvadapter 需要什么版本的 Node.js?

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

@apollo/utils.keyvadapter 可以商用吗?

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

@apollo/utils.keyvadapter 的下载量有多少?

根据 npm 官方下载统计,@apollo/utils.keyvadapter 在 2026/10/07 – 2026/11/05 这 30 天内被下载 717,473 次,日均约 23,916 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@apollo/utils.keyvadapter 还在维护吗?

最新版本 4.0.1 发布于 2025/05/22(1.5 年前);至今共发布 12 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/05/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。