百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

prisma-redis-middleware

> 数据库
开源

Prisma 用于在 Redis 中缓存查询的中间件

363 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Prisma 用于在 Redis 中缓存查询的中间件

prisma-redis-middleware

This is a Prisma middleware used for caching and storing of Prisma queries in Redis (uses an in-memory LRU cache as fallback storage).

Uses async-cache-dedupe.

Features

  • Cache Invalidation
  • Supports custom cache keys
  • Cache persistance with Redis (uses an in-memory LRU cache as fallback)
  • Caching multiple Prisma models each with a specific cache time
  • Excluding certain Prisma models from being cached
  • Excluding certain Prisma queries from being cached across all models

Supported Node.js versions

The latest versions of the following Node.js versions are tested and supported.

  • 16
  • 18

Default Cached Methods

Here is a list of all the Prisma methods that are currently cached by default in prisma-redis-middleware.

  • findUnique
  • findUniqueOrThrow
  • findFirst
  • findFirstOrThrow
  • findMany
  • count
  • aggregate
  • groupBy
  • findRaw
  • aggregateRaw

queryRaw is not cached as it's executed against the Prisma db itself and not a model. This Prisma middleware is used for caching queries based on the models that they are executed against.

Quick Start

Install the package using npm:

bash
npm i --save-exact prisma-redis-middleware

You will also need to install and configure an external dependency for Redis (for example: ioredis or one that uses a similar API) if you don't already have a Redis Client in your project.

bash
npm i --save-exact ioredis @types/ioredis

Code Example (ESM / Import)

…

Code Example (Common JS / Require)

…

API

createPrismaRedisCache(opts)

Options:

  • onDedupe: (optional) a function that is called every time a query is deduped.

  • onError: (optional) a function that is called every time there is a cache error.

  • onHit: (optional) a function that is called every time there is a hit in the cache.

  • onMiss: (optional) a function that is called every time the result is not in the cache.

  • cacheTime: (optional) (number) the default time (in seconds) to use for models that don't have a cacheTime value set. Default is 0.

  • excludeModels: (optional) (string) an array of models to exclude from being cached.

  • excludeMethods: (optional) (string) an array of Prisma methods to exclude from being cached for all models.

  • models: (optional) an array of Prisma models. Models options are:

    • model: (required) string.

    • cacheKey: (optional) string. Default is the model value.

    • cacheTime: (optional) number (in seconds).

    • excludeMethods: (optional) (string) an array of Prisma methods to exclude from being cached for this model.

    • invalidateRelated: (optional) (string) an array of Prisma models to invalidate when mutating this model.

      Example:

      javascript
      createPrismaRedisCache({
        models: [
          { model: "User", cacheTime: 60, invalidateRelated: ["Post"] },
          { model: "Post", cacheKey: "article", excludeMethods: ["findFirst"] },
        ],
      });
  • storage: (optional) the storage options; default is { type: "memory" }. Storage options are:

    • type: memory (default) or redis

    • options: by storage type

      • for memory type

        • size: (optional) maximum number of items to store in the cache. Default is 1024.
        • invalidation: (optional) enable invalidation. Default is disabled.
        • log: (optional) logger instance pino compatible, or console, default is disabled.

        Example:

        javascript
        createPrismaRedisCache({
          storage: { type: "memory", options: { size: 2048 }, log: console },
        });
      • for redis type

        • client: a redis client instance, mandatory. Should be an ioredis client or compatible.
        • invalidation: (optional) enable invalidation. Default is disabled.
        • invalidation.referencesTTL: (optional) references TTL in seconds, it means how long the references are alive; it should be set at the maximum of all the caches ttl.
        • log: (optional) logger instance pino compatible, or console, default is disabled.

        Example

        javascript
        const redis = new Redis();
        
        createPrismaRedisCache({
          storage: {
            type: "redis",
            options: { client: redis, invalidation: { referencesTTL: 60 }, log: console },
          },
        });
  • transformer: (optional) the transformer to used to serialize and deserialize the cache entries. It must be an object with the following methods:

    • serialize: a function that receives the result of the original function and returns a serializable object.

    • deserialize: a function that receives the serialized object and returns the original result.

    • Default is undefined, so the default transformer is used.

      Example

      javascript
      import superjson from "superjson";
      
      createPrismaRedisCache({
        transformer: {
          serialize: (result) => superjson.serialize(result),
          deserialize: (serialized) => superjson.deserialize(serialized),
        },
      });

Debugging

You can pass functions for onMiss, onHit, onError and onDedupe to createPrismaRedisCache which can then be used to debug whether a Prisma query is being cached or not.

You can also pass a custom log (pino or console) to the storage option and async-cache-dedupe will print debug info as it queries, sets, expires and invalidates the cache. Note that the log option can print out very verbose output.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptcachingprismaprisma-middlewareredis

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库