P
Prisma•3mo ago
Gabe

0% cache hits

I just setup accelerate and have verified it's working, but I keep getting 0% cache hit rate. How do I debug this? I think I am making the exact same query repeatedly
3 Replies
RaphaelEtim
RaphaelEtim•3mo ago
Hi @Gabe 👋 Can you confirm that you have explicitly defined a cache strategy for your queries. By default, Accelerate does not cache queries unless you opt-in. Here is an example of how to define a cache strategy:
const user = await prisma.user.findMany({
where: {
email: {
contains: '[email protected]',
},
},
cacheStrategy: { swr: 60, ttl: 60 },
})
const user = await prisma.user.findMany({
where: {
email: {
contains: '[email protected]',
},
},
cacheStrategy: { swr: 60, ttl: 60 },
})
This example sets a stale-while-revalidate (SWR) and time-to-live (TTL) of 60 seconds each. See the documentation
Getting started with Prisma Accelerate | Prisma Documentation
Learn how to get up and running with Prisma Accelerate.
Gabe
GabeOP•3mo ago
Yes, I have via my middleware Lemme make sure by adding it manually to some queries just in case For some reason my middleware just isn't working. Adding the cache strategy to individual queries works
// file: app/lib/server/prismaMiddleware.ts
import { Prisma } from '@prisma/client';
import { PrismaClient } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';

// Dynamically update lastModifiedTimestamp for the `memory` model.
const memoryExtension = Prisma.defineExtension({
name: 'memoryExtension',
model: {
memory: {
async $before(operation, args, next) {
if (operation === 'create' || operation === 'update' || operation === 'updateMany') {
if (!args.data) {
args.data = {};
}
args.data.lastModifiedTimestamp = Date.now();
}
return await next(args);
},
},
},
});

// Cache strategy extension to set a default TTL for relevant queries
const cacheStrategyExtension = Prisma.defineExtension({
name: 'cacheStrategyExtension',
query: {
$allModels: {
async $before(operation, args, next) {
if (
operation === 'findMany' ||
operation === 'findUnique' ||
operation === 'findFirst' ||
operation === 'aggregate' ||
operation === 'groupBy'
) {
if (!args.cacheStrategy) {
args.cacheStrategy = {
ttl: 60,
swr: 60
}; // Default cacheStrategy
}
}
return await next(args);
},
},
},
});

// Initialize Prisma Client with extensions
export const prisma = new PrismaClient({
log: ['query', 'info', 'warn', 'error']
})
.$extends(withAccelerate())
// .$extends(memoryExtension)
.$extends(cacheStrategyExtension);

export default prisma;
// file: app/lib/server/prismaMiddleware.ts
import { Prisma } from '@prisma/client';
import { PrismaClient } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';

// Dynamically update lastModifiedTimestamp for the `memory` model.
const memoryExtension = Prisma.defineExtension({
name: 'memoryExtension',
model: {
memory: {
async $before(operation, args, next) {
if (operation === 'create' || operation === 'update' || operation === 'updateMany') {
if (!args.data) {
args.data = {};
}
args.data.lastModifiedTimestamp = Date.now();
}
return await next(args);
},
},
},
});

// Cache strategy extension to set a default TTL for relevant queries
const cacheStrategyExtension = Prisma.defineExtension({
name: 'cacheStrategyExtension',
query: {
$allModels: {
async $before(operation, args, next) {
if (
operation === 'findMany' ||
operation === 'findUnique' ||
operation === 'findFirst' ||
operation === 'aggregate' ||
operation === 'groupBy'
) {
if (!args.cacheStrategy) {
args.cacheStrategy = {
ttl: 60,
swr: 60
}; // Default cacheStrategy
}
}
return await next(args);
},
},
},
});

// Initialize Prisma Client with extensions
export const prisma = new PrismaClient({
log: ['query', 'info', 'warn', 'error']
})
.$extends(withAccelerate())
// .$extends(memoryExtension)
.$extends(cacheStrategyExtension);

export default prisma;
Gabe
GabeOP•3mo ago
Fixed:
// Cache strategy extension to set a default TTL for relevant queries
export const prisma = new PrismaClient().$extends({
query: {
$allModels: {
$allOperations({ operation, args, query }) {
// Cache strategy extension logic applied to all operations on all models
if (
['findMany', 'findUnique', 'findFirst', 'aggregate', 'groupBy'].includes(operation) &&
!args.cacheStrategy
) {
args.cacheStrategy = {
ttl: 60,
swr: 60, // Default cache strategy
};
}
return query(args);
},
},
},
}).$extends(withAccelerate());
// Cache strategy extension to set a default TTL for relevant queries
export const prisma = new PrismaClient().$extends({
query: {
$allModels: {
$allOperations({ operation, args, query }) {
// Cache strategy extension logic applied to all operations on all models
if (
['findMany', 'findUnique', 'findFirst', 'aggregate', 'groupBy'].includes(operation) &&
!args.cacheStrategy
) {
args.cacheStrategy = {
ttl: 60,
swr: 60, // Default cache strategy
};
}
return query(args);
},
},
},
}).$extends(withAccelerate());
Thanks for the help! I probably should've noticed that cache configured queries was 0 🙂
No description
Want results from more Discord servers?
Add your server