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 🙏

© 2024 – Pkg Stats / Ryan Hefner

prisma-extension-redis

v1.2.0

Published

Extensive Prisma extension designed for efficient caching and cache invalidation using Redis and Dragonfly Databases

Downloads

934

Readme

prisma-extension-redis

The prisma-extension-redis library is a comprehensive package that provides a unified solution for optimizing database access times, enhancing cache management, and offering versatile functions for efficient Redis/Dragonfly database maintenance.

🚀 If prisma-extension-redis proves helpful, consider giving it a star! ⭐ Star Me!

Installation

Using npm:

npm install prisma-extension-redis

Using yarn:

yarn add prisma-extension-redis

Using pnpm:

pnpm add prisma-extension-redis

Using bun:

bun add prisma-extension-redis

Initialization of setup

import {PrismaClient} from '@prisma/client';
import {Redis} from 'ioredis';
import pino from 'pino';
import {
  getCacheKey,
  getCacheKeyPattern,
  PrismaExtensionRedis,
} from 'prisma-extension-redis';

// Create a Redis client
const redis = new Redis({
  host: env.REDIS_HOST_NAME, // Specify Redis host name
  port: env.REDIS_PORT, // Specify Redis port
});

// Create a pino logger instance for logging
const logger = pino();

Auto cache config

Auto-caching can be enabled for all read operations by default. Set auto to customize behavior or exclude specific models or operations.

const auto = {
  excludedModels: ['Post'], // Models to exclude from auto-caching default behavior
  excludedOperations: ['findFirst', 'count', 'findMany'], // Operations to exclude from auto-caching default behavior
  models: [
    {
      model: 'User',
      excludedOperations: [],
      ttl: 10, // Time-to-live for caching
      stale: 5, // Stale time for caching
    },
  ], // Custom auto-cache configuration for specific models
  ttl: 1, // Default time-to-live for caching
};

Cache Client Config is required to enable auto-cache.

Cache Client Config

This configuration is required for enabling auto-cache and handling caching using cache: true or cache: false per Prisma query (refer use cases).

const cache = {
  ttl: 1, // Time-to-live for caching
  stale: 1, // Stale time for caching
  storage: {
    type: 'redis',
    options: {
      client: redis,
      invalidation: {referencesTTL: 60}, // Invalidation settings
      log: logger, // Logger for cache events
    },
  }, // Storage configuration for async-cache-dedupe
};

Create Prisma Extended Client

// Create a Prisma client instance
const prisma = new PrismaClient();

// Extend Prisma with prisma-extension-redis
const extendedPrisma = prisma.$extends(
  PrismaExtensionRedis({auto, cache, redis})
);

Use case 1: Default Auto-Caching Configuration

// Example: Query a user and cache the result when auto caching is enabled
extendedPrisma.user.findUnique({
  where: {id},
});

// Example: Query a user and cache the result by setting `cache: true` to toggle auto cache
extendedPrisma.user.findUnique({
  where: {id},
  cache: true, // Enable caching with default configuration
});

// Example: Exclude automatic caching for a specific operation
extendedPrisma.user.findFirst({
  where: {userId: id},
  cache: false, // Disable caching for this operation
});

Use case 2: Selective Caching with Custom Configuration

// Example: Query a user and cache the result - with custom configuration
extendedPrisma.user.findUnique({
  where: {id},
  cache: {ttl: 5, key: getCacheKey([{prisma: 'User'}, {userId: id}])},
});

Use case 3: Invalidation of Cached Data

// Example: Update a user and invalidate related cache keys
extendedPrisma.user.update({
  where: {id},
  data: {username},
  uncache: {
    uncacheKeys: [
      getCacheKey([{prisma: 'User'}, {userId: id}]),
      getCacheKeyPattern([{prisma: '*'}, {userId: id}]), // Pattern matching under a specific key, eg: prisma:*:userId:1234
      getCacheKeyPattern([{prisma: 'Post'}, {userId: id}, {glob: '*'}]), // Utilizing the key 'glob' to create a wildcard region, eg: prisma:post:userId:1234:*
    ], // Keys to be invalidated
    hasPattern: true, // Use wildcard pattern for key matching
  },
});

Custom cache invalidation is designed for custom caching (not auto-caching).

Additional Information

  • All durations must be specified in seconds (Time-to-Live, Stale Time, ReferenceTTL).
  • Any configuration for auto-cache exclusion will result in the query being omitted from auto-cache during execution.

Prerequisites

  • Redis.JSON must be enabled in your Redis instance (this is enabled by default in Dragonfly).

Dependencies

  • ioredis

Key Features

  • Automatic Query Result Caching: Easily cache Prisma query results in Redis with minimal configuration.
  • Selective Cache Invalidation: Invalidate specific Prisma queries to ensure accurate and up-to-date data retrieval.
  • Fine-grained Control: Configure caching and invalidation settings on a per-query basis for granular control over caching behavior.
  • Cache Invalidation Strategies: Implement cache invalidation strategies to ensure that cached data remains up-to-date.
  • Versatile Functions: Utilize general-purpose functions for efficient Redis/Dragonfly database maintenance.

Why use prisma-extension-redis?

  • Simplified Dependencies: Instead of managing multiple packages, you now only need prisma-extension-redis for all the features.
  • Enhanced Maintenance: Centralized updates and improvements for all functionalities, leading to easier maintenance.
  • Streamlined Codebase: Consolidate your codebase by eliminating redundant dependencies and optimizing performance.
  • Community Focus: Join the community around prisma-extension-redis for collective support and collaborative development.

Upgrade to prisma-extension-redis today to experience a more streamlined and efficient Redis caching solution.