> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # Redis 存储 Redis 存储实现通过 [`node-redis`](https://github.com/redis/node-redis)(Node.js 官方 Redis client)的直接 Redis 连接提供高性能存储解决方案。它支持独立 Redis,也可通过自定义 client 配置支持 Redis Sentinel 和 Cluster 部署。 ## 安装 ```bash npm install @mastra/redis ``` ## 使用方法 ### 使用连接字符串 ```typescript import { RedisStore } from '@mastra/redis' const storage = new RedisStore({ id: 'redis-storage', connectionString: 'redis://localhost:6379', }) await storage.init() ``` ### 使用 Host/Port 配置 ```typescript import { RedisStore } from '@mastra/redis' const storage = new RedisStore({ id: 'redis-storage', host: 'localhost', port: 6379, password: 'your-password', db: 0, }) await storage.init() ``` ### 使用预配置的 Client 对于 Sentinel 或 Cluster 等高级配置,你可以传入预配置的 Redis client: ```typescript import { RedisStore } from '@mastra/redis' import { createClient } from 'redis' const client = createClient({ url: 'redis://localhost:6379', socket: { reconnectStrategy: retries => Math.min(retries * 50, 2000), }, }) // Connect the client before passing to RedisStore await client.connect() const storage = new RedisStore({ id: 'redis-storage', client, }) ``` ## 参数 **id** (`string`): storage 实例的唯一标识符 **connectionString** (`string`): Redis 连接 URL(例如 redis\://localhost:6379 或 redis\://:password\@localhost:6379) **host** (`string`): Redis host 地址 **port** (`number`): Redis port 号 (Default: `6379`) **password** (`string`): 用于身份验证的 Redis password **db** (`number`): Redis database 号 (Default: `0`) **client** (`RedisClient`): 用于高级设置的预配置 redis client(来自 redis package) > **备注:** 你必须提供 `connectionString`、`host` 或 `client` 之一。这些选项互斥。 ## 其他说明 ### Key 结构 Redis 存储实现使用以下 key 模式: - Thread:`mastra_threads:id:{threadId}` - Message:`mastra_messages:threadId:{threadId}:id:{messageId}` - Message 索引:`msg-idx:{messageId}`(用于快速查找) - Thread message 有序集合:`thread:{threadId}:messages` - Workflow 快照:`mastra_workflow_snapshot:namespace:{ns}:workflow_name:{name}:run_id:{id}` - Score:`mastra_scorers:id:{scoreId}` - Resource:`mastra_resources:{resourceId}` ### Redis Sentinel 对于使用 Redis Sentinel 的高可用部署,请创建自定义 client: ```typescript import { RedisStore } from '@mastra/redis' import { createClient } from 'redis' const client = createClient({ url: 'redis://sentinel-host:26379', // Configure sentinel options as needed for your setup }) await client.connect() const storage = new RedisStore({ id: 'redis-sentinel-storage', client, }) ``` ### Redis Cluster 对于 Redis Cluster 部署,请使用 cluster client: ```typescript import { RedisStore } from '@mastra/redis' import { createCluster } from 'redis' const cluster = createCluster({ rootNodes: [ { url: 'redis://node-1:6379' }, { url: 'redis://node-2:6379' }, { url: 'redis://node-3:6379' }, ], }) await cluster.connect() const storage = new RedisStore({ id: 'redis-cluster-storage', client: cluster, }) ``` ### 访问底层 Client 你可以访问底层 Redis client 以执行自定义操作: ```typescript const storage = new RedisStore({ id: 'redis-storage', connectionString: 'redis://localhost:6379', }) await storage.init() const client = storage.getClient() // Custom Redis operations await client.set('custom-key', 'value') const value = await client.get('custom-key') ``` ### 关闭连接 关闭应用程序时,请关闭 Redis 连接: ```typescript await storage.close() ``` ## 使用示例 ### 向 agent 添加 memory ```typescript import { Memory } from '@mastra/memory' import { Agent } from '@mastra/core/agent' import { RedisStore } from '@mastra/redis' export const redisAgent = new Agent({ id: 'redis-agent', name: 'Redis Agent', instructions: 'You are an AI agent with the ability to automatically recall memories from previous interactions.', model: 'openai/gpt-5.6-sol', memory: new Memory({ storage: new RedisStore({ id: 'redis-agent-storage', connectionString: process.env.REDIS_URL!, }), options: { lastMessages: 10, }, }), }) ``` ### 使用 agent ```typescript import 'dotenv/config' import { mastra } from './mastra' const threadId = '123' const resourceId = 'user-456' const agent = mastra.getAgent('redisAgent') const message = await agent.stream('My name is Mastra', { memory: { thread: threadId, resource: resourceId, }, }) await message.textStream.pipeTo(new WritableStream()) const stream = await agent.stream("What's my name?", { memory: { thread: threadId, resource: resourceId, }, memoryOptions: { lastMessages: 5, }, }) for await (const chunk of stream.textStream) { process.stdout.write(chunk) } ``` ### 与 Mastra 实例一起使用 ```typescript import { Mastra } from '@mastra/core' import { RedisStore } from '@mastra/redis' const storage = new RedisStore({ id: 'mastra-storage', host: 'localhost', port: 6379, }) const mastra = new Mastra({ storage, // init() called automatically }) ``` 如果不通过 Mastra 直接使用 storage,请显式调用 `init()`: ```typescript import { RedisStore } from '@mastra/redis' const storage = new RedisStore({ id: 'redis-storage', host: 'localhost', port: 6379, }) await storage.init() // Access domain-specific stores via getStore() const memoryStore = await storage.getStore('memory') const thread = await memoryStore?.getThreadById({ threadId: '...' }) ```