ioredis について
ioredis v5 を使う開発者向けのリファレンススキルです。TypeScript の Redis クライアントの基本操作、接続設定、高度なパターンを扱います。
主な機能
- 01Redis 接続の設定
- 02パイプラインとトランザクション
- 03Pub/Sub と Lua スクリプト
- 04スキャンとストリームのパターン
- 05Cluster と Sentinel の設定ガイド
活用シーン
- 01Redis 接続のセットアップ
- 02複数コマンド操作のパターン選択
- 03Pub/Sub や Lua スクリプトの利用
- 04Redis Cluster または Sentinel の設定
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ jgamaraalv/ ts-dev-kit --skill ioredisインストール後、Claude Code で/ioredisを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
references/advanced-patterns.mdcluster-sentinel.mdconnection-options.mdcore-api.md詳細
ioredis v5 — Node.js Redis クライアント
ioredis v5.x。Node.js >= 12、Redis >= 2.6.12 が必要です。100% TypeScript です。
<quick_reference>
重要:インポート形式
// CORRECT — named import (required for NodeNext / moduleResolution: "nodenext")
import { Redis } from "ioredis";
// For Cluster:
import { Redis, Cluster } from "ioredis";
クイックリファレンス
| 操作 | コード |
|---|---|
| 接続 | new Redis() または new Redis(6379, "host") または new Redis("redis://...") |
| 取得/設定 | await redis.set("key", "val") / await redis.get("key") |
| パイプライン | await redis.pipeline().set("a","1").get("a").exec() |
| トランザクション | await redis.multi().set("a","1").get("a").exec() |
| Pub/Sub | sub.subscribe("ch") / sub.on("message", cb) / pub.publish("ch", msg) |
| Lua スクリプト | redis.defineCommand("name", { numberOfKeys: 1, lua: "..." }) |
| スキャン | redis.scanStream({ match: "prefix:*", count: 100 }) |
| 正常な切断 | await redis.quit() |
| 強制切断 | redis.disconnect() |
</quick_reference>
よくある注意点
- 名前付きインポート:NodeNext 解決を使用する場合は、必ず
import { Redis } from "ioredis"を使用してください - Pub/Sub の分離:サブスクライブ中のクライアントでは他のコマンドを実行できません。別のインスタンスを使用してください
maxRetriesPerRequest:デフォルトは 20 です。無限に再試行するにはnullを設定してください(BullMQ で必須)- パイプラインのエラー:
pipeline.exec()は決して reject しません。エラーは各結果の[0]の位置に格納されます showFriendlyErrorStack:パフォーマンスコストがかかるため、本番環境では有効にしないでください- クラスターのパイプライン:パイプライン内のすべてのキーは、同じノードが処理するスロットにハッシュされる必要があります
enableAutoPipelining:スループットが 35~50% 向上します。グローバルに有効にしても安全です
リファレンスの読み込みタイミング
| 必要な情報 | リファレンスファイル |
|---|---|
| 接続のセットアップ、RedisOptions、TLS、retryStrategy、ライフサイクル | references/connection-options.md |
| コア API:パイプライン、トランザクション、Pub/Sub、Lua スクリプト、スキャン、イベント | references/core-api.md |
| ストリーム、自動パイプライン、トランスフォーマー、バイナリデータ、エラー処理、デバッグ | references/advanced-patterns.md |
| Redis Cluster のセットアップ、ClusterOptions、Sentinel の設定、フェイルオーバー | references/cluster-sentinel.md |
コメント
Markdown 対応