Skip to content

Instantly share code, notes, and snippets.

@ragokan
Created June 7, 2022 21:35
Show Gist options
  • Save ragokan/bd0ffa8897b5bc133a95019c11318875 to your computer and use it in GitHub Desktop.
Save ragokan/bd0ffa8897b5bc133a95019c11318875 to your computer and use it in GitHub Desktop.
Cache middlweware for prisma
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;
};
}
@JRakhimov
Copy link

Does it work with prisma transactions?

@ragokan
Copy link
Author

ragokan commented Jul 10, 2022

Does it work with prisma transactions?

I am not sure, I will try it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment