> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-HK/llms.txt # Redis 儲存空間 Redis 儲存空間實作透過 [`node-redis`](https://github.com/redis/node-redis)(Node.js 官方 Redis 用戶端)直接連接 Redis,提供高效能儲存方案。它支援獨立 Redis,亦可透過自訂用戶端設定支援 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() ``` ### 使用主機/連接埠設定 ```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() ``` ### 使用預先設定的用戶端 如需 Sentinel 或 Cluster 等進階設定,你可以傳入預先設定的 Redis 用戶端: ```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`): 此儲存空間執行個體的唯一識別碼 **connectionString** (`string`): Redis 連線 URL(例如 redis\://localhost:6379 或 redis\://:password\@localhost:6379) **host** (`string`): Redis 主機位址 **port** (`number`): Redis 連接埠號碼 (Default: `6379`) **password** (`string`): 用於驗證的 Redis 密碼 **db** (`number`): Redis 資料庫編號 (Default: `0`) **client** (`RedisClient`): 用於進階設定的預先設定 Redis 用戶端(來自 redis 套件) > **備註:** 你必須提供 `connectionString`、`host` 或 `client` 其中一項。這些選項互相排斥。 ## 補充說明 ### Key 結構 Redis 儲存空間實作使用以下 key 模式: - Thread:`mastra_threads:id:{threadId}` - 訊息:`mastra_messages:threadId:{threadId}:id:{messageId}` - 訊息索引:`msg-idx:{messageId}`(用於快速查找) - Thread 訊息排序集合:`thread:{threadId}:messages` - Workflow snapshot:`mastra_workflow_snapshot:namespace:{ns}:workflow_name:{name}:run_id:{id}` - 分數:`mastra_scorers:id:{scoreId}` - 資源:`mastra_resources:{resourceId}` ### Redis Sentinel 如使用 Redis Sentinel 進行高可用性部署,請建立自訂用戶端: ```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 用戶端: ```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, }) ``` ### 存取底層用戶端 你可以存取底層 Redis 用戶端以執行自訂操作: ```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,請明確呼叫 `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: '...' }) ```