prisma-redis-middleware is a Prisma middleware designed to cache the results of Prisma queries in Redis, significantly improving application performance by reducing database load. It also provides an in-memory LRU cache as a fallback mechanism. The current stable version is 4.8.0, with frequent patch and minor releases, indicating active development and responsiveness to community needs. Key features include fine-grained cache invalidation, support for custom cache keys, persistence with Redis, and the ability to define specific caching rules for individual Prisma models and methods (e.g., `findUnique`, `findMany`, `count`, `aggregate`, `groupBy`). It allows developers to include or exclude certain models or query methods from being cached, and define custom cache times per model, distinguishing it from simpler caching solutions. The middleware internally leverages `async-cache-dedupe` for efficient request deduplication. Developers must provide their own Redis client implementation, such as `ioredis`, which is a common and recommended choice for robust Redis connectivity.
npm install prisma-redis-middlewareVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to initialize and apply the Prisma Redis caching middleware with `ioredis`, configuring specific model caching rules, excluding certain methods, and logging cache hits/misses.
Upgrade your Node.js environment to a supported version (16.x or 18.x) as specified in the package's `engines` field.
Install a compatible Redis client like `ioredis`: `npm install ioredis @types/ioredis` and pass an initialized client instance to the middleware configuration.
Consult the release notes for both `prisma-redis-middleware` and `@prisma/client` to verify compatibility. Update both packages concurrently if necessary, following official documentation.
Thoroughly test your cache invalidation logic in development. Understand the implications of `cacheTime` for read operations and `referencesTTL` for write-triggered invalidations to prevent stale data.
Experiment with the order of middleware in your `$use` chain. Caching middleware is often placed early for read operations and strategically for write operations to ensure proper invalidation. Refer to Prisma's middleware documentation for best practices.
Install `ioredis` and its types: `npm install ioredis @types/ioredis`.
Update `@prisma/client` to a version that supports middleware (Prisma 2.x or newer). Ensure `new PrismaClient()` is called before attempting to apply middleware.
Verify that your Redis server is running and accessible. Check the Redis client configuration (e.g., `REDIS_URL` environment variable or explicit options) for correct host, port, and authentication credentials.
Ensure `import Prisma from 'prisma';` is used to import the `Prisma` namespace correctly. The `createPrismaRedisCache` function returns the middleware function, which is then assigned to a variable typed as `Prisma.Middleware`.