跳至主要內容

Redis 儲存空間

Redis 儲存空間實作透過 node-redis(Node.js 的官方 Redis client)直接連線至 Redis,提供高效能的儲存解決方案。它支援獨立 Redis,也能透過自訂 client 設定支援 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()

使用 Host/Port 設定
「使用 Host/Port 設定」的直接連結

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
「使用預先設定的 client」的直接連結

對於 Sentinel 或 Cluster 等進階設定,可以傳入預先設定的 Redis client:

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 host 位址

port?:

number
= 6379
Redis port 號碼

password?:

string
用於驗證的 Redis 密碼

db?:

number
= 0
Redis 資料庫編號

client?:

RedisClient
用於進階設定、預先設定完成的 redis client(來自 redis 套件)
備註

你必須提供 connectionStringhostclient 其中之一。這些選項彼此互斥。

其他注意事項
「其他注意事項」的直接連結

Key 結構
「Key 結構」的直接連結

Redis 儲存空間實作使用下列 key 模式:

  • Thread:mastra_threads:id:{threadId}
  • 訊息:mastra_messages:threadId:{threadId}:id:{messageId}
  • 訊息索引:msg-idx:{messageId}(用於快速查詢)
  • Thread 訊息 sorted set: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 的高可用性部署,請建立自訂 client:

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 client:

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
「存取底層 client」的直接連結

你可以存取底層 Redis client,以執行自訂操作:

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 新增記憶體
「為 Agent 新增記憶體」的直接連結

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 而直接使用 storage,請明確呼叫 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: '...' })