@fortedigital/nextjs-cache-handler
Version:
Next.js cache handlers
36 lines (35 loc) • 879 B
JavaScript
// src/handlers/composite.ts
function createHandler({
handlers,
setStrategy: strategy
}) {
if (handlers?.length < 2) {
throw new Error("Composite requires at least two handlers");
}
return {
name: "composite",
async get(key, ctx) {
for (const handler of handlers) {
const value = await handler.get(key, ctx);
if (value !== null) {
return value;
}
}
return null;
},
async set(key, data) {
const index = strategy?.(data) ?? 0;
const handler = handlers[index] ?? handlers[0];
await handler.set(key, data);
},
async revalidateTag(tag) {
await Promise.all(handlers.map((handler) => handler.revalidateTag(tag)));
},
async delete(key) {
await Promise.all(handlers.map((handler) => handler.delete?.(key)));
}
};
}
export {
createHandler as default
};