跳至主要內容

Redis 儲存空間

Redis 儲存空間實作透過 node-redis(Node.js 官方 Redis 用戶端)直接連接 Redis,提供高效能儲存方案。它支援獨立 Redis,亦可透過自訂用戶端設定支援 Redis Sentinel 及 Cluster 部署。

安裝
安裝 的直接連結

npm install @mastra/redis

用法
用法 的直接連結

使用連線字串
使用連線字串 的直接連結

import { RedisStore } from '@mastra/redis'

const storage = new RedisStore({
id: 'redis-storage',
connectionString: 'redis://localhost:6379',
})

await storage.init()

使用主機/連接埠設定
使用主機/連接埠設定 的直接連結

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 用戶端:

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:6379redis://:password@localhost:6379

host?:

string
Redis 主機位址

port?:

number
= 6379
Redis 連接埠號碼

password?:

string
用於驗證的 Redis 密碼

db?:

number
= 0
Redis 資料庫編號

client?:

RedisClient
用於進階設定的預先設定 Redis 用戶端(來自 redis 套件)
備註

你必須提供 connectionStringhostclient 其中一項。這些選項互相排斥。

補充說明
補充說明 的直接連結

Key 結構
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 的直接連結

如使用 Redis Sentinel 進行高可用性部署,請建立自訂用戶端:

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 的直接連結

如使用 Redis Cluster 部署,請使用 Cluster 用戶端:

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 用戶端以執行自訂操作:

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 連線:

await storage.close()

用法範例
用法範例 的直接連結

為 Agent 加入 memory
為 Agent 加入 memory 的直接連結

src/mastra/agents/redis-agent.ts
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
使用 Agent 的直接連結

src/test-redis-agent.ts
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 執行個體使用
配合 Mastra 執行個體使用 的直接連結

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()

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: '...' })