メインコンテンツへスキップ

APIリファレンス

Prisma Postgres APIリファレンスドキュメントは以下のスキーマに基づいています

model User {
id Int @id @default(autoincrement())
name String?
email String @unique
}

すべての例はUserモデルに基づいています。

cacheStrategy

Prisma Postgres用Prismaクライアント拡張機能を使用すると、モデルクエリにcacheStrategyパラメータを使用し、ttlおよびswrパラメータを使用してPrisma Postgresクエリのキャッシュ戦略を定義できます。クライアント拡張機能にはPrisma Clientバージョン4.10.0のインストールが必要です。

オプション

cacheStrategyパラメータは以下のキーを持つオプションを受け取ります

オプション必須説明
swr60Intいいえstale-while-revalidateの時間(秒単位)。
ttl60Intいいえtime-to-liveの時間(秒単位)。
tags["user"]String[]いいえtagは、アプリケーション内の特定のクエリの無効化を制御するための変数として機能します。これは、キャッシュを無効にするためのオプションの文字列配列であり、各タグは英数字とアンダースコアのみを含み、最大長は64文字です。

クエリにキャッシュ戦略を追加し、60秒間のstale-while-revalidate (SWR)値、60秒間のtime-to-live (TTL)値、および"emails_with_alice"のキャッシュタグを定義します。

await prisma.user.findMany({
where: {
email: {
contains: "alice@prisma.io",
},
},
cacheStrategy: {
swr: 60,
ttl: 60,
tags: ["emails_with_alice"],
},
});

サポートされているPrisma Client操作

以下は、cacheStrategyをサポートするすべての読み取りクエリ操作のリストです。

情報

cacheStrategyパラメータは、create()などの書き込み操作ではサポートされていません。

withAccelerateInfo

cacheStrategyをサポートする任意のクエリは、withAccelerateInfo()を追加して応答データをラップし、キャッシュされた応答に関する追加情報を含めることができます。

応答のステータスを取得するには、以下を使用します。

const { data, info } = await prisma.user
.count({
cacheStrategy: { ttl: 60, swr: 600 },
where: { myField: 'value' },
})
.withAccelerateInfo()

console.dir(info)
情報

応答オブジェクトのinfoプロパティに注目してください。ここにリクエスト情報が格納されます。

戻り値の型

infoオブジェクトはAccelerateInfo型であり、以下のインターフェースに従います。

interface AccelerateInfo {
cacheStatus: 'ttl' | 'swr' | 'miss' | 'none'
lastModified: Date
region: string
requestId: string
signature: string
}
プロパティ説明
cacheStatus"ttl" | "swr" | "miss" | "none"応答のキャッシュステータス。
  • ttlは、ttl期間内のキャッシュヒットを示し、データベースクエリは実行されませんでした。
  • swrは、swr期間内のキャッシュヒットを示し、データはPrisma Postgresによってバックグラウンドで更新されています。
  • missは、ttlswrの両方が期限切れになり、データベースクエリがリクエストによって実行されたことを示します。
  • noneは、キャッシュ戦略が指定されず、データベースクエリがリクエストによって実行されたことを示します。
lastModifiedDate応答が最後に更新された日付。
regionStringリクエストを受け取ったデータセンターリージョン。
requestIdStringリクエストの一意の識別子。トラブルシューティングに役立ちます。
signatureStringPrisma操作の一意のシグネチャ。

$accelerate.invalidate

$accelerate.invalidate APIを使用してキャッシュを無効にできます。

キャッシュされたクエリ結果をオンデマンドで無効にするには、有料プランが必要です。各プランには、1日あたりのキャッシュタグベースの無効化の数に特定の制限がありますが、$accelerate.invalidate API自体の呼び出しには制限がありません。詳細については、弊社の価格設定をご覧ください。

以下のクエリを無効にするには

await prisma.user.findMany({
where: {
email: {
contains: "alice@prisma.io",
},
},
cacheStrategy: {
swr: 60,
ttl: 60,
tags: ["emails_with_alice"],
},
});

$accelerate.invalidate APIでキャッシュタグを提供する必要があります。

try {
await prisma.$accelerate.invalidate({
tags: ["emails_with_alice"],
});
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
// The .code property can be accessed in a type-safe manner
if (e.code === "P6003") {
console.log(
"The cache invalidation rate limit has been reached. Please try again later."
);
}
}
throw e;
}

1回の呼び出しで最大5つのタグを無効にできます。

$accelerate.invalidateAll

$accelerate.invalidateAll APIを使用して、キャッシュ全体を無効にできます。

以下のクエリを無効にするには

await prisma.user.findMany({
where: {
email: {
contains: "alice@prisma.io",
},
},
cacheStrategy: {
swr: 60,
ttl: 60,
tags: ["emails_with_alice"],
},
});

$accelerate.invalidateAll APIを呼び出すだけです。

try {
await prisma.$accelerate.invalidateAll();
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
if (e.code === "P6003") {
console.log(
"The cache invalidation rate limit has been reached. Please try again later."
);
}
}
throw e;
}

なぜ$accelerate.invalidateAllを使用するのか?

このメソッドは、invalidate("all")のような代替手段よりも優れたエディタサポート(例:IntelliSense)を提供します。

警告

これは環境全体のキャッシュをクリアします。注意して使用してください。

エラー

Prisma Postgres関連のエラーはP6xxxで始まります。

Prisma Postgresの完全なエラーコードリファレンスはこちらで確認できます。

© . All rights reserved.