> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # Redis ストレージ Redis ストレージ実装は、Node.js 公式 Redis クライアント [`node-redis`](https://github.com/redis/node-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` のいずれかを指定する必要があります。これらのオプションは同時に指定できません。 ## 補足 ### キー構造 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 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: '...' }) ```