跳到主要内容

API 参考

Prisma Postgres API 参考文档基于以下模式

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

所有示例都基于 User 模型。

cacheStrategy

结合 适用于 Prisma Postgres 的 Prisma 客户端扩展,您可以在模型查询中使用 cacheStrategy 参数,并使用 ttlswr 参数为您的 Prisma Postgres 查询定义缓存策略。此客户端扩展要求您安装 Prisma Client 版本 4.10.0

选项

cacheStrategy 参数接受一个选项,包含以下键:

选项示例类型必需描述
swr60Intstale-while-revalidate 时间(秒)。
ttl60Inttime-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 使缓存失效。

注意

要按需使缓存的查询结果失效,需要付费计划。每个计划对每天允许基于缓存标签的失效次数有特定限制,但对调用 $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;
}
注意

每次调用最多可以使 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 的完整错误码参考。