P
Prisma3mo ago
Markigno

caching

Ciao, i want to cache the result of a query. What I didn't understand is if the cache works per query parameter. I mean, if I cache user table data for userId=1, if i run a query with userId=2, the second query has a different cache and the output is "where" based?
1 Reply
Prisma AI Help
Prisma AI Help3mo ago
You opted for speed from this bot. Let's conjure up an answer right now! Short answer: yes—each unique query (including its parameters) gets its own cache entry. With Prisma Accelerate/Prisma Postgres, the cache key is a hash of the entire Prisma query, including the where values. So a query for userId=1 and a query for userId=2 will use different cache entries. Only an exact match (same model operation, same filters, same parameter values, same selected fields, etc.) will hit the same cache. [Stage 3; Cache API] Example (per-query caching):
const user1 = await prisma.user.findUnique({
where: { id: 1 },
cacheStrategy: { ttl: 60 }, // caches result for id=1
});

const user2 = await prisma.user.findUnique({
where: { id: 2 },
cacheStrategy: { ttl: 60 }, // separate cache entry for id=2
});
const user1 = await prisma.user.findUnique({
where: { id: 1 },
cacheStrategy: { ttl: 60 }, // caches result for id=1
});

const user2 = await prisma.user.findUnique({
where: { id: 2 },
cacheStrategy: { ttl: 60 }, // separate cache entry for id=2
});
Notes: - You control caching per read query using cacheStrategy (ttl, swr, optional tags). [Cache API; Accelerate getting started] - You can tag queries and invalidate specific cached results on demand. [Caching queries; Accelerate getting started] If you meant prepared statement caching (not result caching): Prisma also caches prepared statements per connection, but that’s separate from result caching. [PostgreSQL prepared] ✅ Great, thanks for letting us know that this solved your question!

Did you find this page helpful?