npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

prisma-extension-redis-auto-cache

v1.1.0

Published

Prisma extension for automatic Redis caching of queries with intelligent invalidation

Downloads

43

Readme

prisma-extension-redis-auto-cache

A Prisma extension that automatically caches query results in Redis and handles cache invalidation intelligently.

Features

  • 🚀 Automatic caching of findUnique, findFirst, and findMany queries
  • 🔄 Automatic cache invalidation on create, update, delete, and upsert operations
  • ⚙️ Configurable cache TTL and model exclusions
  • 🔑 Intelligent cache key generation
  • 🐉 Support for Redis and Dragonfly
  • 🔍 Debug logging option
  • 🎯 TypeScript support
  • 🔌 Flexible Redis connection options (URL, config, or existing client)

Installation

npm install prisma-extension-redis-auto-cache
# or
yarn add prisma-extension-redis-auto-cache

Usage

Basic Usage with Redis URL

import { PrismaClient } from '@prisma/client'
import { withRedisCache } from 'prisma-extension-redis-auto-cache'

const prisma = new PrismaClient().$extends(
  withRedisCache({
    redis: {
      type: 'url',
      url: 'redis://localhost:6379',
      options: {
        password: 'optional-password'
      }
    },
    ttl: 300, // 5 minutes
    excludeModels: ['Log'], // Models to exclude from caching
    prefix: 'my-app', // Custom cache key prefix
    debug: true, // Enable debug logging
  })
)

Using Redis Connection Config

const prisma = new PrismaClient().$extends(
  withRedisCache({
    redis: {
      type: 'config',
      config: {
        host: 'localhost',
        port: 6379,
        password: 'optional-password',
        db: 0
      }
    }
  })
)

Using Existing Redis Client

import Redis from 'ioredis';

const redisClient = new Redis({
  host: 'localhost',
  port: 6379
});

const prisma = new PrismaClient().$extends(
  withRedisCache({
    redis: {
      type: 'client',
      client: redisClient
    }
  })
)

Configuration Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | redis | RedisConnection | Required | Redis connection configuration (URL, config, or client) | | ttl | number | 300 | Time-to-live for cached items in seconds | | excludeModels | string[] | [] | Array of model names to exclude from caching | | prefix | string | 'prisma-cache' | Prefix for cache keys | | debug | boolean | false | Enable debug logging | | keyGenerator | Function | Built-in | Custom function for generating cache keys |

Redis Connection Types

The redis option accepts three types of configurations:

  1. URL with Options
{
  type: 'url',
  url: 'redis://localhost:6379',
  options?: RedisOptions // Optional ioredis options
}
  1. Connection Config
{
  type: 'config',
  config: RedisOptions // ioredis connection options
}
  1. Existing Client
{
  type: 'client',
  client: Redis // Existing ioredis instance
}

Custom Key Generator

You can provide a custom key generator function to control how cache keys are generated:

const prisma = new PrismaClient().$extends(
  withRedisCache({
    redis: { type: 'url', url: 'redis://localhost:6379' },
    keyGenerator: (model: string, operation: string, args: any) => {
      return `custom:${model}:${operation}:${JSON.stringify(args)}`
    }
  })
)

How It Works

  1. Query Caching: When a findUnique, findFirst, or findMany operation is performed, the extension:

    • Generates a unique cache key based on the model, operation, and query arguments
    • Checks if the result exists in Redis
    • If found, returns the cached result
    • If not found, executes the query and caches the result
  2. Cache Invalidation: When a create, update, delete, or upsert operation is performed:

    • All cached entries for the affected model are invalidated
    • This ensures data consistency while maintaining simplicity
  3. Metadata Storage: The extension maintains metadata about cached items using Redis sets and hashes, enabling efficient invalidation and monitoring.

Best Practices

  1. Model Exclusion: Consider excluding frequently changing models or models with sensitive data from caching.

  2. TTL Configuration: Set an appropriate TTL based on your data's update frequency and consistency requirements.

  3. Redis Configuration: For production, configure Redis with appropriate persistence and memory settings.

  4. Client Management: When using an existing Redis client, make sure to manage its lifecycle appropriately.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.