This project demonstrates cache invalidation timing in a Next.js application using Prisma and Prisma Accelerate, with a Time-to-Live (TTL) strategy. It measures the time it takes for cache invalidation to reflect updated data.
The app provides an interactive interface to test cache invalidation behavior.
- A quote is fetched with a TTL of 60 seconds.
- The fetched quote is cached.
- The quote is updated in the database.
- The cache is invalidated using Prisma Accelerate's
$invalidate
API. - The app polls the API until the updated quote is fetched.
- The total time for invalidation to take effect is displayed, including frontend-to-backend roundtrip delays.
Before you begin, ensure you have the following prerequisites:
- Obtain an Accelerate API key from Prisma Data Platform.
- Create a
.env
file in the project root directory with the following content:
# Accelerate connection string
DATABASE_URL="prisma://accelerate.prisma-data.net/?api_key=ACCELERATE_API_KEY"
# To run migrations
DIRECT_URL="postgresql://username:password@host:5432/database_name"
To get started with this project, follow these steps:
- Install project dependencies:
npm install
- Set up Prisma by running migrations and generating a PrismaClient for edge functions:
npx prisma migrate dev
npx prisma db seed
npx prisma generate --no-engine
- Start the application:
npm run dev
The app will run at http://localhost:3000.
- Click the “Start cache invalidation test” button on the homepage.
- Observe the timeline for cache invalidation and updated data retrieval.
- Review the displayed total invalidation time.
Contributions are welcome! Feel free to open issues or submit pull requests to improve this project.