Redis ストレージ
Redis ストレージ実装は、Node.js 公式 Redis クライアント node-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:6379、redis://:password@localhost:6379)host?:
string
Redis のホストアドレス
port?:
number
= 6379
Redis のポート番号
password?:
string
Redis 認証用のパスワード
db?:
number
= 0
Redis のデータベース番号
client?:
RedisClient
高度な構成に使用する、設定済みの Redis クライアント(
redis パッケージ)注記
connectionString、host、client のいずれかを指定する必要があります。これらのオプションは同時に指定できません。
補足補足への直接リンク
キー構造キー構造への直接リンク
Redis ストレージ実装では次のキーパターンを使用します。
- 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 を使用する高可用性構成では、カスタムクライアントを作成します。
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 クライアントを使用します。
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: '...' })