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-cache-extension

v1.0.1

Published

A Prisma extension for caching database queries.

Downloads

112

Readme

Prisma Cache Extension

Build Dynamic JSON Badge

The prisma-cache-extension is a robust package designed to optimize database performance by improving access times, streamlining cache management, and offering flexible tools for efficient maintenance.

Databases supported:

  • Redis

Installation

Using npm:
npm install prisma-cache-extension
Using yarn:
yarn add prisma-cache-extension

Initializtion of setup

import {PrismaClient} from '@prisma/client';
import {Redis} from 'ioredis';
import {
    PrismaCacheExtension,
} from 'prisma-cache-extension';

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

Cache Client Config

This configuration is required for enabling caching.

const cache: CacheConfig = {
    storage: {
        type: 'redis',
        options: {
            client: redis,
            invalidation: {
                referencesTTL: 15
            }, // Invalidation settings
            // Logger for cache events
            log: console,
        },
    },
};

Create Prisma Extended Client

// Create a Prisma client instance
const prisma = new PrismaClient();
return prisma.$extends(
    PrismaExtensionRedis(30, {
        cache, redis
    }));

Use case 1: Caching Configuration

// Example: Query a user and cache the result when caching is enabled
prisma.user.findUnique({
    where,
    cache: {
        key: JSON.stringify(where)
    }
});


// Disable caching for this operation, if not mentioned
extendedPrisma.user.findFirst({
    where: {userId: id},

});

Use case 2: Invalidation of Cached Data

// Example: Update a user and invalidate related cache keys
extendedPrisma.user.update({
    where: {id},
    data: {username},
    uncache: {
        uncacheKey: "*",
        models: ["user", "userprofile"],
        hasPattern: true, // Use wildcard pattern for key matching
    },
});

Dependencies

  • ioredis
  • micromatch
  • async-cache-dedupe

Key Features

Why Choose prisma-cache-extension?

Effortless Query Caching: Seamlessly cache your Prisma query results in Redis with minimal setup required.

Targeted Cache Invalidation: Easily invalidate specific Prisma queries to maintain accurate and up-to-date data.

Granular Control: Adjust caching and invalidation settings for each query, giving you precise control over caching behavior.

Dynamic Invalidation Strategies: Apply effective cache invalidation strategies to keep your cached data current.

Versatile Maintenance: Leverage general-purpose functions to efficiently manage Redis or Dragonfly databases.


Benefits of prisma-cache-extension:

  • Unified Dependencies: Replace multiple packages with a single prisma-cache-extension dependency, simplifying your setup.
  • Streamlined Maintenance: Enjoy centralized updates and improvements, making maintenance more straightforward.
  • Optimized Codebase: Reduce redundancy and enhance performance by consolidating your codebase.
  • Community Engagement: Connect with the prisma-cache-extension community for support and collaborative development.

Upgrade to prisma-cache-extension today for a more efficient and integrated Redis caching solution. If this proves helpful, consider giving it a star! ⭐ Star Me!