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
storage 实例的唯一标识符
connectionString?:
string
Redis 连接 URL(例如
redis://localhost:6379 或 redis://:password@localhost:6379)host?:
string
Redis host 地址
port?:
number
= 6379
Redis port 号
password?:
string
用于身份验证的 Redis password
db?:
number
= 0
Redis database 号
client?:
RedisClient
用于高级设置的预配置 redis client(来自
redis package)备注
你必须提供 connectionString、host 或 client 之一。这些选项互斥。
其他说明其他说明的直接链接
Key 结构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 SentinelRedis 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 ClusterRedis 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 添加 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 直接使用 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: '...' })