Created
June 7, 2022 21:35
-
-
Save ragokan/bd0ffa8897b5bc133a95019c11318875 to your computer and use it in GitHub Desktop.
Cache middlweware for prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Prisma } from "@prisma/client"; | |
import Redis from "ioredis"; | |
const mutationActions = ["create", "update", "delete", "deleteMany", "updateMany"]; | |
const queryActions = ["findUnique", "findMany", "count"]; | |
const allActions = [...mutationActions, ...queryActions]; | |
export function cacheMiddleware( | |
redis: Redis, | |
cacheDuration = 100 // 100 seconds | |
): Prisma.Middleware<any> { | |
return async (operation, execute) => { | |
const { action, model = "", args: _args = {} } = operation; | |
if (!allActions.includes(action)) return execute(operation); | |
const args = JSON.stringify(_args); | |
// If the action is findUnique or findMany, we read it from the cache. | |
if (queryActions.includes(action)) { | |
const key = `${model}:${action}:${args}`; | |
const cache = await redis.get(key); | |
if (cache) { | |
// Update the cache expire time. | |
await redis.expire(key, cacheDuration); | |
return JSON.parse(cache); | |
} | |
const data = await execute(operation); | |
await redis.set(key, JSON.stringify(data), "EX", cacheDuration); | |
return data; | |
} | |
// If the action is create, update or delete, we firstly evaluate the method. | |
const data = await execute(operation); | |
if (action === "create") { | |
const keys = await redis.keys(`${model}:findMany:*`); | |
for (const key of keys) { | |
await redis.del(key); | |
} | |
const keysToIncrement = await redis.keys(`${model}:count:*`); | |
for (const key of keysToIncrement) { | |
await redis.incr(key); | |
} | |
} else { | |
const keys = await redis.keys(`${model}:*`); | |
for (const key of keys) { | |
await redis.del(key); | |
} | |
} | |
return data; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am not sure, I will try it soon.