# @frontmcp/plugin-cache 中文文档

规范地址：https://npmcn.com/package/%40frontmcp/plugin-cache/

版本：1\.12\.2
npm 元数据更新时间：2026-11-14T21:11:02.667Z
中文译文更新时间：2026-11-15T15:13:34.417Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40frontmcp/plugin-cache
原文对照：https://npmcn.com/package/%40frontmcp/plugin-cache/?lang=original
项目主页：https://frontmcp.dev/reference/plugins/cache
源码仓库：https://github.com/agentfront/frontmcp

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：68.9 kB，22 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=24\.0\.0
- 运行依赖：3 个
- peer 依赖：@vercel/kv ^2\.0\.0 \|\| ^3\.0\.0
- 首次发布：2026-01-09T03:02:41.318Z
- 最新版本发布：2026-11-14T21:11:02.267Z
- 近 30 天下载量：7841（2026-10-16 至 2026-11-14，npm 官方下载统计）
- 已发布版本数：112（近 12 个月 112 个）

## 中文简介

FrontMCP 缓存插件: 回答内存、 Redis 或 Vercel KV 的重复工具调用, 而不是再次运行工具

## 安装

```sh
npm install @frontmcp/plugin-cache
```

也可以使用：`pnpm add @frontmcp/plugin-cache`、`yarn add @frontmcp/plugin-cache`、`bun add @frontmcp/plugin-cache`

## README

# @ frontmcp/ 插件缓存

一个FrontMCP插件,它存储一个工具返回的内容并用商店的相同参数回答下一个呼叫, 不再运行工具。 使用它的工具 慢或昂贵的 答案不会改变每一个 电话 工具逐一选择,使用 `cache` 内 `@Tool`,或名称,与 `toolPatterns`。条目生活在记忆中,在 Redis,或者在Vercel KV中,每个条目属于制作它的人,除非你另有说法.

[![npm](https://img.shields.io/npm/v/@frontmcp/plugin-cache.svg)](https://www.npmjs.com/package/@frontmcp/plugin-cache) [![License](https://img.shields.io/npm/l/@frontmcp/plugin-cache.svg)](https://github.com/agentfront/frontmcp/blob/main/LICENSE)

## 安装

```bash
npm install @frontmcp/plugin-cache @frontmcp/sdk
```

需要节点.js 24或后. 保留 `@frontmcp/sdk` 上。 红色 客户端( e)`ioredis`页:1. `@vercel/kv` 是可选的对等依赖: 安装它以通过 Vercel KV `type: 'global-store'`。 。 。 .

## 使用量

在应用程序上注册插件,该插件应该缓存,并将这些工具标记为 `cache`数字 :

```ts
import { CachePlugin } from '@frontmcp/plugin-cache';
import { App, Tool, ToolContext, z } from '@frontmcp/sdk';

@Tool({
  name: 'get_exchange_rate',
  description: 'The current exchange rate between two currencies',
  inputSchema: { from: z.string().length(3), to: z.string().length(3) },
  cache: { ttl: 300 }, // answer repeated calls from the cache for 5 minutes
})
export class GetExchangeRate extends ToolContext {
  async execute({ from, to }: { from: string; to: string }) {
    return { from, to, rate: await lookUpRate(from, to) };
  }
}

@App({
  id: 'rates',
  name: 'Rates',
  tools: [GetExchangeRate],
  plugins: [CachePlugin.init({ type: 'memory', keyByIdentity: false })], // the same rate for every caller
})
export class RatesApp {}
```

缓存的答案跳过 `execute()`,其结果包括: `_meta.cache: 'hit'`。 。 。 . `CachePlugin.init()` 没有 参数,以及 `plugins: [CachePlugin]`说 `type: 'memory'` 与默认。 打开 `@FrontMcp({ plugins })`,则 插件缓存每个应用程序的工具 .

## `cache` 打开 `@Tool`

工具在有时被缓存 `cache`,或名称匹配 `toolPatterns`。没有其他的缓存.

| 数值 | 进入生命 | 读取条目 |
| --- | --- | --- |
| `cache: true` | `defaultTTL` | 离开它的过去 |
| `cache: { ttl: 60 }` | 60秒钟 | 离开它的过去 |
| `cache: { ttl: 60, slideWindow: true }` | 60秒钟 | 再给它60秒 |
| `cache: { ttl: 0 }`,或负 TTL | 未存储 | 工具没有缓存,即使 `toolPatterns` 和它比对 |

关键是呼叫者的SHA-256散列(见下文),工具(`<app id>:<tool name>`),以及经过验证的论点 用对象键排序。 工具返回的结果 `isError: true` 没有存储.

## 谁得到哪个条目

与 `keyByIdentity: true`,默认情况下,每个呼叫者都会得到自己的条目: 登录的用户按下他们的代号 主体,所以一个用户永远不会得到另一个用户的缓存答案.

-   **无身份的呼叫者跳过缓存.** 没有符号主题、客户端 ID 或会话的呼叫器服务器 已校验, 例如没有认证的 MCP 2026-07-28 客户端, 运行每个调用上的工具, 以及插件 既不读也不存储条目。 匿名身份证明( A)`anon:…`)算作无身份. 没有错误或 警告 警告他们.
-   **对每个人都一样的答案** (汇率,公共目录) `keyByIdentity: false`。每个呼叫者然后共享每个工具和参数的一个条目,因此从不将其用于其 答案取决于打电话者的账户、租户或许可.

## 选项

| 选项 | 默认 | 说明 |
| --- | --- | --- |
| `type` | `'memory'` | 在保留条目时: `'memory'`, (中文(简体) ), `'redis'`, (中文(简体) ), `'redis-client'` 或者说 `'global-store'`选项。 任何其他价值 `init()` 丢出一个 `CachePluginConfigurationError`。 。 。 . |
| `defaultTTL` | `86400` (a) 日数 | 秒一个条目在工具存在时存在 `cache` 给予不 `ttl`,用于匹配的工具 `toolPatterns`。 。 。 . |
| `keyByIdentity` | `true` | 保持每个呼叫者的单独条目. 见 [谁得到哪个条目](https://npmcn.com/package/%40frontmcp/plugin-cache/#readme-who-gets-which-entry)。 。 。 . |
| `toolPatterns` | `[]` | 缓存这些工具,没有 `cache` 在他们的元数据。 工具名称或 `<app id>:<tool name>`时 `*` 匹配任何东西 : `'catalog:*'`, (中文(简体) ), `'get_*'`。 。 。 . |
| `bypassHeader` | `'x-frontmcp-disable-cache'` | 当该调用值值为时跳过缓存的请求头 `true` 或者说 `1`它必须从 `x-frontmcp-`,或 `init()` 丢出一个 `CachePluginConfigurationError`。 。 。 . |
| `config` |  | 与 `type: 'redis'`数字 : `{ host, port, password?, db? }`。 。 。 . |
| `client` |  | 与 `type: 'redis-client'`页:1 `ioredis` 您创建的客户端 . |

## 存储

`'memory'` 将条目保存在服务器的进程中 : 它们在重新启动时丢失, 并且没有在实例之间共享 。 对于 Redis, 让插件连接 :

```ts
CachePlugin.init({
  type: 'redis',
  defaultTTL: 300,
  config: { host: 'localhost', port: 6379, password: process.env.REDIS_PASSWORD, db: 0 },
});
```

或者通过一个客户 你已经有了:

```ts
import { Redis } from 'ioredis';

const redis = new Redis({ host: 'localhost', port: 6379 });

CachePlugin.init({ type: 'redis-client', client: redis, defaultTTL: 300 });
```

`'global-store'` 使用配置在 `@FrontMcp({ redis })`: Redis, 或 Vercel KV 带 `redis: { provider: 'vercel-kv' }` (原文为: `KV_REST_API_URL` 财务报告和审定财务报表 `KV_REST_API_TOKEN` 当你不通过 `url` 财务报告和审定财务报表 `token`) (中文(简体) ). 无 `redis`,服务器不会启动.

```ts
@App({ id: 'catalog', name: 'Catalog', plugins: [CachePlugin.init({ type: 'global-store' })] })
class CatalogApp {}

@FrontMcp({
  info: { name: 'shop', version: '1.0.0' },
  apps: [CatalogApp],
  redis: { host: 'localhost', port: 6379 },
})
export default class Server {}
```

浏览器构建( A)`create()` 在网页WebMCP中,Redis没有 TCP 套接字: `'redis'` 财务报告和审定财务报表 `'redis-client'` 掷出 `BrowserStoreUnavailableError` 从 `init()`,以及 `'global-store'` 在雷迪丝上 `redis` 服务器时抛出 开始,在连接之前。 信件名称 `type: 'memory'`. Vercel KV, over HTTPS, 允许使用.

## 文档

-   [缓存插件引用](https://frontmcp.dev/reference/plugins/cache): 每一个选项,键,点击,绕行和 排除出故障, 有可运行的例子
-   [缓存结果](https://frontmcp.dev/learn/caching-results):分步学习
-   [插件和适配器](https://frontmcp.dev/reference/plugins): 在哪里注册插件

## 相关软件包

-   [`@frontmcp/sdk`](https://www.npmjs.com/package/@frontmcp/sdk): 此插件扩展的框架
-   [`@frontmcp/plugin-remember`](https://www.npmjs.com/package/@frontmcp/plugin-remember)数字 : `this.remember`值 您的工具存储和阅读自己
-   [`@frontmcp/plugin-codecall`](https://www.npmjs.com/package/@frontmcp/plugin-codecall): 取决于此插件
-   [`@frontmcp/plugins`](https://www.npmjs.com/package/@frontmcp/plugins): 重新导出此插件的元包 与另外三个

## 捐款

资料来源: [`plugins/plugin-cache`](https://github.com/agentfront/frontmcp/tree/main/plugins/plugin-cache)B. 议题和议题 欢迎拉请求;见 [捐款额](https://github.com/agentfront/frontmcp/blob/main/CONTRIBUTING.md)。 。 。 .

## 许可证

阿帕奇2.0 (英语). 见 [自由主义](https://github.com/agentfront/frontmcp/blob/main/LICENSE)。 。 。 .

## 常见问题

### @frontmcp/plugin-cache 是什么？

FrontMCP 缓存插件: 回答内存、 Redis 或 Vercel KV 的重复工具调用, 而不是再次运行工具

### 如何安装 @frontmcp/plugin-cache？

在项目目录运行 npm install @frontmcp/plugin-cache，或使用 pnpm add / yarn add / bun add。

### @frontmcp/plugin-cache 支持 TypeScript 吗？

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

### @frontmcp/plugin-cache 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @frontmcp/plugin-cache 需要什么版本的 Node\.js？

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

### @frontmcp/plugin-cache 可以商用吗？

@frontmcp/plugin-cache 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @frontmcp/plugin-cache 的下载量有多少？

根据 npm 官方下载统计，@frontmcp/plugin-cache 在 2026/10/16 – 2026/11/14 这 30 天内被下载 7,841 次，日均约 261 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @frontmcp/plugin-cache 还在维护吗？

最新版本 1\.12\.2 发布于 2026/11/15（1 天前）；至今共发布 112 个版本，其中过去 12 个月发布了 112 个；首次发布于 2026/01/09。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

