@apollo/utils.keyvaluecache

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

Minimal key-value cache interface

原始使用文档

KeyValueCache interface

export interface KeyValueCache<
  V = string,
  SO extends KeyValueCacheSetOptions = KeyValueCacheSetOptions,
> {
  get(key: string): Promise<V | undefined>;
  set(key: string, value: V, options?: SO): Promise<void>;
  delete(key: string): Promise<boolean | void>;
}

This interface defines a minimally-compatible cache intended for (but not limited to) use by Apollo Server. It is notably implemented by KeyvAdapter from the @apollo/utils.keyvadapter package. (KeyvAdapter in conjunction with a Keyv is probably more interesting to you unless you're actually building a cache!)

InMemoryLRUCache

This class wraps lru-cache and implements the KeyValueCache interface. It accepts LRUCache.Options as the constructor argument and passes them to the LRUCache which is created. A default maxSize and sizeCalculator are provided in order to prevent an unbounded cache; these can both be tweaked via the constructor argument.

const cache = new InMemoryLRUCache({
  // create a larger-than-default `LRUCache`
  maxSize: Math.pow(2, 20) * 50,
});

PrefixingKeyValueCache

This class wraps a KeyValueCache in order to provide a specified prefix for keys entering the cache via this wrapper.

const cache = new InMemoryLRUCache();
const prefixedCache = new PrefixingKeyValueCache(cache, "apollo:");

One reason to use this is if a single piece of software wants to use a cache for multiple features. For example, you can pass a KeyValueCache as the cache option to @apollo/server's ApolloServer class; it provides this cache to plugins and other features as a default cache to use (if the user does not provide the specific plugin its own cache). Each feature uses PrefixingKeyValueCache with a different prefix to prevent different features from stomping on each others' data.

However, if you are configuring one of those features explicitly, you may not want this prefix to be added. In that case, you can wrap your cache in a cache returned by PrefixingKeyValueCache.cacheDangerouslyDoesNotNeedPrefixesForIsolation. The only difference between this cache and the cache that it wraps is that when it is passed directly to a PrefixingKeyValueCache, no prefix is applied.

That is, let's say you are using a class that is implemented like this:

class SomePlugin {
  private cache: KeyValueCache;
  constructor(cache: KeyValueCache) {
    this.cache = new PrefixingKeyValueCache(cache, "some:");
  }
}

If you set up your plugin as new SomePlugin({ cache: myRedisCache }) then the plugin will add some: to all keys when interacting with your cache, but if you set it up as new SomePlugin({ cache: PrefixingKeyValueCache.cacheDangerouslyDoesNotNeedPrefixesForIsolation(myRedisCache) }), then the plugin will not apply its prefix. You should only do this if you feel confident that this feature's use of this cache will not overlap with another feature: perhaps this is the only feature you have configured to use this cache, or perhaps the feature provides suitable control over cache keys that you can ensure isolation without needing the plugin's prefix.

Software like ApolloServer that passes a single KeyValueCache to several features should throw if a PrefixesAreUnnecessaryForIsolationCache is provided to it; it can check this condition with the static PrefixingKeyValueCache.prefixesAreUnnecessaryForIsolation method (which is safer than an instanceof check in case there are multiple copies of @apollo/utils.keyvaluecache installed).

ErrorsAreMissesCache

This class wraps a KeyValueCache in order to provide error tolerance for caches which connect via a client like Redis. In the event that there's an error, this wrapper will treat it as a cache miss (and log the error instead, if a logger is provided).

An example usage (which makes use of the keyv Redis client and our KeyvAdapter) would look something like this:

import Keyv from "keyv";
import { KeyvAdapter } from "@apollo/utils.keyvadapter";
import { ErrorsAreMissesCache } from "@apollo/utils.keyvaluecache";

const redisCache = new Keyv("redis://user:pass@localhost:6379");
const faultTolerantCache = new ErrorsAreMissesCache(
  new KeyvAdapter(redisCache),
);

版本与发布记录

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

版本发布日期距今
4.0.0latest2025/05/221.5 年前
3.1.02023/09/143 年前
3.0.02023/04/183 年前
2.1.12023/04/043 年前
2.1.02022/12/103 年前
1.0.22022/11/243 年前

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

@apollo/utils.keyvaluecache 是什么?

Minimal key-value cache interface

如何安装 @apollo/utils.keyvaluecache?

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

@apollo/utils.keyvaluecache 支持 TypeScript 吗?

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

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

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

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

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

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

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

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

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

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

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