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

@paulwer/prisma-extension-cache-manager

v2.5.0

Published

A caching extension for [Prisma](https://www.prisma.io/), fully compatible with [cache-manager](https://www.npmjs.com/package/cache-manager), predefined uncaching strategies and custom handlers for key generation and uncaching.

Downloads

249

Readme

@paulwer/prisma-extension-cache-manager

A caching extension for Prisma, fully compatible with cache-manager, predefined uncaching strategies and custom handlers for key generation and uncaching.

Features

  • full cache-manager compatibility => also supports external storages like redis (see cache-manager)
  • Model queries and custom queries are cacheable (additional methods $queryRawCached or $queryRawUnsafeCached)
  • Automatic uncaching strategy
  • Namespaces for separate caching ttl
  • Custom keys for custom caching strategies
  • Cache-Keys and Uncache-Keys can be handled with a custom function after data fetching
  • Deduplicate concurrent queries

Installation

Install:

npm i @paulwer/prisma-extension-cache-manager

Usage

import { PrismaClient } from "@prisma/client";
import * as cm from "cache-manager";
import cacheExtension from "@paulwer/prisma-extension-cache-manager";

async function main() {
  const cache = await cm.caching("memory", {
    ttl: 10000,
    max: 200,
  });
  const prisma = new PrismaClient().$extends(
    cacheExtension({ cache, useAutoUncaching: true }),
  );
  await prisma.user.findUniqueOrThrow({
    where: {
      email: user.email,
    },
    cache: true, // using cache default settings
  });
  await prisma.user.findMany({
    cache: 5000, // setting ttl in milliseconds
  });
  await prisma.user.count({
    cache: {
      ttl: 2000,
      key: "user_count", // custom cache key
    },
  });
  await prisma.user.count({
    cache: {
      ttl: 24 * 60 * 60 * 1000,
      namespace: "pricing_tier1", // custom namespace for custom ttls
    },
  });
  await prisma.user.update({
    data: {},
    cache: {
      ttl: 2000,
      key: (result) => `user-${result.id}`, // custom cache key by result (There will be no reading from the cache, only a write down)
    },
  });
  await prisma.user.create({
    data: {},
    uncache: `user_count`, // delete key from cache
  });
  await prisma.user.create({
    data: {},
    cache: {
      ttl: 2000,
      key: (result) => `user-${result.id}`, // custom cache key by result (There will be no reading from the cache, only a write down)
    },
    uncache: [`user_count`, `users`], // delete keys from cache
  });
  // Custom Queries
  await prisma.$queryRawCached(
    Prisma.sql`SELECT * FROM "User" WHERE id = ${1}`,
    {
      cache: {
        namespace: "test",
        ttl: 2000,
        key: (result) => `user-${result[0].id}`, // custom cache key by result (There will be no reading from the cache, only a write down)
      },
      uncache: [`user_count`, `users`], // delete keys from cache
    },
  );
  await prisma.$queryRawUnsafeCached(
    Prisma.sql`SELECT * FROM "User" WHERE id = 1`,
    {
      cache: "custom_query1",
      uncache: {
        namespace: "test", // delete keys from cache
      },
    },
  );
}

main().catch(console.error);

Customize Caching

Caching Key

By default this extension will create a cache-key in the format of <namespace?>:<model>:<operation>@<args-hash>.

You can customize this behavior by providing one or both of the following parameters. Both parameters can also be computed by a function which gets passed the result of the query for even more customization options.

namespace By providing a namespace you can prefix the key and handle seperate caching ttls.

key By providing a custom key you can define how the caching key is generated. When using a custom key, the cache key will be generated as following: <key> or <namespace>:<key>.

Automatic Uncaching

When a write-operation was performed on a model, all cache-data for this model will be removed. We also support nested write operations.

Important Notice: This will only work for the default caching keys.

Deduplication

When using deduplication a map of running promisses is kept on your local server to deduplicate requests with the same cache-key.

Important Notice: Deduplication is not supported for custom key functions as they determine the cache-key after the execution.

TTL

You can customize the ttl of the cache key. The plugin will use the first ttl only when originaly creating the cache entry.

(De-)Serialization

This plugin serialize/deserialize some classes used by prisma to string with a prefix to deserialize it back when using cache later. You can customize this behavior by passing the prefixes property to the plugin while initialization.

Planned features

  • more granular automatic uncaching
  • performance improvements for uncaching

Limitations & Important Considderations

  1. Be carefull when using custom cache-keys and automatic-uncaching. If you produce an overlay it could happen, that more cache entries gets deleted than exspected.
  2. Automatic Uncaching only works when using @prisma/client. Custom generated client which are loaded from another origin/package are not supported yet.
  3. when using custom key generator functions, you cannot rely on a cache for this function. those should only be used to generate the cache for other functions.

Credit

Original Implementation by @knaus94

Learn more